Sharding
Last updated
Last updated
Sharding is the method by which a bot's code is "split" into multiple instances of itself. When a bot is sharded, each shard handles only a certain percentage of all the guilds the bot is on.
There are additional difficulties when sharding a bot that add complexity to your code (one of the reasons you shouldn't shard too early). You do not need to worry about sharding until your bot hits around 2,400 guilds. YOU MUST SHARD by the time you hit 2,500 guilds, however.
There are two styles of sharding that we'll be discussing: and . Each of these sharding styles holds benefits depending on your situation.
internal
sharding is the method by which a bot's code creates multiple shard connections to the Discord API within a single process. This means that all the guilds, channels, and users on one shard will be available to another shard via a direct call (e.g. client.guilds.cache.get('GUILD_ID')
). Due to the large memory size the single bot process will grow to using this style of sharding, it is not ideal for bots with many guilds.
If you would like to use this, adjust the Client options in your main bot file where you define your client like so:
traditional
sharding is the method by which a bot's code spawns individual child processes via a main shard manager process, each child process being one shard of the bot. When using this style of sharding, guilds, channels, and users on one shard will not be available to another via direct call (e.g. client.guilds.cache.get('GUILD_ID')
) because each shard is in a separate process.
To learn how to make use of this, read on!
Collections do not cache data from all shards, so you can't grab data from a guild in another shard easily.
And again:
Traditionally sharded bots often gain very marginal performance increase and might even use more memory due to using more node processes.
If you're using any sort of database or connection, multiple shards may cause issues with multiple processes connecting to a single end point.
These two functions are your go-to for getting any information from other shards, so get familiar with them!
Example:
Let's say you want to do something like get your total server count - In a non-sharded environment, this would be as simple as getting the client.guilds.cache.size
. However, in this case client.guilds.cache.size
will not return the total servers your bot is in. Instead it returns only the total number of servers on this shard, like in the first part of the example above.
Here's an example of a function that uses fetchClientValues()
to first get, then add the total number of guilds from all shards (i.e. your bot's total guild count):
Example:
Say you want to get a guild from your client. In a non-sharded environment, you would simply use client.guilds.cache.get('ID')
or something of that nature and then carry on with your code. In this case however, it is possible that the guild you're trying to get is not present on the shard. In order to get the guild for use, you would then need to fetch it from whatever shard it is present on using broadcastEval()
.
Here's an example of a function that uses broadcastEval()
to get a single guild no matter what shard it is present on:
This style of sharding is ideal for larger bots, or bots that need to be scalable to allow for future growth. The rest of this page will discuss and .
In order to do anything across shards you need to worry about using and (Examples and explanation below).
Remember how we were talking about sharding being a method of "splitting" the bot into multiple instances of itself? Because of this, things like your adding your total guilds or getting a specific guild are not as simple as they were before. We must now use either or to get information from across shards.
gets Client properties from all shards. This is what you should use when you would like to get any of the nested properties of the Client, such as guilds.cache.size
or uptime
. It's useful for getting things like Collection sizes, basic client properties, and unprocessed information about the client.
evaluates the input in the context of each shard's Client(s). This is what you should use when you want to execute a method or process data on a shard and return the result. It's useful for getting information that isn't available through client properties and must instead be retrieved through the use of methods.
Example of a Guild object returned by :