Lesson 8 of 9 · 26m
Sharding, Guild Affinity, and Horizontal Scale
Understand Discord's shard assignment model, process ownership, cross-shard state, and the operational traps that appear as guild count grows.
Sharding is not a performance optimization you add when the bot feels slow. It is Discord's partitioning model for gateway scale. Once your bot grows past Discord's single-process thresholds, every guild belongs to a shard whether you planned for it or not. You can either design around that ownership model or spend a year debugging duplicate consumers, inconsistent caches, and commands answered by the wrong process.
Guilds are assigned deterministically
Discord documents shard assignment approximately as:
shard_id = (guild_id >> 22) % number_of_shardsThis means guild affinity is stable for a fixed shard count. Change the shard count and affinity changes across the fleet. That is not merely a restart detail; any local cache, in-memory queue, or shard-local workflow tied to guild ownership must survive resharding or move to shared storage.
A shard is a unit of ownership, not a pod label
For each shard, exactly one active gateway session should own the event stream. If two replicas connect the same shard, you can get flapping sessions, duplicated processing, or identify churn. A production layout often looks like:
shard manager
assigns shard ranges, observes sessions, handles replacement/failover
shard worker 0: shards 0-1
shard worker 1: shards 2-3
interaction workers + action executors: command processing and Discord RESTCross-shard state does not live in memory
A moderation case can start with a command in one guild, trigger lookup of a user in another guild, enqueue an action, and produce a dashboard update. If the workflow assumes every object is in the local shard cache, it will fail unpredictably. Use shared stores for durable state and explicit fetch paths for data outside local affinity:
Redis/Postgres: workflow state, guild settings, quota counters
local cache: hot guild/member/channel lookups owned by this shard
REST fetch: authoritative object when cache is insufficientScaling shard count is an operational migration
When increasing shard count, roll deliberately:
1. calculate old and new guild assignments
2. drain workflows tied to shard-local state
3. start workers with the new shard count in controlled batches
4. observe identify concurrency and event lag
5. remove old owners only after new ownership is stableDiscord gives large bots an identify concurrency bucket. Respect it. Restarting hundreds of shards at once is not confidence; it is an outage generator.
Broadcast is the hidden cost
At scale, the expensive event is not necessarily the command. It is the fanout: member updates, message creates, reaction events, presence if you foolishly enabled it, and cache maintenance. Measure events per second per shard and downstream writes per event. Horizontal scaling does not help if every shard writes the same unbounded data into one database.
Field notes
- Shard count is not the same as worker count. One worker can own multiple shards; one shard should not be owned by multiple active workers.
- Localhost-only communication patterns break as soon as shard ownership moves. Use explicit message buses or RPC.
- Resharding invalidates simple assumptions about stable local memory even for guilds that still exist.
- Do not enable presence or member-intensive features just because sharding already feels expensive; measure first.
Was this lesson helpful?