Skip to main content
Cachingchapter 3 of 8 · 4 lessons

Cache Read Strategies

How the application reads data, from cache first, or transparently.

Your product page takes 38 milliseconds, and 30 of those are three database queries that return the same rows for every visitor.

So you cache it. Ask the cache first, and when it comes back empty, read the database and put a copy back. The page drops to 2 milliseconds and your database load falls by 95 percent.

You have just written the pattern almost everybody writes first, and there is a second arrangement where the cache does that fallback for you and your code never knows whether it was a hit.

The difference sounds cosmetic. It decides where your logic lives, and it decides what happens to your site the day the cache goes down, which is the question worth asking before you pick.

Lessons

4 in this chapter
  1. Cache-AsideThe app checks the cache first and takes responsibility for filling it on a miss.3 min
  2. Read-ThroughThe cache fetches from the database itself, so the app only ever talks to the cache.3 min
  3. What a Hit Rate Actually Buys YouHit rate is a multiplier on database load, and the last few percent matter most.2 min
  4. Staleness and TTLsEvery cached read might be a lie, and the TTL is how long you agree to be lied to.2 min