Hot keys and uneven shards
You hash-sharded your data and the keys are spread perfectly, yet one shard is at its limit while the rest are idle. One key is getting a big share of the traffic, and a key lives on exactly one shard. Here is how to see it, find it, split it and cache it.
01 Even keys, uneven load
Hash sharding gives each shard about the same number of keys. It does not give them the same number of requests. Keys are not equally popular: a celebrity’s post, a product in a flash sale, a viral video or the one config key every request reads can each take a large share of all traffic.
All requests for one key go to the one shard that owns it. That is the routing working as designed, and it is exactly the problem. Watch eight posts land on four shards:
- just placed
- over its limit
Four shards, each able to serve about 20,000 requests a second (example numbers). Keys go to crc32(key) % 4. Eight posts are about to be placed; seven get 2,000 requests a second each.
Adding shards does not help. With eight shards, post:42 still lives on one of them and still needs 30,000 a second; the extra machines only take load off shards that were fine. Resharding moves the hot key to a new home, and the heat moves with it. The fix has to change the key, or keep requests away from it.
02 Hot for reads, or hot for writes?
The first question is what the traffic does. Reads can be served from copies, so they can be spread anywhere. Writes must reach the key’s owner, so the key itself has to change.
| Hot for | Examples | What spreads it |
|---|---|---|
| Reads | A count on a viral post, a product page, a feature config | Copies: a short cache in each app server, or read replicas |
| Writes | Likes on that post, stock of a flash-sale item, one big customer’s rate limit | Splitting the key into pieces, or adding up writes in memory and sending one batch a second |
| Both | A live score that millions watch while it changes | Split the writes, cache the total |
Managed databases hit the same wall. In DynamoDB, for example, AWS documents a limit per partition of about 3,000 read units and 1,000 write units a second, and its adaptive capacity can split a busy partition, but never one key: a single item always lives in one partition.
03 Split a hot counter into pieces
To spread writes, turn one key into several: likes:post:42#0, #1, #2. Each write picks one piece, so each piece gets a share of the writes, and the pieces hash to different shards. A read fetches every piece and adds them up.
- just written
- read for the total
- added to the total
The count for post:42 was 9,000, now kept as three pieces of 3,000, one on each shard (here the router puts them on three different shards). Likes are about to arrive.
This works for a counter because adding up is order-free: it does not matter which piece took which like. It does not work for a value that must be read and changed as one, such as the last item in stock (two pieces could each sell it) or a unique username. Those need each piece to own its own share, for example 100 units held as 4 pieces of 25, refilled from the others when one runs out.
04 Measure it: one key against eight pieces
Here is the same idea at a larger size: 1,000 posts on 4 shards with crc32, 999 of them at 70 requests a second and post:42 at 30,000. Then the hot key is split into eight pieces.
That is a real run. The keys could not be spread more evenly, 250 on each shard, yet shard 3 serves 2.7 times what the others do. After the split every shard is within 70 requests a second of the rest. Here the eight pieces happened to land two per shard. Hashing does not promise that: with only eight pieces, three can land on one shard. Use a few times more pieces than shards, or place them yourself.
05 Cache a read-hot key next to the reader
For reads, the cheapest fix is to stop asking the shard. Say a count is read 1,000,000 times a second across 200 app servers. If each server keeps the value in its own memory for one second, the shard sees at most one read per server per second.
One more detail decides whether this holds up. When the copy expires, every request on that server misses at the same moment, and each one would fetch the key: a stampede. Let only the first miss fetch, and make the others wait for its answer:
In a test run on Node 24, 1,000 calls made at the same moment led to one fetch from the shard. A shared cache such as Redis in front of the database helps too, but a Redis key is itself one key on one node, so a key that hot still needs the copy in each app server.
06 Finding the hot key
The symptom is one shard with high CPU, slow replies or throttling errors while the rest are quiet. The cause is a key, and dashboards per shard will not name it. Ways to find it:
- Count keys in the client or proxy. Sampling one request in a hundred and keeping the top few keys is cheap and points straight at the culprit.
- Use the store’s own tools. DynamoDB’s CloudWatch Contributor Insights lists the most-used keys; Redis has
redis-cli --hotkeys. - Avoid
MONITORon a busy Redis: it streams every command and can slow the server noticeably.
That is a real run on Redis 8.4 after 20,000 reads of post:42 and 300 of post:7. The counters are not request counts. They are the logarithmic LFU counters Redis keeps per key (a new key starts at 5), so --hotkeys only works when maxmemory-policy is an LFU one, and it walks the whole keyspace with SCAN, so run it on a replica.
07 When hot keys bite
INCR {post:42}#3Pieces that share a hash tagIn Redis Cluster, only the part inside {} is hashed, so every piece lands in the same slot on the same node. Leave the braces out of split keys.MGET likes:post:42#0 likes:post:42#1One MGET across piecesRedis Cluster rejects a multi-key command whose keys sit in different slots, with a CROSSSLOT error. Send one GET per piece, or one pipeline per node.