How it works · 16 of 38 Advanced
What is database sharding?
Splitting a database that's too big
Sharding: Sharding splits one database across several machines by a shard key, a field a router reads to send each row and each query to the machine that holds it; a key that spreads rows and writes evenly, and that most queries include, makes it scale, while a bad key overloads one machine or makes every query ask all of them.
Loading the animation…
Say it like this Sharding splits one big database across several machines by a shard key; pick a key that spreads rows and traffic evenly and that most queries include, or one machine does all the work and other queries must ask every machine.
Under the hood — for builders
- The source's own example: sharding on continent
- MongoDB: a continent key has "a cardinality of 7", so "adding more than seven shards would not provide any benefit":
- Shard and shard key
- Shard: one machine holding part of the data. Shard key: the field that decides where each row goes.
- Chunk
- A range of key values kept together on one shard; one key value can't be split.
- Watch out: ever-increasing keys
- MongoDB: with an always-increasing key, "all new inserts" go to one chunk: one hot shard.
- Hashed vs ranged
- Hashed spreads writes evenly but scatters range queries; ranged keeps neighbours together.
- Before you shard
- Scale up, add read replicas and indexes, archive old rows. Shard when one machine can't keep up.
- Sources
- MongoDB Manual: "Sharding", "Choose a Shard Key" (read November 2026).
What happens in the sharding animation, step by step?
- The loans database has reached 20 TB. A bigger machine helps until there is no bigger machine: cloud providers cap how much CPU, memory and disk one server can have.
- Sharding: split the data across several machines, each holding a part. A router reads one field of every row, the shard key, to decide which machine it goes to.
- Your turn: 40 customers are waiting to be stored. Pick the shard key and drop them in.
- Sharding by surname letter, the S–Z rack spills over (Sharma, Singh, Shah…) while the rest sit half empty. Hash the customer id instead, and each rack gets about a quarter.
- A key also decides which questions are cheap. "Balance of customer 7731?" goes to one rack. "Everyone in Pune?" has no shard key in it, so the router must ask all four.
- Each rack also has a replica, and copies update a moment apart. What if a reader hits the copy that hasn't caught up yet? Next: consistency and CAP.
What everyday example explains sharding?
A bank branch that splits customers between four counters by surname. It works until the S counter has a queue out of the door (Sharma, Singh, Shah…) while M–R sits idle. Hand out counter numbers by shuffling account numbers instead and the queues even out, but "everyone from Pune, please" now means calling out at all four counters.
Why should a team care about sharding?
Shard only when one machine can no longer hold the data or the writes, after indexes, replicas and archiving; then choose the key from your most common queries (often a customer or tenant id, hashed), check how its values are spread, and avoid dates and auto-increment ids.