The whole course ▾
System Design · Lesson 12 of 39 · Week 3
Sharding: splitting data across machines
QuickBite's orders no longer fit on one machine, and replicas can't help: every copy is just as big. Today: sharding.
Got a doubt? Ask in the comments under the video.Subscribe
QuickBite's orders table outgrows one machine.
QuickBite is the food delivery app we grow through the course, from one server to millions of orders.
Too big for one machine
Yesterday, read replicas spread the reads. But every follower holds a full copy of the data, and the data keeps growing.
QuickBite now runs in forty cities and takes twenty million orders a day. With their items, status changes and indexes, each order takes about five kilobytes. The app servers send all of it to the orders DB: a hundred gigabytes of new data a day, more than thirty terabytes a year.
Here's where it hurts. The disks fill up, a backup takes a whole day, and the busy part of the data no longer fits in memory, so queries slow down. Followers can't fix any of that: each one is a full copy, just as big. And every write, thousands a second at peak, still goes through a single leader. We've hit the ceiling of one machine.
Split it into shards
So stop putting all the data in one place. Split it.
Sharding splits one big table across several databases. Each one is called a shard, and holds only its slice of the orders: a third each, then a tenth, then a hundredth as QuickBite grows. Every shard is an ordinary database with its own leader and followers, exactly like yesterday. And a shard router, often just a small library inside the app, decides which shard holds which data. Each shard can live on its own machine, so adding machines finally adds capacity for writes too.
In one line: each shard holds a slice of the data, and one field, the partition key, decides which slice a row belongs to. Writes now spread across many leaders, and each shard is small enough to back up, copy and keep in memory. So the whole design hangs on one question: which field?
Choosing the partition key
Choose the partition key the way you chose indexes: from the questions the app asks most.
A good partition key does three things. The busiest queries include it, so each one goes to exactly one shard. It spreads data and traffic evenly, so no shard gets more than its share. And it keeps rows that are read together on the same shard, so one request means one shard.
For QuickBite's orders, that's the customer ID. The history tab, the busiest query, always knows the customer. A customer's orders all sit on one shard, so their history is a single, fast query. And with millions of customers, the slices come out even. Compare a bad choice: order time. Every new order would land on the shard that holds today, one shard melting while the rest hold old history. Remember the good choice, because the next question is how a customer ID turns into a shard.
Range or hash
There are two ways to map a key to a shard, and they fail in opposite ways.
Range partitioning gives each shard a range of values: customers one to a million on shard one, the next million on shard two. It's simple, and range queries are fast. But new customers get the highest IDs, so every signup, and most new orders, land on the last shard while the others sit idle. Hash partitioning runs the key through a hash function, which scrambles it, and the result picks the shard. Neighbours end up far apart, so the load spreads evenly, new customers included.
The trade-off: hash spreads the load evenly, but a range query, like every order from the last hour, now has to ask every shard. Range keeps things in order, but piles new data onto one shard. QuickBite reads orders by customer, not by range, so it hashes the customer ID.
What gets harder
Sharding solves the size problem, and hands you a few new ones. Here's the first, on the board.
A restaurant app opens its dashboard: tonight's orders. But orders are split by customer, so one restaurant's orders are spread across every shard. The router has to ask all of them and merge the answers, which is called scatter and gather. Fine for three shards; slow and fragile for a hundred. The usual fix is a second copy of the orders, split by restaurant, kept up to date from the same change log.
So, the new problems. Queries without the partition key must ask every shard. Hot shards appear when one key is far busier than the rest, like a viral restaurant if you'd split by restaurant. Joins and transactions across shards get hard, so you design to avoid them. And resharding, adding shards as you grow, means moving data while orders keep arriving. With hash mod N, going from four shards to five moves about eighty percent of the keys. In practice, you copy data to the new shards in the background, then switch reads and writes over, a slice at a time.
In the interview
Sharding is where interviewers check whether you scale with a reason, or just because big systems do.
Say it with the key and the reason together: shard orders by a hash of the customer ID, so the load is even and each customer's queries hit one shard. Then name the query that doesn't fit, the restaurant dashboard, and how you'd serve it. That's the part most candidates miss.
Then expect these. Why not one bigger machine? You can, for a while, as lesson five said, but it has a ceiling and a steep price. How do restaurants see their orders? A second copy split by restaurant. One shard is overloaded: now what? Find the hot key, and split it or give it its own shard. And how do you add a shard without moving most of the data? That's tomorrow's lesson.
The common mistake is sharding too early. It makes every query, every migration and every transaction harder. Use indexes, caches and replicas first, and shard when the numbers say one machine can't cope. Say those numbers out loud.
Quick check
Orders are sharded by customer ID. Which common query gets slow?
Show the answer
Restaurant queries, like tonight's orders: they must ask every shard.
Keys go to shards by hash mod 4. You add a fifth shard. Roughly how many keys move?
Show the answer
About 80%: only 1 key in 5 stays where it was.
Remember: shard only when one machine can't cope. The partition key decides everything, so pick it from your busiest queries. And hash partitioning spreads load evenly, while range keeps order. Tomorrow, the fix for that eighty percent.
Lessons on weekdays, the week's revision on Saturday, a mock interview on Sunday.