retrystorm · Learn · Interview practice · Glossary · Components

Home › Learn › Sizing a Cache

Chapter 14 · Caching and edge · 15 min

Sizing a Cache

Working sets, eviction, why each extra hit costs more than the last, and when cache memory beats database capacity.

Inkwell Shop's catalog has grown to 50 000 products, served by a slow catalog database. The old cache barely helps any more, and finance is asking why the shop's bill keeps climbing.

A cache can't hold everything, and it doesn't need to. How big it should be depends on how concentrated the traffic is, and on what the misses cost you.

The working set

A cache works because a few items get most of the requests. The items that are asked for often enough to be worth keeping are the working set. If the cache holds the working set, most requests hit; if it holds only part of it, the rest miss and go to the database.

So the useful question is never "how big is the data?" but "how many items get most of the traffic?" A catalog of 50 000 products can be served mostly from a cache of a few thousand.

Eviction

When the cache is full and a new item arrives, something has to go. Most caches use LRU (least recently used): they drop the item nobody has asked for in the longest time.

Cache holds 3 itemsRequestCache after (most recent first)
A, B, CD (miss)D, A, B (C evicted)
D, A, BA (hit)A, D, B
A, D, BC (miss)C, A, D (B evicted)

Popular items keep getting requested, so they stay near the front; rarely asked-for ones drift to the back and fall out. LRU keeps roughly the working set without anyone having to say what it is.

Bigger caches, dearer hits

Doubling the cache doesn't double the hits. The first slots hold the most popular items, which bring the most hits; each later slot holds something less popular than the last, so it catches fewer reads. Under a typical skew, each doubling adds about the same number of points, but each doubling also costs twice as much memory as the one before.

A catalog of 10 000 items below, read with a typical skew. Move the cache size along, and then change how concentrated the reads are.

Skew decides

How concentrated the traffic is matters as much as the cache's size. With strong skew, a small cache already holds most of what's asked for. With traffic spread out, even a large cache misses a lot.

With nearly even reads, the same cache of 500 items catches under 10%. With strong skew, it catches about 93%. Look at the traffic before deciding how big a cache needs to be.

Paying for a cache

Memory costs far more per gigabyte than the disks a database keeps its data on, but it's cheap per read served: a hit takes a fraction of the work a database read does. A cache that takes half the reads off the database can let you run a smaller database, and the two together cost less than a big database alone. The catch: when the cache is cold (just after a restart) or fails, the full load lands on that smaller database, so it has to survive that, or the cache has to warm up before it takes traffic. But past the working set, every extra bit of cache costs more than the few misses it saves.

In the wild

The ideas in this chapter, and where you'll meet them in real systems and interviews:

IdeaWhere you'll meet it
EvictionRedis maxmemory-policy allkeys-lru, Memcached's LRU
Watching hit ratesRedis INFO stats (hits and misses), cache dashboards in Datadog or CloudWatch

Your practice

Inkwell Shop's catalog has grown to 50 000 products, served by a slow catalog database. Product pages have to load fast, and finance has a budget. Pick the cache and database that make most pages fast for the least money.

Key takeaways

  • Size a cache for the working set, the items that get most of the traffic, not the whole dataset.
  • LRU drops the item asked for least recently, which keeps roughly the working set.
  • Each extra slot catches fewer reads than the last, so every doubling pays more per extra hit.
  • The more concentrated the traffic, the smaller the cache it needs.
  • A cache plus a smaller database often costs less than a big database alone, if the database can survive a cold cache.

Read first

  • Caching

Practise it

  • Right-Size the Cache: A big catalog behind a slow database. Find the cache that makes pages fast for the least money.

Terms

  • LRU: Least recently used: evicts the item nobody has asked for in the longest time.
  • Working set: The items asked for often enough to be worth caching.
AI system design mock interview · About · Contact · Privacy · Terms of use