FreePatternRedis
Caching, hit and miss
In one sentence: A cache answers repeat questions from memory, so the database only does the work once.
Animated diagrams are not available for printing. Get StackKinetic Pro to download this explainer.
Inspect:
Read this explainer as text
Cache hit
Beginner
- You open a product page.
- The app first checks its fast notepad: “Do I already know this?”
- Yes! The answer is there, ready to use.
- Your page appears quickly, and the database wasn’t bothered at all.
Practitioner
- The client requests a product.
- The app looks up the product’s key in the cache.
- Cache hit: the stored product comes straight back.
- The app responds without touching the database.
Architect
- GET /products/42 reaches the app tier.
- GET product:42 against the cache.
- Hit, served from memory in well under a millisecond.
- Response served with zero database load; hit ratio drives both latency and DB sizing.
Cache miss
Beginner
- You open a page nobody has looked at recently.
- The app checks its fast notepad first.
- Nothing there this time.
- So the app asks the database, which is slower but always has the answer.
- The database sends back the product.
- The app writes the answer in its notepad for next time.
- You get your page. The next person who asks will get it much faster.
Practitioner
- The client requests a product.
- The app looks up the key in the cache.
- Cache miss: the key isn’t stored.
- The app queries the database.
- The database returns the row.
- The app stores the result in the cache with an expiry time.
- The response goes back; the next request will be a hit.
Architect
- GET /products/77 reaches the app tier.
- GET product:77 against the cache.
- Miss (never cached, expired, or evicted).
- Fallback query against the primary store.
- Row returned. This is the expensive path the cache exists to avoid.
- SET product:77 with a TTL so stale entries eventually age out.
- Served. Subsequent reads are hits until the TTL expires or the key is invalidated.
Data changes
Beginner
- Someone changes the product’s price.
- The app saves the new price in the database first.
- The database confirms it’s saved.
- Then the app crosses out the old price in its notepad, so nobody sees a stale answer.
- Done. The next visitor gets the new price.
Practitioner
- The client sends an update.
- The app writes the change to the database.
- The database acknowledges the write.
- The app deletes the cached copy so the next read fetches fresh data.
- The update is confirmed; the next read refills the cache.
Architect
- PUT /products/42 with a new price.
- Write committed to the source of truth first.
- Commit acknowledged.
- DEL product:42: invalidate rather than update, which avoids racing writers.
- Write path complete; the next read is a miss that repopulates the key.
Now add…
Take it with you
Pro adds video downloads, with embeds and editable source coming soon. Core explainers are always free to watch.