Articles on: Bots & Applications

Sharding: How to scale your bot on Square Cloud

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: Node.js and Python have memory limits per process. Splitting the bot into shards allows you to make better use of your plan's total RAM.
  • Discord Limits: Discord enforces a strict limit of 2,500 servers per shard. Without sharding, your bot will stop receiving events once it hits this limit. 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.


from discord.ext import commands

bot = commands.AutoShardedBot(command_prefix='!')

@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: Shards run as isolated processes. To exchange data between them, you will need to 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: 05/20/2026

Was this article helpful?

Share your feedback

Cancel

Thank you!