Lesson 04

Hot Partitions

Building block

A partition count is computed for an even spread, and real traffic is not even. One key, one range or one moment can send a share of the load to one partition that no count set in advance absorbs.

A partition takes a fixed number of writes a second, and a key that needs five times that is not helped by adding partitions, only by changing where its load goes. There are three shapes of heat, each with its own fix, and moving data while it is under load is the fourth thing an interview asks about.

Three Shapes of Heat

One hot key: a celebrity's post, or a viral link. A hot range: many popular keys that share one slice. A moving hot spot: a time-ordered key whose newest slice takes every write. Each takes a different fix, so the first move is to say which one it is.

Hot keys in the cache tier, and the request coalescing and local tier that fix them, belong to Chapter 2. This lesson is about the partition in the store.

What is hot, and the fix
One key, hot for reads→The cache tier and a local copy on each server
One key, hot for writes→Salt it, or buffer its writes
A range of many hot keys→Split the range
The newest slice of a time-ordered key→A bucket prefix on the key

A Read-Hot Key

Take Chapter 7's viral link: one link at 50,000 reads a second. One partition takes 3,000 reads a second, so that link is about seventeen times over. No partition scheme spreads one key's reads, because the key lives in exactly one place.

So those reads have to stop before the store: in the cache tier, and in a short-lived copy on each application server, both from Chapter 2. Replicas of the partition help only as far as the read may be stale.

A Write-Hot Key

Assume one post's like counter takes 5,000 increments a second. One partition takes 1,000 writes a second, so the counter is five times over.

Salting splits it into ten sub-keys: the key plus a suffix from 0 to 9. Each sub-key takes about 500 increments a second, and the sub-keys hash independently, so two may share a partition and fill it to its limit. A read now sums ten keys.

Buffering is the other fix. Assume 50 application servers, each adding up its own increments and writing the total once a second. That is 50 writes a second for the key. It costs a count that runs a second behind, and the buffered increments of any server that crashes.

Salt when every increment must be durable at once. Buffer when a second of lag is fine. Either way, apply the fix only to the keys that are hot, known in advance like a celebrity's account or measured by counting requests per key, because salting every key turns every read into a gather.

One counter at 5,000 increments a second
One keybefore salting
One partition takes all 5,000, five times its limit of 1,000. A read is one key.
Ten sub-keysthe key plus 0 to 9
About 500 a second on each sub-key, half a partition's limit; two that share a partition fill it. A read sums ten keys.

A Moving Hot Spot

With ranges over a timestamp, all 20,000 writes a second of the first lesson's workload reach the newest slice. That is twenty times one partition's limit, while the older slices sit idle.

Prefix the key with a bucket: one of 40, picked by a hash of the record's ID. Each bucket then takes about 500 writes a second. The cost lands on reads by time: a query for the last hour asks 40 partitions and merges the answers.

Splitting a range helps a range of many hot keys, because the two halves can live on different nodes. It never helps one key, which stays whole on one side of the cut, and it never helps a moving spot, because the newest half is hot again at once.

Moving Data Under Load

Rebalancing without downtime takes four steps. Copy a snapshot of the partition to its new node. Stream the writes that arrived since the snapshot. Switch the map, the ring or the directory, once the copy has caught up. Then retire the old copy.

Throttle the copy, because it competes with live traffic on both nodes. Start moves deliberately, by a person or a slow controller: a store that rebalances every time a node looks slow can turn one slow node into a whole cluster busy moving data. Which keys move is the previous lesson's arithmetic; this is how they move.

Moving a partition while it serves traffic
Copy the snapshotto the new node
→
Stream the catch-upwrites since then
→
Switch the maponce, when caught up
→
Retire the old copylast
Splitting vs Salting

Splitting cuts a hot partition's range in two and moves half to another node. Use it when the heat is many keys sharing one range.

Salting cuts one key into sub-keys on several partitions, and every read gathers them. Use it when the heat is one key's writes and its reads can afford the gather.

How Interviewers Probe This

The first probe is "One key takes fifty times the traffic of the others. What happens?" A good answer names its partition and that partition's limit, says whether the heat is reads or writes, and gives the fix with its cost: the cache for reads, salting or buffering for writes.

The second is "Your key starts with a timestamp. Anything wrong with that?" A good answer says that every new write lands on the newest slice, gives the multiple over one partition, adds a bucket prefix, and says what reads by time now cost. You might say: "Every new write lands on the newest slice, twenty times what one partition takes. I'd prefix forty buckets and accept that a query for the last hour asks all forty."

The third is "You need to move a partition while it serves traffic. Walk me through it." A good answer gives the snapshot, the catch-up stream, one switch of the map and the throttle, and says what a client with a stale map gets: a redirect or a retry.

Misconceptions
  • "Adding partitions fixes a hot partition." One key lives on one partition however many there are; more partitions only spread more keys.
  • "Hashing the key prevents hot spots." It stops ordered keys piling onto one slice. It does nothing for one key's traffic.
  • "Salt every key to be safe." Every read then gathers sub-keys. Salt only the keys known or measured to be hot, and keep the list.
  • "Rebalancing is a free background job." The copy spends disk and network on the busiest nodes, and the switch must not lose the writes made during the copy.
  • "A cache in front fixes any hot key." It fixes reads. A key that is hot for writes needs salting or buffering.
Why It Matters
  • Name the hot thing first, a key, a range or the newest slice, and match the fix to it.
  • Put the heat against one partition's limit and say how many ways the load must split.
  • Salt or buffer only the keys that are hot, and say how the design finds out which ones are.
  • Move partitions with a throttled copy, a catch-up stream and one switch of the map, started on purpose and never by every slow heartbeat.
RelatedWhen the Cache Hurts hot keys in the cache tierConsistent Hashing what moves when a node joinsURL Shortener the viral linkNews Feed a celebrity's followersAd-Click Aggregation one ad, a tenth of the clicks

Knowledge Check

A like counter takes 4,000 increments a second, and every increment must be durable the moment it is acknowledged. Which fix fits?

  • Buffer the increments in each server and write the total every second
  • Salt it into sub-keys on several partitions, and sum them on each read
  • Put the counter in the cache tier, in front of the partition it lives on
  • Add partitions until the counter's share of the load falls under the limit

Why does adding partitions not help one hot key?

  • New partitions start empty and take only keys written after they join
  • More partitions raise the limit of each partition, but only for reads
  • One key lives on one partition, however many partitions there are
  • The router has to lock every partition while it looks a hot key up

One key draws 12,000 reads a second, and one partition takes 3,000 reads a second. How far over is it, and what takes the load?

  • About four times over, and the cache tier and local copies take it
  • About four times over, and splitting its partition's range fixes it
  • About four times over, and four more partitions spread its reads
  • About twelve times over, and twelve read replicas take the load

A store keyed by time adds a prefix of 40 buckets to spread its new writes. What does that cost?

  • Each write now goes to all 40 partitions, one copy in every bucket
  • The store can no longer read a single record when given its ID
  • A query for the last hour asks 40 partitions and merges the answers
  • The old buckets must be rebalanced every hour as time moves on

A partition moves to a new node while it serves traffic. Which order of steps is right?

  • Switch the map, copy a snapshot, stream the catch-up, retire the old copy
  • Copy a snapshot, switch the map, retire the old copy, stream the catch-up
  • Stop all writes, copy a snapshot, switch the map, start writes again
  • Copy a snapshot, stream the catch-up, switch the map, retire the old copy

You got correct