Articles on: Bots & Applications
This article is also available in:

Sharding on Discord: how to scale your bot (2,500+)

Sharding: How to Scale Your Discord Bot on Square Cloud


When a Discord bot grows and reaches the 2,500 server mark, Discord requires it to use a technique called Sharding. Even before reaching this milestone, sharding can be a smart strategy to manage resources and prevent a failure in one server from bringing down the bot's connection across all others.




1. What Is Sharding?


Sharding is the process of splitting your bot's workload into multiple instances or "pieces" (shards). Instead of a single WebSocket connection handling all events from all servers, each shard takes care of a specific subset of servers.


Think of sharding as opening multiple cash registers at a supermarket instead of having just one cashier handling a mile-long line. This avoids processing bottlenecks and improves the bot's responsiveness.




2. Why Use Sharding on Square Cloud?


Hosting a sharded bot on Square Cloud offers strategic advantages:


  • Error Isolation: If one shard encounters a critical error and crashes, the other shards continue operating normally.
  • Memory Management: From 1 GB of RAM, Square Cloud sizes the Node.js heap to the app's RAM. Sharding splits the event load; it does not get around a memory limit.
  • Discord Limits: Discord enforces a strict limit of 2,500 servers per shard. Without sharding, from 2,500 servers on Discord refuses the bot's gateway connection (error 4011, Sharding required). Learn more here.




3. Technical Implementation


In JavaScript (Discord.js)

Discord.js features a native ShardingManager. You typically create a shard.js file to manage your main file (index.js).


const { ShardingManager } = require('discord.js');

const manager = new ShardingManager('./index.js', {
token: 'YOUR_TOKEN_HERE',
totalShards: 'auto'
});

manager.on('shardCreate', shard => console.log(`Launching Shard ${shard.id}`));
manager.spawn();


In Python (Discord.py / Nextcord)

In Python, sharding is usually implemented by changing the bot class to AutoShardedBot.


import discord
from discord.ext import commands

intents = discord.Intents.default()
bot = commands.AutoShardedBot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
print(f'Bot online with {len(bot.shards)} shards.')

bot.run('YOUR_TOKEN_HERE')




4. Configuration on Square Cloud


When deploying a sharded bot, pay close attention to two points in your squarecloud.app file:


  1. MAIN: Point to your manager file (e.g., shard.js) and not directly to the bot file if you are using Node.js's ShardingManager.
  2. MEMORY: Sharded bots consume more RAM because each shard acts as a new instance of the bot serving a specific set of servers. Make sure your plan has enough RAM to support your intended number of shards.




5. Things to Keep in Mind When Sharding


  • Cross-Shard Communication: With discord.js's ShardingManager, each shard is a separate process, which isolates failures and adds up RAM usage. With discord.py's AutoShardedBot, all shards run in one process and share memory. To exchange data between ShardingManager shards, use your library's broadcast methods.
  • Global State: Avoid storing important data in global variables. Use a database (such as Redis, MongoDB, or PostgreSQL) to keep information synchronized across all shards.

Updated on: 10/04/2026

Was this article helpful?

Share your feedback

Cancel

Thank you!