Try
↑ ↓ move↵ openesc close

Interview prep / Caching

Mid · Caching · System design interview question

Your cache budget is 8 GB, objects average 4 KB, and 20 million distinct objects are requested each day. How would you estimate the hit rate before buying hardware?

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

A strong answer

Start with capacity. 8 GB at 4 KB per object holds about 2 million objects, which is 10 percent of the 20 million. If requests were uniform, the hit rate would be 10 percent, and the purchase would not be worth it.

Real traffic is skewed. Model it as Zipf and sum the popularity of the top 2 million keys. At skew 1.0 that is about ln(2,000,000) over ln(20,000,000), adjusted by the harmonic-number constant, or roughly 87 percent. At skew 0.9 it is about 75 percent, at 0.8 about 62 percent, and at 0.7 about 50 percent. The estimate is very sensitive to a number you have not measured.

So do not buy on the model. Replay a day of real request logs through a cache simulator at several sizes, for example 1, 2, 4 and 8 GB, and plot hit rate against size. The curve shows the knee, where more memory stops paying, and you can choose the cheapest point that meets the database's load target.

Check what the replay hides. Object sizes vary, so object hit rate and byte hit rate differ, and the busiest hour usually matters more than the daily average.

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