Try
↑ ↓ move↵ openesc close
In this lesson · topic 1 of 7
  1. 1. Why a small cache works at all
  2. 2. FIFO, LRU, LFU, and TinyLFU
  3. 3. Move the hot keys under four policies
  4. 4. Why plain LFU stays stuck after a shift
  5. 5. How big a cache do you need
  6. 6. When a hot key expires
  7. 7. Check the eviction rules

Topic 1

Why a small cache works at all

4 min read

Slow storage, fast memory

Your service reads data from a database or another service. Each read is slow compared with reading from memory on the same machine. A cache keeps a copy of some of the data in that fast memory. A request for a key the cache holds is a hit and is fast. A request for a key it does not hold is a miss, and has to take the slow path.

The cache is much smaller than the data. So two questions decide whether it helps. Which keys do requests ask for, and which keys does the cache keep?

Popularity is skewed

If every key were equally likely, a cache that holds a twentieth of the keys would answer a twentieth of the requests. Nothing you did would change that. Real traffic is not like this. A few keys get a large share of requests and most keys are rarely touched. The usual model is the Zipf distribution: the key at rank k is requested in proportion to one over k raised to a power. That power is the skew. At zero it is uniform, and as it rises a smaller set of keys takes more of the traffic.

The lab uses this model with a cache of 50 keys and a keyspace of 1000 keys. With uniform requests the hit rate is 5%measured, which is what cache size over keyspace predicts. With skew 1.0 and an LRU cache it is 46%measured.

Predict first

Same cache, same size, same number of keys. You raise the Zipf skew from 0.2 to 1.4. What happens to the hit rate?

The two decisions

A cache has to make two choices. When a key is missing and the cache is full, which key leaves? That is the eviction policy. And, for some designs, should the new key even come in? That is an admission policy.

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