The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S54 · V3·53

Computed Once per Change

Volume 3, chapter 53. TiffinBox has no database to cache in front of, and still had a stampede: after a deploy every kitchen tablet reconnects, and each kitchen page rebuilt its kitchen's snapshot under the event stream's lock, the same lock every order at every kitchen needs to publish its change. 600 pages for one kitchen made orders at eight other kitchens wait up to 253 ms. This stage gives every kitchen a version (the sequence number of its latest event), caches each kitchen's order list and its first screen of tickets per version, and adds the per-request parts (the server's time) on each use: no TTL, no purge, no invalidation race, and the worst order wait fell to 44–84 ms.

6 parts · ~3 min to read
Updated Suggest a correction

Stage 54: computed once per change

The brief

What you'll build.

  • Kitchen versions: KitchenEvents.version(kitchen), the sequence number of the kitchen's latest event (0 before any); every change to its open orders publishes one.
  • The order-list cache: Orders.snapshot reads the version first, reuses the cached list if it was made at that version, otherwise builds and stores it; serverTime is the request's instant each time. cacheStats() counts hits and misses.
  • The ticket cache: Pages.tickets(kitchen, filter), the first screen of HTML tickets, cached the same way.
  • Tests: OrdersTest.aKitchensSnapshotIsComputedOncePerChange, ServerTest.theKitchenPageShowsAChangeRightAwayDespiteItsCache.
  • Measurements in exp/54: Stampede.java (stampede-before.out, stampede-after.out).

Why a real company needs this. Deploys, failovers and network blips make every client reconnect at once. The first minute after a deploy is the busiest, and whatever each reconnect rebuilds, it rebuilds thousands of times.

Why now. Chapter 53 is caching. Stage 46 cached the menu at the edge (HTTP); stage 24 embedded snapshots in kitchen pages; stage 17 spread reconnects with random delays but didn't make each one cheaper.

What you'll have at the end.

  • kitchen pages and streams that cost one build per change;
  • orders that don't wait for anyone's page;
  • caches that can't serve a stale list;
  • ADR-0056: caching in TiffinBox.

Theory to own

  1. What a cache protects. In TiffinBox, what did each snapshot cost, and who paid? (Companion, Gaps 1 and 4.)
  2. Versioned keys. Why does reading the version first make the cache safe without invalidation? (Companion, Gaps 2 and Theory 1; the chapter's lab, Experiment 4.)
  3. Single-flight. How many times is a list built when 600 requests arrive together? (Companion, Theory 2, Code walkthrough.)
  4. Shared and personal. Which parts of a snapshot or a page can't be cached? (Companion, Gaps 3 and Theory 3.)

Build it

Step 1: a stampede

Write exp/54/Stampede.java: kitchen k7 with 60 open orders; 600 kitchen-page requests released at once while eight clients place orders at eight other kitchens; pages' and orders' latencies and the process's CPU, three rounds. Run it on stage 53's server.

Checkpoint. exp/54/stampede-before.out.

Step 2: versions and the list cache

Add per-kitchen versions to KitchenEvents and the cached list to Orders.snapshot, with the request's instant added each time. Write the OrdersTest case.

Checkpoint. aKitchensSnapshotIsComputedOncePerChange: one miss and one hit for two snapshots of one version, different serverTimes, and a miss after each change.

Step 3: the ticket cache

Move the ticket HTML into Pages.tickets with the same versioned cache. Add the page test. Run Stampede again and the release check.

Checkpoint. exp/54/stampede-after.out; core 57, server 78 and tbx 21 tests; golden master: 47 exchanges, identical; the release check passes.

Senior and architect lens

1. Cache what you rebuild under contention. The biggest win wasn't the most expensive work, but the work done inside a lock everyone shares.

2. ADR-0056: caching in TiffinBox. Write it with these points:

  • Server caches are keyed by the version of what they're built from (kitchen versions); no TTLs; no invalidation messages; versions read before data.
  • Cached parts are shared state only; per-request values (time, nonce, theme, identity) are added per response.
  • HTTP caching (stage 46): the menu with s-maxage=5 and an ETag at shared caches; everything else no-store.
  • Measurement: each cache has hit and miss counters, and a load test with it off.

3. A version is the cheapest invalidation. When state already has a sequence number (here, the event stream's), use it as the key.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
the menu at shared caches changes edges serve the old menu for up to 35 s stage 56 (projects: versioned menu URLs that shared caches keep forever)
one cached list per kitchen, forever memory for kitchens that close stage 56 (projects: kitchens as data, with their caches), re-pointed there to Volume 4 (databases: kitchens as a table)
two server instances each builds its own lists (fine) but versions differ between them Volume 5 (distributed systems: versions that mean the same everywhere)
the cache is only tested by two cases a missed publish would serve stale lists stage 55 (testing: a property test that every change publishes)

Evidence

  • exp/54/stampede-before.out, stampede-after.out.
  • KitchenEvents.version, the list cache in Orders.snapshot, Pages.tickets, cacheStats().
  • Core 57, server 78 and tbx 21 tests; golden master: 47 exchanges, identical; the release check passes.
  • ADR-0056 is committed.

Carry forward

  • Key caches by the version of their source.
  • Read the version, then the data.
  • Never cache what belongs to one request.