Machine Room

5 Levels of Caching

One request to a live score page, and five places that can answer it: the browser cache, the CDN, the gateway cache, Redis, and the buffer pool inside MySQL. The one-page notes from the reel.

Download the PDF

One-page notes, 5 Levels of Caching. One request to a live score page, and five places that can answer it. 1, Browser cache: on your phone. A phone shows Orvale 2-1 Briska with the two team crests; an arrow carries them into a drawer labelled browser cache holding orv.png and brk.png. Your browser keeps a copy of files that don't change, like the team logos. Open the page again: they load from your phone. No request is sent to the server. The server's header says how long the copy stays fresh: Cache-Control: max-age=86400 = one day. 2, CDN: a copy near you. Three phones send to a CDN server holding images · orv.png, videos · goal.mp4, CSS · app.css, JavaScript · app.js; far below it, past a clock marked far, the main servers. Files that are the same for everyone come from a CDN server close to you that holds a copy. They don't travel all the way from the main servers every time. Closer is faster: "the closer the client and cache are, the faster the response will be." (MDN). By default, Cloudflare's CDN doesn't cache HTML or JSON. So the live score goes further. 3, Gateway cache: NGINX keeps each answer for 1 second. Five phones send to NGINX, which holds score 2-2 · everyone with a 1s timer, in front of a server and a database holding ORV 2-2 BRK. The setting: proxy_cache_valid 200 1s; 200 = only OK answers, 1s = for one second. A timeline: 0 s: one request goes through to the database. NGINX keeps the answer: 2-2. Within that second: every request gets the score from NGINX. HIT. After 1 s: the copy expires. One request brings the new score: 3-2. NGINX's own test, 2015: a WordPress site, ab -c 10 -t 30. Requests a second: no cache 5.53; 1s cache 600.73, 120×; 1s cache + cache lock + stale while updating 2,185.03. Good for answers that are the same for everyone, even ones that change every second. The price: they can be up to a second old. 4, In-memory cache: Redis. A phone with saved teams: Orvale, Kelmar passes through NGINX to the server, which talks to Redis · in memory, with the database below. Some answers are only for you, so they pass through NGINX. 1. The server asks Redis first: GET user:42:teams. 2. HIT: ["Orvale","Kelmar"]. The database is never asked. 3. MISS: (nil). The server reads the database: SELECT team FROM saved_teams WHERE user_id = 42. 4. It leaves a copy in Redis for next time (here for 5 minutes): SET user:42:teams '["Orvale","Kelmar"]' EX 300. This is cache-aside, which AWS calls lazy loading. 5, Buffer pool: the cache inside MySQL. Inside MySQL, the buffer pool in memory holds page 12 · saved_teams (42 Orvale, 42 Kelmar, 43 Briska) and page 3 · teams; below it, the disk, where page 19 is read up into memory. WHERE user_id = 42: page 12 is in memory, HIT. WHERE user_id = 57: MISS, page 19 is read from the disk and stays in memory. InnoDB keeps the table data it used recently in memory, in pages of rows. When memory is full, the least recently used page is evicted. On a dedicated server, up to 80% of memory often goes to it. It holds pages of rows, never query results: MySQL 8.0 removed the query cache. Each level answers what it can, so the next one gets asked less.

On a phone, press and hold the image to save it. The reel: 5 Levels of Caching Every Developer Should Know.