The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S53 · V3·52

When the Disk Is Slow

Volume 3, chapter 52. A game day on TiffinBox: one of two instances behind haproxy gets a disk whose fsyncs take 2 seconds longer, injected with a new fault setting. The pair served 41 orders in 20 seconds instead of 36,513: orders for a customer wait for that customer's lock, which was held across the slow write, so every client ended up queued on the slow instance while it reported itself ready. Shedding on the log's own queue didn't help, because the waiting happened before it. This stage measures the oldest write in progress anywhere in the order service, refuses new orders above shed-after (kitchen events only at twice that), answers 503 with Retry-After when nothing was saved, and says "not ready: overloaded" for 5 seconds after the last refusal so the load balancer moves traffic and doesn't flap: 40,194 orders. Scheduled jobs get deadlines.

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

Stage 53: when the disk is slow

The brief

What you'll build.

  • Fault injection: OrderLog.Options.faultFsyncDelay and the fault-fsync-delay setting (0 in production): every fsync takes that much longer, for drills.
  • OrderLog.lagMillis(): how long the oldest write not yet durable has waited.
  • Admission control in Orders: lag() is the older of the log's lag and the oldest place or apply in progress (lock waits included); place refuses above shed-after, apply above twice it, with Overloaded, before anything is written or queued.
  • 503 with Retry-After: 2: "nothing was saved: retry in 2 seconds with the same Idempotency-Key".
  • Readiness: /health/ready says "not ready: overloaded" while writes are behind and for 5 s after the last refusal.
  • Settings: shed-after (default 1s, 0ms turns shedding off), fault-fsync-delay (default 0ms).
  • Job deadlines: Jobs.every(name, period, timeout, work); each run on its own virtual thread, interrupted at the deadline and counted as a failure; while a timed-out run is still alive, later runs are skipped and counted.
  • Tests: OrdersTest.aStalledLogRefusesNewOrdersButNotTheOnesInFlight, JobsTest.aHungJobTimesOutAndLaterRunsAreSkippedNotStacked.
  • Measurements in exp/53: sick.sh and load.py (sick.out, sick-first-version.out).

Why a real company needs this. Dependencies rarely die; they slow down. A service that waits politely behind a slow one passes the slowness to every caller, and a load balancer that only asks "are you alive?" keeps sending traffic to it.

Why now. Chapter 52 is resilience: timeouts, retries, isolation, overload. Stage 48 built readiness for deploys; stage 52 left hung jobs invisible; chapter 52's C2 planned a game day for TiffinBox.

What you'll have at the end.

  • a repeatable drill for a slow disk;
  • an instance that removes itself from rotation when its writes fall behind;
  • refusals that are safe to retry;
  • jobs that can't hang silently;
  • ADR-0055: TiffinBox under overload.

Theory to own

  1. Hidden queues. Where can requests wait in TiffinBox, and which of those did a metric see? (Companion, Gaps 1, 2 and Theory 1.)
  2. Admission control. What should decide whether a request is let in, and what should it get if not? (The chapter, "Load shedding"; Companion, Gaps 2 and Theory 2.)
  3. Safe refusal. When is "retry later" a promise the server can keep? (Companion, Gaps 3; stage 19, idempotency keys.)
  4. Hysteresis. Why hold "not ready" after the condition clears? (Companion, Gaps 2 and Theory 3.)

Build it

Step 1: a slow disk, on purpose

Add faultFsyncDelay to OrderLog.Options and the fault-fsync-delay setting. Write exp/53/load.py (eight clients placing orders for 20 s through haproxy, with latencies by status) and exp/53/sick.sh (two instances behind haproxy checking /health/ready; instance a's fsync slowed). Run it with no fault, then with a's fsync 2 s slower and shedding off.

Checkpoint. The first two runs in exp/53/sick.out.

Step 2: shed on the right signal

Add lagMillis() to OrderLog. First shed when the log's lag passes shed-after; run sick.sh again (exp/53/sick-first-version.out: 2,158 orders). Then measure the oldest place or apply in progress (inProgress), shed before taking the customer's lock, refuse events at twice the threshold, hold readiness for 5 s, and map Overloaded to a 503.

Checkpoint. The third run in exp/53/sick.out; aStalledLogRefusesNewOrdersButNotTheOnesInFlight.

Step 3: deadlines for jobs

Add the timeout overload to Jobs.every, run work on its own virtual thread, interrupt at the deadline, and skip runs while a timed-out one is alive. Write the test with a job that ignores interrupts. Run the release check.

Checkpoint. aHungJobTimesOutAndLaterRunsAreSkippedNotStacked; core 57, server 76 and tbx 21 tests; golden master: 47 exchanges, identical; the release check passes.

Senior and architect lens

1. Drill, then fix. The convoy wasn't in any review; a drill with a measurement found it in 20 seconds, and the same drill is now the regression test.

2. ADR-0055: TiffinBox under overload. Write it with these points:

  • Signal: age of the oldest write in progress in the order service, including lock waits.
  • Admission: new orders refused above shed-after (1 s); kitchen events above twice it; reads never.
  • Response: 503, Retry-After: 2, nothing saved, retry with the same key.
  • Readiness: not ready while overloaded and 5 s after; load balancers check every 500 ms with fall 1, rise 1.
  • Drills: fault-fsync-delay for slow disks; exp/53/sick.sh in staging each release.
  • Jobs: every run has a deadline (default: its period).

3. Locks across I/O are convoys waiting to happen. The stripe lock keeps "first order" correct; it no longer waits on an overloaded disk, because writes are refused before taking it.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
the stripe lock is still held across a normal fsync a slow-but-not-overloaded disk still serialises one customer's orders stage 56 (projects: decide under the lock, write outside it)
tokens can't be revoked one by one (stage 51) a stolen token works until it expires Volume 10 (security: revocation and short-lived tokens from an identity provider)
no request carries a deadline the server keeps working on requests the client gave up on Volume 5 (distributed systems: deadline propagation between services)
the drill runs by hand regressions in overload behaviour go unnoticed stage 55 (testing: the drill as an automated test)

Evidence

  • exp/53/sick.out, sick-first-version.out, load.py, sick.sh.
  • faultFsyncDelay, lagMillis(), admission control in Orders, the 503, overload-aware readiness with a hold, job deadlines, the two settings.
  • Core 57, server 76 and tbx 21 tests; golden master: 47 exchanges, identical; the release check passes.
  • ADR-0055 is committed.

Carry forward

  • Every wait has a limit, and every limit an answer: refuse, retry, or degrade.
  • Measure waiting where it happens, from the moment work enters.
  • Refuse before doing anything, so a retry is always safe.