In this lesson · topic 1 of 7
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
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.