TweakCode
Building Blocks lessons

Partitioning · Stage 3 of 3

Hot-Key Mitigation

Tell hot writes from hot reads, then store once or copy the key

Foundations

So far, a good key spread the load evenly. Now, what if one key is so popular that no key choice spreads it: one record written to millions of places, or one key read 50,000 times a second?

First classify the hotspot. A hot write fans one change out to many targets; the fix is to store it once and merge it in when readers ask, so the write stays one row. A hot read hammers one key; the fix is to copy that key into several caches or read replicas, so many nodes share the reads. Ordinary keys keep the normal path.

Take a system where most records reach 100 readers. One record must reach 10 million: copying it 10 million times is the hot write, so store it once and merge it at read time. One key read 50,000 times a second is the hot read, so cache it on five nodes, about 10,000 reads each.

readcopy50,000/sHot writeStored once1 rowReadersmerge on readHot key5 copies10,000 reads/s eachReaders
A hot write is stored once and merged in at read time, so 10 million copies become one row. A hot read is copied to five caches, so each serves about 10,000 reads a second.
  • Partitioning

    Keys are spread over nodes by hashing. That lesson covers the ring.

    Open lesson

Case Scenario

The design brief

Rebalance a hot celebrity

Timelines are partitioned by viewer id. One viewer is fine. The problem is a celebrity post that writes every viewer, and one viewer whose home is read constantly is a different hotspot.

To see how hot keys need their own fixes, consider home timelines in a feed like X's, partitioned by viewer id: a celebrity post must reach 20 million viewer timelines, a hot write, while one shared-screen viewer whose home is read 50,000 times a second is a hot read. Store the first once; copy the second.

Viewer-id partitioning keeps an ordinary home read in one place, yet two extreme cases remain. A celebrity post targets 20 million different viewer timelines, while one shared-screen viewer can send 50,000 reads per second to a single key. The normal user’s home must stay a one-partition read while neither hotspot overwhelms its owning partitions.

Pro

The rest of this is on Pro

The idea above is free. The walkthrough, the cost, and the practice are on Pro.