Workbench / The TiffinBox Build
S52 · V3·51
Later, Reliably
Volume 3, chapter 51. TiffinBox's background work had no home: the RUM summary was computed on request while holding the lock every beacon needs, so a dashboard refreshing ten times a second pushed beacons' p99 from 65 µs to 105 ms; stage 50's compactor rewrote the whole snapshot after every roll, ten bytes for every byte written; and stage 49's and 51's per-request lines about deprecated calls and denials told nobody whose code to change. This stage adds Jobs (named, fixed-delay, failures counted and logged instead of silently cancelling the schedule as the JDK's scheduler does, state at GET /jobs), computes the summary outside the lock and refreshes it on a schedule, compacts only when closed segments reach half the snapshot (1.9 bytes rewritten per byte), and reports deprecated uses and denials by caller and route once a day.
Stage 52: later, reliably
The brief
What you'll build.
Jobs:every(name, period, work)with fixed delay, one run at a time per job; a failure is counted, logged with the job's name and recorded as the last error, and the job runs again;runNow;describe(). Errors other thanVirtualMachineErrornever escape to the JDK's executor.GET /jobsandPOST /jobs/{name}/run(admin).- RUM:
summary()copies each series under the lock and sorts outside it; therum-summaryjob refreshes a cached copy every 10 s;GET /rum/summaryreturns it. - Compaction ratio:
OrderLog.Options.compactRatio(default 0.5): compact when closed segments reach that fraction of the snapshot;stats()reports compactions, skips, bytes appended and rewritten. - The
usagejob: deprecated uses and access denials counted by caller and route, written as one{"kind":"usage",…}line a day, then reset. - Tests:
JobsTest(2),CompactionTest.aRatioCompactsOnlyWhenTheClosedSegmentsAreWorthIt,ServerTest.theUsageJobReportsDeprecatedUsesByCallerAndRoute. - Measurements in
exp/52:RumLock.java(rumlock-before.out,rumlock-after.out),Amplify.java(amplify.out),Silent.java(silent.out),jobs.out,usage.out.
Why a real company needs this. Every service grows background work: summaries, cleanups, reports, maintenance. Unowned, it takes locks it shouldn't, grows with the data, and fails without anyone noticing.
Why now. Chapter 51 is asynchronous work, jobs and messaging. Stage 32 left the RUM summary on the request path; stage 50 left compaction's trigger; stages 49 and 51 left per-caller reporting.
What you'll have at the end.
- one place for background work, with visible health;
- requests that don't wait for reports;
- maintenance with a budget;
- a daily answer to "who still calls the old API?";
- ADR-0054: background work in TiffinBox.
Theory to own
- Lock scope. What must a lock cover, and what can happen outside it? (Companion, Gaps 1 and Theory 1.)
- Write amplification. How does a maintenance job's cost scale, and what bounds it? (Companion, Gaps 2 and Theory 2.)
- Failure as a state. Why alert on the last success, not on errors? (The chapter, "Scheduled jobs"; Companion, Gaps 3 and Theory 3.)
- In-process or durable. Which of this stage's work may be lost on a crash? (The chapter, "Choosing where work runs"; Companion, Gaps 4 and Theory 4.)
Build it
Step 1: who waits for the summary
Write exp/52/RumLock.java: fill RUM to its limits, then accept 2,000 small beacons one a millisecond, alone and while another thread asks for the summary ten times a second. Write exp/52/Silent.java. Run both on stage 51's code.
Checkpoint. exp/52/rumlock-before.out, silent.out.
Step 2: jobs
Write Jobs and JobsTest; add GET /jobs and POST /jobs/{name}/run.
Checkpoint. JobsTest passes; exp/52/jobs.out.
Step 3: the summary off the lock
Change Rum.summary() to copy under the lock and compute outside; add refresh() and latest(), the rum-summary job, and serve latest(). Run RumLock again.
Checkpoint. exp/52/rumlock-after.out.
Step 4: compaction with a budget
Add compactRatio, worthCompacting and the counters to OrderLog; pass the default ratio from Main. Write exp/52/Amplify.java (the same workload at ratio 0 and 0.5) and the ratio test.
Checkpoint. exp/52/amplify.out.
Step 5: the usage report
Count deprecated uses and denials by caller and route in Server.access, add the usage job, and test it. Run the release check.
Checkpoint. exp/52/usage.out; core 57, server 74 and tbx 21 tests; golden master: 47 exchanges, identical; the release check passes.
Senior and architect lens
1. Background work is a feature with an owner. Name it, schedule it, budget it, and show its health where operators look.
2. ADR-0054: background work in TiffinBox. Write it with these points:
- Where: in-process jobs (
Jobs) for derived, rebuildable work (summaries, reports, maintenance); never for work whose loss loses an order. Durable work reads the log (a cursor per consumer) when it's needed. - How: fixed delay, one run at a time, failures counted and logged, state at
GET /jobs; alert when a job's last success is older than three periods. - Budgets: compaction by ratio (0.5); background computations copy under locks and compute outside them.
- Reports: a daily usage line by caller and route for deprecations and denials.
- Single instance: jobs run in every server process; with more than one instance, report jobs need a lease or a single runner (stage 56).
3. Copy, then compute. The cheapest fix for a slow reader is to make it stop holding what writers need.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| two server instances | each runs every job (two usage reports) | stage 56, re-pointed there to Volume 5 (distributed systems: leases, once there's a second instance) |
| a job that hangs | it never runs again; only staleness shows it | stage 53 (resilience: deadlines for jobs and calls) |
| the kitchen stream after a restart | clients get a snapshot, not missed events | stage 56, re-pointed there to Volume 5 (distributed systems: consumers reading a log from their own offsets) |
the event stream code still lives in Server |
615 lines | stage 56, re-pointed there to Volume 5 (distributed systems: the stream as its own service) |
Evidence
exp/52/rumlock-before.out,rumlock-after.out,amplify.out,silent.out,jobs.out,usage.out.Jobs,GET /jobs,POST /jobs/{name}/run, the summary job and off-lock summary,compactRatio, the usage job.- Core 57, server 74 and tbx 21 tests;
golden master: 47 exchanges, identical; the release check passes. - ADR-0054 is committed.
Carry forward
- New background work is a
Jobsentry with a period and an owner. - Locks cover copies, not computations.
- Maintenance has a ratio or a rate, and a metric.