-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Sharding Guide #9562
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
Open
Hadock-is-ok
wants to merge
17
commits into
Rapptz:docs/guide
Choose a base branch
from
Hadock-is-ok:docs/guide
base: docs/guide
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sharding Guide #9562
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6c4b047
Fix commit line with a very ugly method.
Hadock-is-ok 30d8514
Added a small note about specifying shard_count in AutoShardedBot
Hadock-is-ok 5965e2c
Update docs/guide/topics/sharding.rst
Hadock-is-ok dad5081
Update docs/guide/topics/sharding.rst
Hadock-is-ok 50bf782
Changed some wording, added a section on large bot sharding.
Hadock-is-ok 374be1f
Mistake in wording, almost useless commit
Hadock-is-ok 24731af
fix misleading documentation about shards in commands.Bot
Hadock-is-ok 306d3ed
Merge branch 'Rapptz:docs/guide' into docs/guide
Hadock-is-ok 7950ca6
fix hyperlink.
Hadock-is-ok 9aad396
Small fix to update invalid case.
Hadock-is-ok 8a6247b
Update clarification on 0-indexing.
Hadock-is-ok 2ab7d8b
grammar.
Hadock-is-ok 97c7863
use simpler words, add info about default
Hadock-is-ok 717ab34
Update docs/guide/topics/sharding.rst
Hadock-is-ok 37460b6
Specify how to achieve a specific behaviour.
Hadock-is-ok 7b5492b
Add clarification on the shard formula
Hadock-is-ok ad95323
Explain manual shard count better.
Hadock-is-ok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
.. 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 number of incoming messages, typing events, and status updates can start to become as high as hundreds per second. | ||
|
||
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 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 work across different connections, environments, or even entirely different machines. | ||
|
||
Discord recommends sharding when your application reaches 1,000 guilds. Once your application reach 2,500 guilds, sharding is required. | ||
|
||
Let's discuss our options for setting up sharding with discord.py. | ||
|
||
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. | ||
|
||
There's 2 ways you can do sharding: | ||
|
||
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.
Outdated
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.
Outdated
Show resolved
Hide resolved
|
||
|
||
.. note:: | ||
|
||
When nearing 150,000 guilds, Discord will migrate your bot to [large bot sharding](https://discord.com/developers/docs/topics/gateway#sharding-for-large-bots). More details are available on their documentation page. | ||
Hadock-is-ok marked this conversation as resolved.
Outdated
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. | ||
Hadock-is-ok marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Hadock-is-ok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.. note:: | ||
|
||
If you specify shard IDs, you must also specify a shard count. This does not do the same effect vice versa. | ||
Hadock-is-ok marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
.. warning:: | ||
|
||
Don't forget that indexing in Python starts at 0, and that shard_id 0 is a valid thing. | ||
Hadock-is-ok marked this conversation as resolved.
Outdated
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 | ||
|
||
intents = discord.Intents.default() | ||
intents.message_content = True | ||
bot = commands.AutoShardedBot(command_prefix='!', intents=intents) | ||
|
||
@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 want to use discord's recommended shard count, you can specify your own: | ||
|
||
.. code-block:: python3 | ||
|
||
import discord | ||
from discord.ext import commands | ||
|
||
intents = discord.Intents.default() | ||
intents.message_content = True | ||
bot = commands.AutoShardedBot(command_prefix='!', shard_count=10, intents=intents) | ||
|
||
@bot.command() | ||
async def shards(ctx): | ||
await ctx.send(f"I am running on {bot.shard_count} shards!") | ||
|
||
bot.run("token") | ||
|
||
.. note:: | ||
|
||
You can specify the shard_count in AutoShardedBot, but it makes more sense to use :class:`~ext.commands.Bot` for this. | ||
Hadock-is-ok marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.