AltivoraAltivora
Back to Blog Overview

Update: our game, bot and database hosting plans are closed for now. A VPS is the better way to run them.

The workloads in this guide run great on an Altivora Cloud VPS. You get full root access, so you can host any game server, Discord bot, Lavalink node or database yourself, with fast NVMe storage, DDoS protection and no per-slot limits. Choose the location closest to your players: India, Singapore, USA, Germany or Canada. Pay in INR with UPI or by card.

See VPS Plans
Discord BotOptimizationLatencyShardingPerformance

How to Reduce Discord Bot Latency: Optimization & Sharding Guide

Altivora Developer Hub
11 min read

Nothing frustrates server members more than a bot that takes several seconds to process a moderation command or fails with 'The application did not respond'. Here is a comprehensive technical blueprint for stripping hundreds of milliseconds of latency off your bot.


The 4 Main Sources of Discord Bot Latency

  1. Network Transit Time: The physical round-trip time between your host server and Discord's gateway clusters.
  2. Event Loop Congestion: Synchronous blocking code in JavaScript or Python that prevents incoming WebSocket events from being processed.
  3. Database Query Bottlenecks: Slow disk IOPS or unindexed queries holding up command execution.
  4. Discord REST API Rate Limits: Hitting Discord's 50 requests/second per-route rate limits.

Step-by-Step Latency Optimization Blueprint

1. Enable Zlib WebSocket Payload Compression

By default, WebSocket event payloads can be several kilobytes. Enabling zlib payload compression reduces bandwidth usage by 60% and speeds up event deserialization:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
    intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
    ws: { compress: true }
});

2. Cache Pruning & Memory Sweepers

Prevent memory bloat by cleaning old message objects:

const client = new Client({
    intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
    sweepers: {
        messages: {
            interval: 1800, // Sweep every 30 minutes
            lifetime: 900   // Remove messages older than 15 minutes
        },
        users: {
            interval: 3600,
            filter: () => user => user.id !== client.user.id
        }
    }
});

3. In-Memory Redis Layer for Guild Configuration

Instead of querying PostgreSQL on every incoming message:

const Redis = require('ioredis');
const redis = new Redis();

async function getGuildPrefix(guildId) {
    const cached = await redis.get(`prefix:${guildId}`);
    if (cached) return cached;
    
    const dbPrefix = await db.fetchPrefix(guildId);
    await redis.set(`prefix:${guildId}`, dbPrefix, 'EX', 3600);
    return dbPrefix;
}
High-Performance Infrastructure

Deploy Dedicated NVMe Cloud VPS

Deploy high-IOPS Gen4 NVMe compute across USA, Germany (Frankfurt), India (Mumbai & Noida), and Singapore, with DDoS protection included.

Explore VPS Plans

Ready to upgrade your infrastructure?

Experience the Altivora difference with a 99.9% uptime SLA and DDoS protection on every server.