Skip to content

Hot keys and uneven shards

Deep diveConcept

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:

Placing eight keys on four shards
Step 1 of 9
Busiest shard 0
Key → shard
Empty
Requests a second on shards 0–3 (example)
00
01
02
03
  • 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.

20k req/s shard limit

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.

Two kinds of hot key
Hot forExamplesWhat spreads it
ReadsA count on a viral post, a product page, a feature configCopies: a short cache in each app server, or read replicas
WritesLikes on that post, stock of a flash-sale item, one big customer’s rate limitSplitting the key into pieces, or adding up writes in memory and sending one batch a second
BothA live score that millions watch while it changesSplit 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.

Writing and reading a counter split three ways
Step 1 of 7
Pieces of likes:post:42
#0 on A3000
#1 on B3000
#2 on C3000
Requests for one hot counter
AppShard AShard BShard CNothing sent yet
  • 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.

9,000 true total

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.

hot.py
import zlib
SHARDS = 4
def shard_of(key):
return zlib.crc32(key.encode()) % SHARDS
# Requests a second per key: 999 ordinary posts and one celebrity post.
load = {f"post:{i}": 70 for i in range(1000)}
load["post:42"] = 30_000
def per_shard(load):
totals = [0] * SHARDS
for key, rps in load.items():
totals[shard_of(key)] += rps
return totals
Shell
$ python3 hot.py
keys per shard [250, 250, 250, 250]
post:42 lives on shard 3
load, one key [17500, 17500, 17500, 47430]
sub-keys land on [3, 1, 3, 1, 2, 0, 2, 0]
load, 8 pieces [25000, 25000, 25000, 24930]

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.

Every read goes to the shard1,000,000 a secondThe shard that owns the key takes all of it. Read replicas divide it, but only by the number of replicas.
1-second copy in each of 200 serversabout 200 a secondEach server refreshes once a second. The price: a count can be up to a second old.

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:

cache.ts
const cache = new Map<string, { value: number; until: number }>();
const inFlight = new Map<string, Promise<number>>();
// Serve a hot key from memory for up to 1 second. When the copy is stale,
// only the first caller fetches it; the rest wait for that same promise.
export async function getCached(key: string): Promise<number> {
const hit = cache.get(key);
if (hit && hit.until > Date.now()) return hit.value;
let pending = inFlight.get(key);
if (!pending) {
pending = fetchFromShard(key)
.then((value) => {
cache.set(key, { value, until: Date.now() + 1000 });
return value;
})
.finally(() => inFlight.delete(key));

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 MONITOR on a busy Redis: it streams every command and can slow the server noticeably.
Shell
$ redis-cli --hotkeys
...
-------- summary -------
Sampled 100 keys in the keyspace!
hot key found with counter: 71 keyname: "post:42"
hot key found with counter: 15 keyname: "post:7"
hot key found with counter: 5 keyname: "post:97"

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.
Splitting every keyReads become N times dearer for millions of keys that were never hot. Split only keys you have measured as hot, and store the split count where readers can find it.
Moving the key moves the heatA bigger machine, a new shard or a rebalance relocates the hot key. It is still one key on one machine.
Every server refreshes at onceIf all app servers cache with the same TTL from the same moment, they refetch together. Add a little random jitter to each expiry.
Splitting a value that must stay singleStock, balances and unique names cannot be summed from pieces. Give each piece its own share, or keep one owner and queue the writes to it.

08 Check yourself

Question

A cluster has 8 shards and one key takes 40% of all writes. You grow to 16 shards. What happens to the load on the shard holding that key?

Question

You split likes:post:42 into 8 pieces. What became more expensive?

Question

200 app servers each keep a hot count for 1 second, and only the first miss on a server fetches it. Roughly how many reads a second reach the shard for that key?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?