What a hit ratio buys
After this: Work out the average time of a read and the load left on the database from a hit ratio.
On this page · 4 sections · 3 checks
In the ten reads, 6 out of 10 were hits, and the average read took 5 ms instead of 10. Would 9 out of 10 be much better? Would 99 out of 100? This lesson puts numbers on that, because the answer decides whether a cache is worth adding.
The hit ratio
The share of reads that are hits is the : hits divided by all reads. For the ten reads it is 6 / 10 = 0.6, or 60%. The share of misses is the rest: 1 − 0.6 = 0.4.
Run the ten reads again and watch the hit ratio and the average under the picture. They move in opposite directions: each hit pulls the average down, each miss pushes it up.
- Cache-aside: 10 requestsRun it and watch two counters as you step: the hit ratio and the average time. Each hit pulls the average down a little; each miss pushes it up.
Press Run it, then step through and watch the requests move.
The average time of a read
Work the average for the ten reads one step at a time.
- The hits take 6 × 1 ms = 6 ms.
- The misses take 4 × 11 ms = 44 ms.
- Together that is 50 ms, over 10 reads: 5 ms on average.
The same sum works for any hit ratio. Call the hit ratio h. A share h of reads take 1 ms, and the share 1 − h take 11 ms:
average = h × 1 + (1 − h) × 11 = 11 − 10h ms
Check it with h = 0.6: 11 − 6 = 5 ms, as above.
The load on the database
Only misses reach the database. If a site makes 1,000 reads per second, the database sees 1,000 × (1 − h) of them each second. At h = 0.9 that is 1,000 × 0.1 = 100 reads per second.
This program works out both numbers for six hit ratios. Each line prints the hit ratio, the average time in ms, and the database reads per second at 1,000 reads per second.
| Hit ratio | Average read | Database reads per second |
|---|---|---|
| 0 | 11 ms | 1,000 |
| 0.5 | 6 ms | 500 |
| 0.8 | 3 ms | 200 |
| 0.9 | 2 ms | 100 |
| 0.95 | 1.5 ms | 50 |
| 0.99 | 1.1 ms | 10 |
Two things stand out.
- Going from 0.9 to 0.99 takes less than 1 ms off the average, from 2 ms to 1.1 ms. But it cuts the database's load tenfold, from 100 reads per second to 10. A busy database is often the part that breaks first, so the last few percent of hits matter more than they look.
- At a hit ratio of 0 the average is 11 ms, worse than the 10 ms with no cache at all. Every read pays for the cache check and gets nothing back. A cache in front of reads that never repeat a key makes things slower.
Check yourself
A hit takes 1 ms and a miss 11 ms. If 80% of reads are hits, what is the average time of a read?
A site makes 1,000 reads per second through a cache with a hit ratio of 0.95. How many reads per second reach the database?
A cache is added in front of a database, but the reads never repeat a key, so the hit ratio is 0. What happens to the average time of a read?
- Work out a hit ratio: hits divided by all reads, 0.6 for the ten reads.
- Work out the average time of a read: h × 1 + (1 − h) × 11 = 11 − 10h ms, so 0.9 gives 2 ms.
- Work out the reads left for the database: 1,000 × (1 − h) per second, 100 at 0.9 and 10 at 0.99.
- Say why a cache with a hit ratio of 0 makes reads slower: 11 ms against 10 ms with no cache.