The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S42 · V3·41

Measured, Then Changed

Volume 3, chapter 41. Measured at 2,000, 20,000 and 50,000 orders, the TiffinBox server held two copies of every changed order (its retry table kept each order's first version, and returned it: a retry after the kitchen accepted said "Placed", until a restart, when it said "Accepted"), its kitchen page scanned the whole day's history to find 20 open orders (3.8 ms at 50,000 orders, with allocation flat, so no allocation profile could see it), every HTML page was rewritten whole to change its header, and the monitoring buffer, bounded at 2.5 million values, held 71 MB and stopped moving once full. This stage measures each with a counter, a histogram or a timing at several sizes, changes one thing at a time, and measures again: 17% less heap per order, the kitchen page ten times faster at 50,000 orders, a fifth less allocation per page, and 71 MB down to 20 MB. It also shows why two allocation profiles of the same build disagreed, from the JDK's own source.

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

Stage 42: measured, then changed

The brief

What you'll build.

  • Retries that see the order as it is now: IdempotencyKey becomes record IdempotencyKey(long hi, long lo); the retry table (byKey) holds order ids, and a retry returns orders.get(id).
  • An open-order index: openByKitchen, a ConcurrentHashMap<String, Set<String>> kept up to date by trackOpen(Order) after every write and at recovery; openOrders(kitchen) reads it instead of scanning every order. The concurrency test checks that the index equals a scan.
  • A leaner page: Json.str returns early for strings that need no escaping; Pages.themed rewrites only the part of the page before <main id="main">.
  • Rum.Series: a ring of primitive doubles holding each series' newest 5,000 values.
  • Measurements in exp/42: Heap.java (heap and page cost at three sizes), retry.sh, KPage.java (bytes and time per request from the thread's allocation counter), jfr-top.py (allocation samples by TiffinBox frame, and how lumpy their weights are), RumFill.java, Recover.java, VDead.java.
  • Tests: ServerTest.aRetryAfterAMoveSeesTheOrderAsItIsNow, RumTest.aFullSeriesKeepsTheNewestValues, and the index check in ConcurrencyTest.

Why a real company needs this. Memory and latency problems that grow with data pass every test with 20 orders and arrive in production on a busy evening. Measuring at several sizes, with numbers you can trust, is how you find them before customers do; changing one thing at a time is how you know which change helped.

Why now. Chapter 41 is memory and performance: the workflow, where data lives, hardware effects, measuring (JMH, profilers, JFR, jcmd, leaks) and tuning. Stage 40 started comparing live heap after a full GC; this stage makes it the method.

What you'll have at the end.

  • before and after numbers for five changes, from the same harnesses;
  • a retry answer that is the same before and after a restart;
  • ADR-0044: how TiffinBox measures memory and performance.

Theory to own

  1. Memory held versus memory allocated. Which tool answers each, and which problems does each find? (The chapter, "Profiling: what to measure" and "Finding a memory leak".)
  2. Weighted samples. What does an ObjectAllocationSample's weight mean, and why can 420 samples be worth six? (Companion, Gaps 3, Code walkthrough and Theory 2.)
  3. Cost at several sizes. What does measuring at one size hide? (Companion, Gaps 2 and Theory 3.)
  4. Boxing. What does a List<Double> cost per value? (The chapter, "Boxing and unboxing"; Companion, Gaps 5.)

Build it

Step 1: the heap at three sizes

Write exp/42/Heap.java: place N orders from 64 virtual threads (20 in kitchen k7, left open; 90% of the rest delivered), force a GC, take jcmd <pid> GC.class_histogram, and time the kitchen page and the orders API. Run it at 2,000, 20,000 and 50,000 orders.

Checkpoint. exp/42/heap-before.out, heap-before-50k.out.

Step 2: what a retry returns

Write exp/42/retry.sh: place an order with an idempotency key, accept it, retry the same request, restart the server, retry again.

Checkpoint. exp/42/retry-before.out, then retry-after.out after the change; aRetryAfterAMoveSeesTheOrderAsItIsNow.

Step 3: an index of open orders

Add openByKitchen and trackOpen, call it after every orders.put and at recovery, and make openOrders(kitchen) read it. In ConcurrencyTest, after the concurrent run, assert that openOrders equals a scan of the orders map for each kitchen. Re-run Heap.java.

Checkpoint. exp/42/heap-after.out, heap-after-50k.out; server tests pass.

Step 4: allocation per page, counted and sampled

Write exp/42/KPage.java: 20 open orders, 3,000 warm-up renders of a path, then 2,000 measured with getCurrentThreadAllocatedBytes. Run it on the stage 41 build and record it twice with -XX:StartFlightRecording=settings=profile; summarise each recording with jfr-top.py. Then make the two changes (Json.str's early return; themed on the head only) and measure again.

Checkpoint. exp/42/kpage-before.out, kpage-after.out, kpage-after-3runs.out, jfr-before.out, jfr-after-run1.out, jfr-after-run2.out; golden master: 47 exchanges, identical.

Step 5: monitoring at its limits

Write exp/42/RumFill.java: fill Rum to 500 series of 5,000 values, measure the heap it retains, send 5,010 slow values to one full series, and time summary(). Then replace List<Double> with Rum.Series, and add aFullSeriesKeepsTheNewestValues.

Checkpoint. exp/42/rum-before.out, rum-after.out; server 48 tests.

Step 6: the promises from earlier stages

Stage 36 promised to look at rebuild time, and stage 39 at what the JVM reports when virtual threads deadlock. Write exp/42/Recover.java (50,000 orders, then three restarts, timing the log read and the server's construction separately) and exp/42/VDead.java (two virtual threads taking two locks in opposite orders), and look at jcmd <pid> Thread.print and jcmd <pid> Thread.dump_to_file -format=text.

Checkpoint. exp/42/recover.out, vdead.out.

Senior and architect lens

1. One harness, before and after. Every number in this stage has a twin from the same program on the previous build (a git worktree at stage 41's commit). A number without its before is an anecdote.

2. ADR-0044: how TiffinBox measures memory and performance. Write it with these points:

  • Sizes: memory and page costs are measured at 2,000, 20,000 and 50,000 orders; quote the largest, and the slope.
  • Held: live heap and GC.class_histogram after a full GC; look for class counts that are multiples of the orders.
  • Allocated: the thread's allocation counter around a warmed-up operation, three runs.
  • Where: JFR or async-profiler to find candidates, read with the weight distribution; never as the measure of a fix.
  • Bounds: every in-memory collection fed from outside has a limit in bytes as well as in count, and says what happens when full (TiffinBox keeps the newest).
  • Retries: a retry returns the current order, before and after a restart.

3. Indexes are invariants. A derived structure is a second copy of the truth; it is only as good as the check that it equals the first. The concurrency test checks it after concurrent writes.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
Rum.summary() sorts 2.5 million values under the lock beacons need 130 ms pauses for beacons while someone reads the summary stage 52 (async: compute summaries off the request path)
the kitchen page allocates 229 KB per render, most of it the embedded snapshot GC work grows with kitchens and viewers stage 54 (caching: render once per kitchen version)
restarts read the whole log restart time grows with history stage 50 (persistence: a database, or snapshots)
a deadlock among virtual threads no deadlock report from the JVM stage 53 (resilience: request timeouts)

Evidence

  • exp/42/heap-before*.out and heap-after*.out (94,982 and 50,000 Orders; 3,836.7 and 363.1 µs at 50,000 orders).
  • exp/42/retry-before.out (Placed, Placed, Accepted) and retry-after.out (Placed, Accepted, Accepted).
  • exp/42/kpage-*.out, jfr-*.out, dense.jfc.
  • exp/42/rum-before.out and rum-after.out.
  • exp/42/recover.out, vdead.out.
  • Core 52, server 48 and tbx 21 tests; golden master: 47 exchanges, identical; the suite and the release check pass (with NG_NODE set for the Angular build).
  • ADR-0044 is committed.

Carry forward

  • The three sizes, and Heap.java, whenever a later stage adds something kept per order.
  • The counter, not the profile, as the measure of any performance change.
  • Bounds in bytes, keep the newest, for every collection fed from outside.