Hot shards and rebalancing
One shard does the work
Section titled “One shard does the work”Shards are added so that the work is shared, and the shards are only as useful as the way the work falls on them. A hot shard is one that receives much more than its fair share, so that it saturates while the others are quiet. Adding shards does not help the requests that go to the hot one, and the requests of unrelated keys that happen to live there are slowed down with it, as in the caching module’s lesson on hot keys.
A shard can be hot for four reasons, and they need different cures.
- A burst of neighbouring keys under range placement: new accounts, one day’s orders, one customer’s records. Lesson 2 showed 55% of the traffic on one of four shards.
- An uneven split of the keys themselves, such as a ring with few virtual nodes: one shard owns more keys, so it gets more of the traffic.
- A large tenant or a big customer whose rows share a shard key value and cannot be split.
- A single key that is too popular.
Rebalancing is the cure for the first two and part of the third. It does not cure the last.
Mental model. Rebalancing moves data from busy shards to quiet ones. It can only move what the layout lets it move: a directory can move one key, a range layout can move a range, and a formula or a ring cannot move anything. And it can never take one key below its own share of the traffic.
Try it: rebalancing a directory and a range layout
Section titled “Try it: rebalancing a directory and a range layout”The lab starts with a directory on four shards, and 40% of the requests going to eight neighbouring keys, which sit together on one shard. Hottest shard shows 55% against a fair 25%.
A directory with one busy shard. Press Rebalance, then switch to range and compare how many keys each strategy moves. Add a shard and rebalance again.
What the last change did
Add or remove a shard, change the strategy, the virtual nodes or the traffic, or press Rebalance. This panel says how many keys the change moved.
- Hottest shard
- 55%Shard 2: 2.2× the fair share of 25%
- Keys moved by the last change
- —No change yet
- Keys per shard
- 2,500 most, 2,500 fewest10,000 keys on 4 shards
- Placement metadata
- 10,000 entriesOne entry for each key.
fair share 25% Hottest shard
Keys arriving
A sample of the requests, and the shard that serves each key under the current layout. The chart and the table hold the figures.
- user:7539Shard 4
- user:4900Shard 2
- user:9833Shard 4
- user:3404Shard 2hot key
- user:3406Shard 2hot key
- user:7741Shard 4
- user:4536Shard 2
- user:3405Shard 2hot key
Look up a key
user:1234 is on Shard 1.
| Shard | Keys held | Share of requests | Keys moved in | Keys moved out |
|---|---|---|---|---|
| Shard 1 | 2,500 (25%) | 15% | 0 | 0 |
| Shard 2 · hottest | 2,500 (25%) | 55% | 0 | 0 |
| Shard 3 | 2,500 (25%) | 15% | 0 | 0 |
| Shard 4 | 2,500 (25%) | 15% | 0 | 0 |
Shards are numbered from 1 here. In the C# example the same shards are node-0, node-1 and so on. The keys moved in and out are those of the last change.
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- The lab counts 10,000 keys, user:0 to user:9999, the same keys as the C# example. A key moves when the shard that holds it changes, and every key counts the same, whatever it holds.
- The hash is FNV-1a over the text of the key, and a virtual node sits at the hash of its number, a # and the name of its shard. This is the definition the C# example uses, so the same keys move. It is not a cryptographic hash.
- A shard's load is the share of requests that reach its keys, worked out from how popular each key is. It is what a very long stream converges to, not a measurement of a run. The stream shown beside it is a sample.
- Hot keys are neighbouring numbers, starting at user:3400, like a burst of new ids or one customer's records. The rest of the requests choose any key with equal chance.
Simplifications
- No data is copied and nothing takes time: a resize or a rebalance is instant. In a real system, moving keys copies data, competes with the traffic, and can take hours.
- Range and directory follow simple rules: split the busiest range, move the hottest keys first. Real systems choose when to split and move, and limit how much moves at once.
- FNV-1a puts labels that differ only in their last character close together, so with a few virtual nodes this ring is more lopsided than points placed at random would be. With 100 or more, the two agree more closely.
- Cross-shard queries, replicas and the cost of looking up a directory are not modelled: the lab counts where keys live and what changing that costs.
Parameter values are illustrative. They are not benchmarks of any real product.
Press Rebalance. The directory moves 12 keys, 0.1% of the 10,000, and the hottest shard falls from 55% to 25%. It did not need to move the whole shard: it moved the hottest keys, one at a time, to the shards that were lightest, and stopped when a move would no longer leave the receiving shard lighter than the one it came from. A directory can do this because it can place any key anywhere.
Now switch Strategy to Range and press Rebalance. A range layout can only move ranges, so it re-cuts the boundaries so that each range carries an equal share of the requests. It moves 3,406 keys, 34.1% of the data, and the hottest shard ends at 25.4%. It reaches nearly the same balance as the directory by moving about 280 times as many keys. The price of the directory’s precision is the table of 10,000 entries that you read in lesson 1.
Switch to Hash mod N or Consistent hashing, and press Rebalance: the lab says it cannot. A key lives where its hash puts it. The only levers are the number of shards and, for a ring, the number of virtual nodes. In this scenario the hash already spreads the eight keys, so there is little to cure, but if a hash layout does get hot, the cure is to change what is hashed, which means choosing the shard key again.
Add a shard to a directory and the picture is different. Switch back to Directory, press Add a shard, and the new shard takes no keys: 0 keys moved, because a directory moves nothing until you tell it to. Press Rebalance and it moves 10 keys, and the hottest shard is at 20%, a fair share of five.
What a real move costs
Section titled “What a real move costs”The lab moves keys instantly. A real move copies data from one machine to another while both go on serving requests, and the copy takes time that grows with the amount of data, not with the number of keys. That is why the number of keys moved matters: it is the proxy for the copying you will pay. The Redis Cluster specification describes the shape of it: slots are moved from one node to another while the cluster runs, and during the move, multi-key operations on keys of a slot that is being moved may become unavailable. Moving is part of normal operations, and it has its own failures and its own load.
This is also the reason for the choice of unit. Dynamo’s paper lists, among the benefits of virtual nodes, that a joining node takes a roughly equal amount of load from each of the others, so no single machine carries the whole transfer. A fixed number of hash slots does the same by a different route, with a small table of slots that is easy to hand around.
What happens if one key carries 40%?
Section titled “What happens if one key carries 40%?”Set Hot keys to 1 and Requests to hot keys to 40%, then press Rebalance with Directory, and again with Range. The directory moves 2,499 keys and the range layout 3,401, and both end with the hottest shard at 40%. The lab says so: one key carries 40% of the requests by itself, so no shard can go below that.
One key lives on one shard, and a shard that holds it has at least its share. Rebalancing cannot divide a key. The directory ends with the hot key alone on its shard, and that shard is still the busiest. The remedies work on the key, not on the layout, and each has a price:
- Cache it, so that most of the requests never reach the shard, as in the caching module. The price is staleness.
- Split it: store the value under several keys, such as
product:42#0toproduct:42#3, and let each reader pick one. The shard load falls, and the price is that every write has to reach every copy. - Change the model so that one key does not carry everything, for example by keeping several counters and adding them up.
MongoDB’s guide states the same limit from the other side: a key with a value so frequent that a piece cannot be split becomes a bottleneck. The shard key lesson told you to look at frequency before choosing, and this is the reason.
- A hot shard is one that receives much more than its fair share. Common causes are a burst of neighbouring keys under range placement, an uneven split of the keys, a large tenant, and a single popular key.
- Rebalancing moves data from busy shards to quiet ones. A directory can move single keys: 12 keys (0.1%) take the hottest shard from 55% to 25%. A range layout re-cuts its boundaries: 3,406 keys (34.1%), to 25.4%.
- Hash mod N and a ring cannot move a key, so they cannot rebalance. Their levers are the number of shards, the virtual nodes and the choice of key.
- A directory gives a new shard no keys until you rebalance. Adding a shard and rebalancing moves 10 keys here, and the hottest shard is then at 20%.
- One key with 40% of the requests sets a floor of 40% under any layout. The cures act on the key: cache it, split it, or change the model.
- A real move copies data while serving traffic. The number of keys moved is a proxy for that cost.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.