Remembering an answer you will need again
After this: Say what a cache, a hit and a miss are, and count the hits, misses and time of ten reads.
On this page · 4 sections · 3 checks
A shop's website shows the price of a pen on its home page. Every time someone opens the page, the app server asks the database the same question: what does the pen cost? A thousand visitors a second means a thousand identical questions a second, and the answer is the same every time. Why ask again?
Keep the answer close by
You do this yourself. When you look up a friend's phone number, you write it on a note on your desk. Next time, you read the note instead of searching again. The note is faster than the search, and it only has room for the numbers you use often.
A program can do the same. It keeps answers it has already fetched in a fast store, and looks there first. That store is a . Here the slow source is the database, and the cache sits beside the app server.
Each answer in the cache is stored under a key, such as p1 for the pen's product id. A cache is a key-value map, the same idea as the hash map you met in Hash Maps & Hashing: give it a key, and it finds the value in about the same time however many keys it holds.
Hits and misses
When the app asks the cache for a key, one of two things happens.
- The key is there. That is a : the cache answers, and the database is not asked.
- The key is not there. That is a : the app reads the database, puts the answer in the cache for next time, and then answers.
This learning path uses round teaching numbers for time: the cache answers in 1 ms (a thousandth of a second) and the database in 10 ms. The app always asks the cache first, so:
- A hit costs 1 ms: the cache answers.
- A miss costs 11 ms: 1 ms to learn the key is not in the cache, then 10 ms for the database.
The time one read takes, from asking to answering, is its .
Ten reads
Take ten reads of four products, with the cache empty at the start and no limit on its size: p1 p2 p1 p3 p1 p2 p4 p1 p2 p3. Work the first four by hand.
p1: the cache is empty, a miss. The app reads the database and storesp1. 11 ms.p2: not in the cache, a miss. Storep2. 11 ms.p1: stored in step 1, a hit. 1 ms.p3: a new key, a miss. 11 ms.
Now step through all ten. The task asks you to say hit or miss before each read.
- Cache-aside: 10 requestsPress Run it and step through the ten reads. Before each read, look at the keys the cache card holds and say hit or miss. Then watch the Reads strip and the counters under the picture.
Press Run it, then step through and watch the requests move.
The Reads strip shows M M H M H H M H H H. That is 6 hits and 4 misses. Each key missed once, the first time it was read, and hit every time after. So the database was read 4 times instead of 10.
| Reads | Time each | Time in all | |
|---|---|---|---|
| Hits | 6 | 1 ms | 6 ms |
| Misses | 4 | 11 ms | 44 ms |
| With the cache | 10 | 50 ms, 5 ms on average | |
| Without a cache | 10 | 10 ms | 100 ms |
The cache halved the total time, and it took six of every ten questions away from the database.
Check yourself
The cache starts empty and has no size limit. The app reads q1, q2, q2, q3 and then q1. Is the last read, q1, a hit or a miss?
With this lesson's teaching costs (the cache answers in 1 ms, the database in 10 ms), how long does one miss take?
In the ten reads p1 p2 p1 p3 p1 p2 p4 p1 p2 p3, how many times did the app read the database?
- Say what a cache is: a small, fast store of answers already fetched, looked at before the slow database.
- Tell a hit from a miss: a hit is answered by the cache in 1 ms; a miss checks the cache, reads the database and stores the answer, 11 ms in all.
- Count the ten reads p1 p2 p1 p3 p1 p2 p4 p1 p2 p3: 6 hits and 4 misses, 50 ms in all against 100 ms with no cache.
- Say that only misses reach the database: 4 database reads instead of 10.