Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharding Guide #9562

Open
wants to merge 17 commits into
base: docs/guide
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 81 additions & 0 deletions docs/guide/topics/sharding.rst
@@ -0,0 +1,81 @@
.. currentmodule:: discord

.. _guide_sharding:

Sharding
==========

When bots start to get considerably large, the amount of events they have to deal with can start to become problematic.

At high user and guild counts the incoming messages, typing events, and status updates can start to climb to being as frequent as hundreds per second.
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

To help deal with this large amount of traffic, Discord supports **sharding**, a feature where your bot can split its guilds amongst separate connections, reducing the amount of data each individual connection has to handle.

This not only helps Discord by reducing how much data they need to direct to one place, but also helps us, as we can split our bot's overall work across different connections, environments, or even entirely different machines.
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

Discord recommends using sharding once you get over 1,000 guilds, and once you reach beyond 2,500 guilds, it becomes a requirement.

Now, let's discuss what options we have for setting up sharding within discord.py.
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

Client vs AutoShardedClient
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Both :class:`~Client` and :class:`~ext.commands.Bot` have auto sharded variants, :class:`~AutoShardedClient` and :class:`~ext.commands.AutoShardedBot` respectively.

The key difference between the two is that the former can only support one connection (one shard), while the auto sharded variants can handle multiple gateway connections. If you are running multiple shards, it is recommended to use the auto sharded variants and have multiple connections in each process.
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

There's 2 ways you can do sharding:
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

Using auto sharding
~~~~~~~~~~~~~~~~~~~~~

If you want to use a single process and the recommended number of shards by Discord, you can simply use :class:`~AutoShardedClient` or :class:`~ext.commands.AutoShardedBot` instead of :class:`~Client` or :class:`~ext.commands.Bot` respectively.
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

Specifying shard count and IDs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You may want to specify a total shard count instead of relying on Discord's recommendation if you find you need more or less shards, or if you want to keep it consistent across restarts of your bot.
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

.. warning::

When specifying a shard count, note that each shard must have less than 2,500 guilds.

Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved
Specifying shard IDs is useful for bots running as multiple processes. For example, if a bot has 16 shards, you may have one process run shards 0-7 and another process run shards 8-15. These values can be specified with, for example, an environment variable, to allow passing different values to each process. If you specify shard IDs, you must also specify a shard count.

Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved
.. note::

This does not have the same effect vice versa.
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

Examples
~~~~~~~~~~

Let's make a bot using :class:`~ext.commands.AutoShardedBot`:

.. code-block:: python3

import discord
from discord.ext import commands

bot = commands.AutoShardedBot(command_prefix='!')
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

@bot.command()
async def shards(ctx):
await ctx.send(f"I am running on {bot.shard_count} shards!")

bot.run("token")


If you don't wanna use discord's recommended shard count, you can specify your own:
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python3

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!', shard_count=10)
Hadock-is-ok marked this conversation as resolved.
Show resolved Hide resolved

@bot.command()
async def shards(ctx):
await ctx.send(f"I am running on {bot.shard_count} shards!")

bot.run("token")