Cache Cake — Performance

Turn on the browser, CDN and app cache layers so requests rarely reach the database: 12 levels of caching strategies and cache invalidation.

What you learn in this game

An HTTP response can be kept on its way to its reader: by the browser (a private cache, one per device) and by shared caches such as a CDN and a reverse proxy, which serve everyone. What decides is not the cache but the response's own headers: Cache-Control, ETag, Vary (RFC 9111).

A kept box can be checked with the server through a validator: the first response carries an ETag, and the cache asks If-None-Match with that value. If nothing changed, the server answers 304 Not Modified with no body, and the cache uses its copy again.

A shared cache serves many people with one copy. A response whose body is one person's (an order, a profile, a dashboard) must not go in there. The response's headers can limit who may keep it.

max-age applies to every cache. s-maxage only to shared caches (CDN, proxy), where it replaces max-age. With both, the browser and the CDN can keep a response for different times.

What the levels ask

  1. Five villagers open the shop's site, each looking at the plain lapis picture twice. A box with no label goes stale at once, so every request lights the oven again. The picture is the same for everyone and unchanged all week. Write its label so the oven is lit once.
  2. The list of today's cakes at /api/kue-hari-ini changes with every new tray: this morning at 07.12 and 07.25. No villager may get an old list after a new one is out. The oven only needs to make the list three times, once per version.
  3. /api/pesanan-saya holds each villager's own order, the box marked with their name. Bu Asih, Pak Tono and Rara open it twice. A named box must not sit on the counter or the back shelf, let alone reach someone else. The oven may be lit once per villager.
  4. The home page (/) is the same for everyone, while /dasbor shows the orders and points of whoever opens it. One switch used to rule every shelf, so the home page was not kept either. Give each page the label that fits. At most three bakes.
  5. The shop's app asks for /api/daftar-harga with a member card (an Authorization header), though the list is the same for everyone. The home page and the price list are both public. Cik Lian wants each box baked once: use every shelf.
  6. Rara opens the logo and the home page three times in five minutes while choosing Imlek cakes. The oven may be lit at most twice, and Cik Lian walks to the oven at most twice, even just to say "still the same".
  7. At 07.20 Cik Lian writes new news at /api/kabar-toko and clears her own counter and back shelf. The villagers' bags she cannot reach. Old news may be read for at most one minute after that. Oven: at most two bakes and two visits.
  8. Someone suggests Vary: Cookie on every box "to be safe". Every villager brings their own session cookie. /api/terlaris is the same for everyone; /api/pesanan-saya carries a name. Give labels that are safe and thrifty: at most three bakes.
  9. The home page hands every visitor a basket ticket (Set-Cookie: keranjang=…), and today that cannot change. The logo, the home page and the price list are the same for everyone. At most three bakes, and every villager goes home with their own ticket.
  10. /tabungan shows every villager's Imlek arisan savings. Rara uses her mother's phone, Bu Sari's. The savings book must not stay on any shelf, not even the bag on a phone. The page style (/gaya.css) is the same for everyone. At most five bakes.
  11. The site's script /app.3f9c2e.js never changes: if its content changes, its name changes with it. /api/kabar-toko changes at 07.10, and the old news must not be read after that. At most three bakes.
  12. Everything at once: the plain lapis (unchanged all week), the home page handing out basket tickets, today's cakes asked with a member card (changes at 07.15 as the counter and shelf are cleared), and every villager's order (changes status at 07.10). What changes may be at most one minute late. At most seven bakes.