Splitting the data
After this: Place rows on shards by a key, and count what moves when a shard is added.
On this page · 5 sections · 3 checks
Replicas share the reading. But every replica holds all the data and applies every write. What if the writes are too many for one primary, or the data no longer fits on one machine? Then no copy can hold everything. The data itself has to be split.
Split the rows
Give each machine some of the rows instead of all of them. Each part, and the machine that holds it, is a . Each row lives on exactly one shard, so each shard stores only its part and takes only the writes for its rows.
The app needs a rule that says which shard holds a row. The rule reads one column of the row, the . A common rule takes the remainder after dividing the key by the number of shards. In code that is %: 7 % 4 = 3, because 7 = 1 × 4 + 3.
A worked example
Four shards, and the rule shard = user id % 4. Users 1 to 10 sign up.
- User 1: 1 % 4 = 1, so Shard 1. Users 2 and 3 go to Shards 2 and 3.
- User 4: 4 % 4 = 0, so Shard 0.
- Users 5 to 8 go round again: Shards 1, 2, 3, 0. Users 9 and 10 go to Shards 1 and 2.
| Shard | Users |
|---|---|
| 0 | 4, 8 |
| 1 | 1, 5, 9 |
| 2 | 2, 6, 10 |
| 3 | 3, 7 |
- Shard: 10 keys over 4 shardsPress Run it and step through. For each user, work out the remainder after dividing the id by 4 before the step shows it. At the end, watch what a count of every user has to do.
Press Run it, then step through and watch the requests move.
To find user 9, the app computes 9 % 4 = 1 and asks Shard 1 alone. Inside that shard, an index on the user id still turns a search through every row into a quick lookup, as in Indexes: why queries are fast or slow. A shard is an ordinary database with fewer rows.
A query without the shard key is different. "Count every user" has no user id to compute with, so the app asks all 4 shards and adds their answers: 2 + 3 + 3 + 2 = 10. Every such query costs work on every shard.
Adding a shard moves most rows
The shop grows and adds a fifth shard. The rule becomes user id % 5. User 7 was on 7 % 4 = 3 and now belongs on 7 % 5 = 2, so its rows must be copied across. Of users 1 to 20, only 1, 2, 3 and 20 give the same answer under both rules: 16 of 20 must move.
The program below counts the moves for more users.
shard_forshardFor is the rule. count_movedcountMoved goes through every user and counts those whose shard is different under the new number. Going from 4 to 5 shards moves 80,000 of 100,000 users: 80%. Doubling from 4 to 8 moves 50,000, which is half. Either way, a large share of the data moves while the app keeps running.
A scheme called consistent hashing places keys so that going from 4 to 5 shards moves only about one key in five on average: roughly the share the new shard should hold. The exact share depends on how the keys happen to fall. This tutorial names it and does not teach it.
Busy shards
Splitting only helps when the load splits too. Suppose the shard key were the country, and most users lived in one country. One shard would hold most of the rows and take most of the requests, while the others sat almost idle. A shard that gets far more than its share is a . Choose a shard key that spreads both the rows and the requests evenly, such as the user id. Even then, one very busy key, such as a celebrity's account, can make its shard hot.
Check yourself
Users 1 to 10 are on four shards by user id % 4, as in the picture. Users 11 to 16 sign up. Which users does Shard 3 hold now?
The app places users with shard_forshardFor, which is user id % shards. It has 100,000 users on 4 shards and adds a fifth. About what share of the users must move to a different shard?
An orders table is split over four shards by user id % 4. Which of these queries must ask every shard?
- Split a table into shards by a shard key, so each machine stores and writes only its part.
- Place a row with key % shards: with 4 shards, users 4 and 8 go to shard 0, and user 9 to shard 1.
- Say which queries ask one shard (they include the shard key) and which must ask every shard (they do not).
- Count what moves when a shard is added: from 4 to 5 shards, 16 of users 1 to 20, and 80% of a large set.
- Spot a hot shard: a key such as the country can put most of the rows and requests on one machine.