Zeeshan Adil
Book a call
← All concepts

CONCEPT

What is a hot key?

A hot key is a single key that receives a large share of all traffic. Hashing spreads different keys across servers, never one key, so every request for it lands on one server, and adding servers does not help. Fixes are splitting the key, caching reads, or giving it dedicated capacity.

Also called: hot partition, hot spot. By Zeeshan Adil··Databases·Distributed Systems·Performance

A bar chart of the busiest database's share of one viral post's writes on 8 databases: 100% with 1, 2 or 4 stripes, then falling to 50%, 25%, 31%, 28% and 25% as the stripe count grows to 128.

The problem it solves

Sharding assumes load follows the keys, and that keys are about equally busy. When one post goes viral, that assumption breaks. A hash function is deterministic: crc32("15001") is the same number today and tomorrow, so that post has exactly one owner. Balanced hashing spreads different keys evenly. It cannot spread one key, because one key is one hash, and one hash is one database.

How it works

Three fixes exist, and they combine.

Split the key. Make the routing key longer than the id and pick a suffix per write. Instead of hashing 15001, hash 15001:0 to 15001:31, choosing the suffix from the comment id. Different strings hash to different places. This is called key salting or striping, and DynamoDB documents it as write sharding.

Code
route = f"{post_id}:{comment_id % stripes}"  # was: str(post_id)

Cache the reads. A viral post is usually read far more than it is written. A cache absorbs most reads, which leaves striping for the write path.

Give it dedicated capacity. A known hot key can be moved to its own server.

What it does not do

  • Splitting makes reads harder. A read must ask every server that holds a stripe and merge the results by id or time. Counters must be summed across stripes.
  • You have to know which keys are hot. Either detect them or let the application decide. Stripe only the hot ones, so reads of other keys stay one lookup.
  • Old data stays put. Stripes apply to new writes. Earlier rows remain on the original owner.
  • Range-ordered keys cannot be striped by suffix, because they are ordered by the id itself.
  • More servers and more virtual nodes change nothing for a single key.

Measured

In my sharding challenge, 200,000 comments were written to one post.

DatabasesBusiest database's share, 1 keyBusiest database's share, 32 stripes
2100.0%50.0%
8100.0%31.2%
32100.0%34.4%

With one key, 32 databases was exactly as bad as 2. On 8 databases, 8 stripes still reached only 2 databases, because strings that differ in one character hash close together under CRC32. 64 stripes reached all 8.

On real shards, with 3,000 comments on one post and 4 databases, 1 stripe put all 3,000 on one database, while 32 stripes spread them as 1,031, 751, 468 and 750. All 3,000 read back, in order, with no duplicates, but the read asked all 4 databases instead of 1.

RUN IT YOURSELF

Challenge · Distributed Systems ChallengesA popular post takes down the database
What is consistent hashing?What are virtual nodes?What is request coalescing?

SOURCES

Get the next deep dive by email

New deep dives on how production systems really behave, from Kubernetes networking to PostgreSQL internals. About one email a month.

You will get one email to confirm. Unsubscribe from any email with one click. Your address is used for this newsletter and nothing else.

Written by Zeeshan Adil

I run systems like this for a living. If your team is fighting the same problem, or wants this as a talk, write to me.