Course outline · 0% complete

0/29 lessons0%

Course overview →

Where caches live

lesson 3-1 · ~9 min · 7/29

Quiz

Warm-up from lesson 1-2: in the one-server timing breakdown, which step took 80% of each request's time?

What a cache is

A cache is a small, fast store that keeps copies of recently used data so repeated requests skip the slow, authoritative source. The database stays the source of truth. The cache is a disposable copy: losing it costs speed, never correctness.

The payoff is enormous because real traffic repeats itself. A read from Redis (the shared in-memory store from lesson 2-3) takes well under 1 ms. The same read from the database takes 10 to 50 ms.

Two words you will use daily:

  • Cache hit: the data was in the cache, answer instantly
  • Cache miss: it was not, fetch from the source, usually storing a copy for next time

The fraction of requests that hit is the hit rate, the single number that tells you whether a cache is earning its keep.

The four layers where caches live

A request from a browser can be answered from four caches, each one closer to the database:

LayerLives whereTypical contents
Browser cacheThe user's deviceImages, CSS, JS files
CDNServers near the user, worldwideStatic files, public pages
App cache (Redis)Next to your app serversQuery results, sessions, computed values
Database cacheInside the DB engineHot pages and rows in RAM

A CDN (content delivery network) is a rented fleet of caching servers spread around the world, so a user in Tokyo gets your logo from Tokyo instead of your data center in Virginia. You met the latency reason in How the Internet Works: distance costs milliseconds, roughly 100 ms for a trans-Pacific round trip.

Rule of thumb: serve each request from the layer closest to the user that can correctly answer it.

Quiz

Your product photos load slowly for users in Australia, but fast in the US where your servers are. Which cache layer fixes this?

Problem

Your app serves 20,000 reads per second. You add a Redis cache with a 90% hit rate. How many reads per second still reach the database?