03. Data & Storage / Lesson 5 of 11
Sharding
Split one huge database across many machines, and pick how to split it.
- 10 min
- Intermediate
- 10 steps
- Shards1 (warning)
- Rows per shard1.6B (bad)
- Hottest shard100% (bad)
- Request
- Write
- Health check
Step 1 of 10: The table no longer fits on one machine
Read every step as text
Step 1: The table no longer fits on one machine
Our shop has an orders table of 1.6B rows, about 320 GB at 200 bytes a row. One server handles up to about 500M rows comfortably, so we are at 320% of that.
Indexes no longer fit in memory, backups take hours and every query is slower. A bigger machine (scaling up) only delays this, and replicas do not help, because each replica holds all the data.
Replication copies the same data. Sharding splits different data across machines.
Step 2: Sharding: each machine holds a slice of the rows
Sharding (horizontal partitioning) puts different rows on different servers, called shards. With 4 shards each holds 400M rows, 80% of the comfortable limit.
A router (or a library in the app) looks at a column, the shard key, and sends the query to the right shard. Every shard can also be replicated on its own.
Now both the data and the writes are spread out. This is how a database scales past one machine's writes.
Step 3: Range sharding: each shard owns a range of keys
In range sharding each shard owns a continuous range of the shard key. Here the key is the order date: shard 1 has 2023, shard 2 has 2024, shard 3 has 2025 and shard 4 has 2026. Say each year has about 400 million orders.
It is easy to understand, and a query for "orders in March 2025" touches only one shard. Ranges can be split further when a shard grows.
Range queries stay on one or a few shards, because neighbouring keys live together.
Step 4: Range sharding on time makes a hot shard
New orders are always written in the newest range, and people mostly look at recent orders. Say 85% of all traffic goes to shard 4 (2026), and the other three share the remaining 15%.
Three servers sit nearly idle while one is overloaded, so adding shards does not add capacity where it is needed. A good shard key spreads the traffic, not only the rows.
Any key that grows steadily (time, an auto-increment id) sends all new writes to the last range.
Step 5: Hash sharding: spread keys by their hash
Instead of the date, shard by customer id:
shard = hash(customer_id) % 4. A hash scrambles the key, so customers land on all shards evenly, and each shard gets about 25% of the traffic.All orders of one customer share a shard, so "my orders" is a single-shard query. Heavy single customers can still be hot, but that is much rarer than a hot year.
Pick a key with many different values that are used about equally often.
Step 6: Hashing scatters neighbouring keys
The price of hashing: customers 100, 101 and 102 are on different shards, and so are all the orders from January. A query such as "all orders from January" must ask every shard and then merge the answers (scatter-gather).
With date ranges it touched one shard; with hash sharding it touches all 4. Many systems keep a second copy of the data, sorted by date, for such reports.
Range sharding is good for range scans and bad for hot spots; hash sharding is the opposite. Choose by your main queries.
Step 7: A directory: look up where each key lives
With directory-based sharding a small lookup table says which shard holds each customer. Nothing is computed from the key, so any customer can be moved to any shard, for example a giant shop onto a shard of its own.
The cost is one more lookup on every query (cache it) and a directory that must be small, fast and highly available, or it becomes the single point of failure.
A directory gives full control over placement. Many systems use it for a few special keys and hashing for the rest.
Step 8: Queries that do not use the shard key
"Find the order by customer email" does not say which shard to ask, so the router asks all 4 and waits for the slowest. Say one shard in a hundred is slow at any moment: with 4 shards the chance that at least one is slow is 4%, with 40 shards it is 33%.
Joins across shards and transactions that change rows on two shards are worse: they need extra coordination (such as two-phase commit) and are slow. Design so that the common queries and transactions stay inside one shard.
Keep things that are used together on the same shard, for example all of a customer's orders and addresses.
Step 9: Adding a shard moves most of the data
Shards fill up, so one day we go from 4 to 5. With
hash % N, a row stays only ifhash % 4equalshash % 5, which is 4 of every 20 values, 20%. So 80% of the rows move: 1.28B rows, about 256 GB. At 100 MB a second that is roughly 43 minutes of copying, while the site stays live.The fix is to hash into many fixed buckets (say 1,024) and map buckets to servers, or use consistent hashing. A new shard then just takes a fifth of the buckets: 320M rows, 64 GB, about 11 minutes.
Plan for growth from day one: start with many small logical shards on few machines, so you can move whole shards later.
Step 10: Sharding in one picture
Sharding splits rows across servers, so 1.6B rows become 400M per shard on 4 shards. The shard key decides everything: range keys keep ranges together but make hot spots, hash keys spread the load but scatter ranges.
Keep common queries inside one shard, expect to ask all shards for the rest, and plan how to add shards before you need to.