Want the details?
What is a cache?
Getting an answer from a database takes time. The request travels to another machine, which reads the data and sends the answer back. If many people ask for the same thing, doing that work every time is a waste.
A cache is a small, fast memory close to the server. It keeps copies of recent answers. When a request comes in, the server looks in the cache first. If the answer is there, it is a hit, and the answer is fast. If not, it is a miss: the server asks the database, and then keeps a copy in the cache for next time.
How to read the bench
| Part on the bench | What it means in a real system |
|---|---|
| Counter (left) | The server. Its display shows the item the current request wants. The lamp lights when the answer is delivered. |
| Shelf next to the counter | The cache. Each block is a copy of one item. Closed places have a lid. |
| Cabinet (far end) | The database. It holds every item, A to L, one per drawer. It is always right, but it is far away. |
| Carriage on the rail | The work of answering. A short trip to the shelf is a hit. A long trip to the cabinet is a miss. The ruler beside the rail marks the long way. |
| Meters | The hit rate and the average answer time over the last 20 requests, and a strip of those requests: filled for a hit, hollow for a miss. |
Which block is thrown out?
The shelf is small. When it is full, a new copy must replace an old one. This bench uses the most common rule, least recently used (LRU): it throws out the block that nobody has asked for in the longest time. Popular items are asked for often, so they stay. Rare items leave first.
The numbers
- There are 12 items. With “a few items are popular”, the most popular item gets about 52% of all requests, and the top four get about 84%. With “every item equally”, each item gets one request in twelve.
- On this bench, a hit takes about 1.7 seconds and a miss about 4.1 seconds.
- Hit rate after the shelf warms up: popular items with 4 places, about 70%. Every item equally with 4 places, about 35%, and with 8 places, about 67%. Popular items with only 2 places, about 45%.
- In real systems the gap is much bigger. A cache in memory answers in less than a millisecond, while a database query often takes 10 to 100 milliseconds.
In real systems
- Common caches. Redis and Memcached keep answers in memory next to the servers. Your browser caches images and files. A CDN, such as Cloudflare, caches whole pages in cities near the visitor.
- Old copies. A copy in the cache becomes wrong when the database changes. Real caches give each copy an expiry time (TTL), or delete it when the data changes. Knowing when to throw a copy out is famously one of the hard problems in computing.
- Other rules. Besides LRU, there is LFU (throw out the least often used) and FIFO (throw out the oldest copy).
- What the bench makes simpler. Real servers answer many requests at the same time, not one by one. The ideas stay the same.
Words to know
- Hit
- The answer was in the cache.
- Miss
- The answer was not in the cache, so the server had to ask the database.
- Hit rate
- The share of requests that are hits.
- Eviction
- Throwing a copy out of the cache to make room.
- LRU
- Least recently used: evict the copy unused for the longest time.
- Cold cache
- An empty cache, just started. Every request misses until it fills.
- TTL
- Time to live: how long a copy may stay before it must be fetched again.