Sharding

Play this lesson

Too much for one primary

Replicas spread out the reads. But every write still goes to one primary, and one server can only hold and write so much.

Sharding splits the data across several databases, each holding a part of it, called a shard. Each shard has its own primary.

The shard key

The shard key is the field that decides which shard a row lives on. Remy could use garden_id: all of one garden's data lives together.

A good key spreads the load evenly and keeps data that is used together in the same shard.

Quick check

Why should Remy shard by garden ID rather than by plant type?

  1. Everything a match needs is on one shard, so it talks to one database
  2. Plant types are secret
  3. Garden IDs are shorter
Show the answer

Everything a match needs is on one shard, so it talks to one database

A match reads and writes one garden. By garden ID, that is one shard. By plant type, one match would touch many shards.

True story: Discord's messages

In 2017, Discord engineer Stanislav Vishnevskiy wrote that Discord stored more than 120 million messages a day. Messages were split by channel and by time, with the key "((channel_id, bucket), message_id)".

Each bucket held about 10 days of one channel's messages, because partitions over 100 MB caused problems. The key matched how people read: the latest messages in one channel.

Source: Discord: How Discord stores billions of messages, discord.com

Hot shards

A streamer's garden gets 100 times the traffic of any other. Its shard overloads while the rest sit idle. That is a hot shard.

Fixes include splitting the key further, the way Discord added time buckets, so no single piece grows without limit.

Quick check

Once data is sharded, a query across every shard is as cheap as a query on one.

Show the answer

False

A query across all shards asks every database and combines the answers. Choose the key so common queries hit one shard.

Pick the key

Shard Garden's chat: players read the latest messages in their garden. Which key fits best?

  1. Garden ID plus a time bucket, like Discord
  2. The first letter of each message
  3. A random number for every message
Show the answer

Garden ID plus a time bucket, like Discord

Readers want one garden's recent messages, so those should sit together. Time buckets stop a busy garden's chat from growing forever in one place.

Shard last

Sharding is hard to undo and makes every query and migration more complicated. Studios usually reach for indexes, caching and replicas first, and shard when one primary truly cannot keep up.

Bolt says: Next: changing a database while players use it.

Fact-checked 12 October 2026