Backend / Caching layers

Caching layers

How keeping copies of answers makes a site fast, and where those copies live. Seven steps follow one price through a miss, a hit, four layers of cache and a stale answer.

Beginner7 steps~7 min
User — asks for the price of product 7
Cache — small, fast store of copies
Database — the slow source of truth
// THE SHORT VERSION

In 30 seconds

A cache keeps a copy of an answer so the slow work of producing it is not repeated. Finding the copy is a hit; not finding it is a miss, which fetches the answer and stores it.

Caches sit at several layers: in the browser, on a CDN near the user, beside the application, and inside the database. The first layer with a copy answers.

Copies expire after a set time to live (TTL). Until then they can be stale: the original changed and the cache still gives the old answer. That is the price of the speed.

Caching layers, step by step

STEPS01 / 07

Timings are round illustrative numbers. Real caches are also limited in size and evict old entries when full, and browsers and CDNs follow HTTP headers such as Cache-Control to decide what to keep.

STEP 01 / 07 Every request goes all the way
THE MATH
no cache: every request = 1 query 1 query = 200 ms
  1. Step 1: Every request goes all the way. A shop page asks for the price of product 7. The app server asks the database, which takes 200 ms, and passes the answer back. The next visitor asks the same question, and the database does the same work again.
  2. Step 2: Miss: fetch it, keep a copy. A cache is a small, fast store placed in front of a slow one. The app looks there first. The first time, the price is not there: a miss. The app asks the database as before, then puts a copy of the answer in the cache under the key price:7.
  3. Step 3: Hit: answer from the copy. The next request for the same price finds the copy: a hit. The app answers in about 2 ms and the database is not asked at all. The more requests that end in a hit, the less work reaches the slow store behind the cache.
  4. Step 4: Caches sit at four layers. Real systems stack caches along the whole path. The browser keeps files on your device. A CDN keeps copies on servers near you. The app keeps results in memory. The database keeps recently read data in memory too. The first layer with a copy answers; here the browser misses and the CDN hits.
  5. Step 5: TTL: copies expire. A copy is not kept for ever. Each entry gets a time to live (TTL), here 60 seconds. Requests within that time are hits. Once it runs out, the copy is treated as gone, so the next request is a miss that fetches a new copy and starts the clock again.
  6. Step 6: What is safe to cache. Two questions decide. Is the answer the same for everyone, and does it matter if it is a little out of date? Logos, scripts and public articles pass both. A basket, a bank balance or the stock left at checkout do not, so they are kept out of shared caches or given very short lives.
  7. Step 7: The cost: stale data. A copy does not know when the original changes. If the shop raises the price to $25 ten seconds after the copy was made, the cache keeps answering $20 until the TTL runs out: 50 seconds of stale answers. A longer TTL gives more hits and longer staleness; choosing it is the trade-off.
VALUES SO FAR
query200 msslow
key····entry
value····copy
hit····fast
layers····places
TTL····rule
source····changed
stale····cost

Spotted a mistake on this page?