Courses System design · Part 5 · Lesson 6
Sharding: Splitting One Database into Many
Replication makes several copies of each piece of data. That spreads the reads and protects against failure, but all writes still go to one leader, and all data must still fit on every server. When either reaches its limit, we split the data. Each part is called a shard or a partition.
When is it needed?
Calculate with the numbers from lesson 2:
- Size: a shop has 2 million orders a day, 2 KB each: 4 GB a day, about 1.5 TB a year. A good server has a few terabytes of fast disk, so in a few years it is full.
- Writes: 2 million a day is about 23 writes a second, perhaps 200 in the busy hour. That is little for one database.
The result: this shop does not need sharding yet; it only needs to move old data to cheaper storage. Sharding adds a lot of complexity, so do not start it until the numbers force you. First try indexes, caches and read replicas.
Three ways to split
1. By range
The data is split by the value of a key: users A to F on shard 1, G to M on shard 2, and so on. Or each month’s orders on one shard.
- Good: a range query (“orders from January”) goes to one shard only.
- Bad: the load can be uneven. Split by date, and all of today’s writes land on one shard.
2. By hash
The key is hashed, and the result picks the shard. The hash sends similar keys far apart, so the load spreads evenly. So that adding a shard does not move all the data, consistent hashing is used.
- Good: even load.
- Bad: a range query must ask every shard.
3. By directory
A small table says where each key is: “shop 42 → shard 3”. It is fully flexible: you can move one big customer to a shard of its own. But the table itself must be fast, always available and always correct.
The most important decision: the shard key
The shard key is the column that decides where each row goes. A bad choice is hard to change later. Ask three questions:
- What do most queries filter on? If you almost always want “the orders of user X”,
user_idis a good key: all of one user’s data is on one shard, and each query goes to one shard. - Are there enough different values? A key such as “country” has only a few values and cannot spread data over a hundred shards.
- Is there one very busy value? That is the hot key problem.
The hot key problem
Say a social network is sharded by user_id. One famous person has 20 million followers. Every post gets millions of likes and comments, and they all go to one shard. That shard is overloaded while the others are idle.
Common fixes:
- Split the hot key: add a small random number to the key, for example
post-981#0topost-981#9, so the writes spread over 10 shards. When reading, add the 10 parts together. - A cache in front: for hot reads, a cache takes most of the load.
- A queue for writes: put likes in a queue and write them in batches.
What sharding costs
Sharding makes simple things hard:
- Joins across shards: if orders and users are on different shards, the database can no longer join them for you. The application must read from several shards and combine the results itself.
- Transactions across shards: a money transfer between two users on two shards is not one simple transaction. You need a distributed transaction (slow and complex), or a design where this is rare.
- IDs:
AUTO_INCREMENTcounts separately on each shard, and IDs repeat. You need distributed unique IDs. - Moving data: when you add a shard, you must move data without stopping the service.
In an interview
- First show why it is needed: with numbers, not with a feeling.
- Choose the shard key out loud, and justify it with the main queries.
- Say one sentence about hot keys, and one about joins and transactions.
Summary
- Sharding splits data over several servers when the size or the writes outgrow one server.
- Range is good for range queries, hash for even load, a directory for flexibility.
- The shard key is the most important decision; watch for hot keys and the cost of joins and transactions.