Try
↑ ↓ move↵ openesc close

Interview prep / Caching

Senior · Caching · System design interview question

You must replace a cache cluster and the new one starts empty. The database can absorb only 10 percent of normal read traffic. How do you cut over?

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

A strong answer

A direct cutover sends 100 percent of reads to an empty cache. Every one misses, so the database sees ten times what it can take, and the slowdown makes the misses pile up.

Do the arithmetic for a ramp. If a fraction f of traffic goes to the new cluster, which has hit rate h, the database sees f × (1 − h) of normal traffic from it. At the start h is near 0, so f can be at most 0.1. As h climbs the cluster can take more: at h = 0.5, f can be 0.2, and at h = 0.9 it can take everything. So ramp by share of users or key ranges, watching the hit rate and database load, and widen the share only while the load stays under the limit.

Two things speed up the ramp. Warm the new cluster from the old one: on a miss, the new cache reads from the old cluster instead of the database, and fills itself. Or preload the hottest keys from a recent list of the most requested ones.

Keep the old cluster until the new one reaches its normal hit rate, so you can roll back. And leave the usual protections on: coalesced misses, a cap on concurrent database queries, and shedding.

Sources

  • Rajesh Nishtala, Hans Fugal, Steven Grimm, Marc Kwiatkowski, Herman Lee, Harry C. Li, Ryan McElroy, Mike Paleczny, Daniel Peek, Paul Saab, David Stafford, Tony Tung, Venkateshwaran Venkataramani (2013). Scaling Memcache at Facebook.

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