Hot keys and hot partitions

Performance and scaling Senior Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Predict the output

A like counter is split over 4 sub-keys. What's the total?

Python 3.13
sub_counts = {
    "likes#0": 250, "likes#1": 248,
    "likes#2": 255, "likes#3": 247,
}
print(sum(sub_counts.values()))

Options
Hint 1 of 1

Add the four values.

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A 1000

Correct. Reads sum the sub-keys; writes spread over 4 places.

Splitting one hot counter into N sub-keys spreads its writes across N places. Reading the total costs a sum of N values, which here gives 1,000.

Why not the others?
  • B 255. That's one sub-key.
  • C 250. Sum all four.
  • D 4. That's the number of sub-keys.

30-second answer

A hot key defeats even partitioning, because all its traffic hashes to one place. For reads, cache it close to callers (in-process or replicated cache entries with a suffix such as key#1..key#8), and collapse duplicate requests. For writes, split the key: write to N sub-keys (counter#0..counter#7) and sum on read, or buffer and batch updates. Detect hot keys with per-key metrics or sampling before they hurt.

Practise saying it · 30 s. No recording, just a timer

Handle a hot key.

Say your answer out loud, then check which of these points you covered:

  • Hashing can't split one key
  • Cache and coalesce reads
  • Split writes into sub-keys
  • Detect per key

Deep dive

DynamoDB adaptive capacity helps but per-partition limits still apply.

Redis cluster: a hot key lives on one node; replicas or client-side caching spread reads.

Write sharding trades read cost (sum N keys) for write throughput.

Kafka: a hot message key fills one partition; add a suffix if strict per-key order isn't needed.

Common mistakes

  • Adding shards, which doesn't split one key.
  • Rate-limiting the popular account's fans as the fix.
  • No per-key visibility.

Senior take & follow-ups

Explain why hashing doesn't help, then read replicas/caching and write splitting.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why don't more shards help?

    Show hint

    One key still maps to one shard.

  2. Cost of write sharding?

    Show hint

    Reads must gather and sum the sub-keys.

Suggest a fix if something here is wrong or out of date.