Try
↑ ↓ move↵ openesc close

Interview prep / Caching

Mid · Caching · System design interview question

One key in a 30-node cache cluster receives 150,000 reads a second. A node can serve about 100,000. The cluster is far below capacity on average. What do you do?

Sketch your own answer first, then compare it with the one below.

A strong answer

Hashing sends every read of that key to the one node that owns it, so adding nodes does nothing. The cluster has spare capacity in the wrong place.

The fix is to spread the reads of that one key, and there are two usual ways.

First, add a tiny cache in the application servers. With 200 app servers each caching the key in process for one second, the cache node sees at most 200 reads a second for it. The price is up to one second of extra staleness.

Second, replicate the key. Store it under several names, such as key#0 to key#7, and have each reader pick one at random. Eight copies turn 150,000 reads into about 19,000 per copy, spread over different nodes. The cost is on the write side: an update or invalidation has to reach every copy, and the copies can briefly disagree.

Neither should be applied to every key. Detect hot keys with sampled counters on the cache nodes or in the clients, and apply the treatment to those only. Production memcache deployments replicate the hottest categories of keys across servers for the same reason.

Do the arithmetic for your own case before choosing. The right number of copies is the peak rate divided by the share one node can give it.

Sources

Learn it by running it

The caching and eviction lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms