Workbench / The TiffinBox Build
S48 · V3·47
Ready, Then Gone
Volume 3, chapter 47. TiffinBox had one health URL that said 200 as soon as the socket was open, while its first requests took twenty times as long as later ones, and on SIGTERM it closed its socket at once, so the load balancer kept sending orders to a server that had gone: 8 to 10 failed orders per restart behind haproxy. This stage splits health into live and ready, warms the main read paths before saying ready, and on shutdown says "not ready", keeps serving for a configured drain, then stops: no failed orders in three runs. A readiness endpoint without the drain changed nothing. Measuring the warm-up turned up a 44 ms stall on every kitchen page over 8 KB (Nagle's algorithm against delayed ACKs), which Tomcat's defaults would have prevented. Misspelt TIFFINBOX_ variables now print a warning, and replay and report take the same zone.
Stage 48: ready, then gone
The brief
What you'll build.
/health/liveand/health/ready: live says 200 while the process can answer; ready says 503startinguntil the warm-up is done, 200 while serving, and 503drainingafterSIGTERM./healthstays for old checks.- A warm-up:
Server.warmUp(n)runs n read-only requests (the menu, a kitchen page, a search, the JSON menu, the next tiffin) through the router in the process, beforeready(). The startup line says how long it took. - A drain: the shutdown hook calls
drain(), waits for the configured time, then closes the server. Config:drain(default2s) andwarm-up(default1000); aTIFFINBOX_*variable that matches no setting is a warning;replayandreportaccept--zone=.TCP_NODELAYon every accepted socket.- Tests:
ServerTest.readinessFollowsWarmUpAndDrain,ConfigTest.aMisspeltEnvironmentVariableIsAWarning. - Measurements in
exp/48:cold.py(cold-before.out,cold-after.out),rolling.shwithhaproxy.cfg.inandload.py(rolling-before.out,rolling-after.out),coldpost.py(coldpost.out),nagle.py(nagle-before.out,nagle-after.out).
Why a real company needs this. Every deploy, scale-up and crash recovery starts a server and usually stops one. If either end of that drops requests, a team that deploys ten times a day drops requests ten times a day, and learns to deploy less.
Why now. Chapter 47 is Spring Boot: Actuator, liveness and readiness, graceful shutdown, startup. Stage 41 promised readiness and warm-up; stage 45 promised readiness before shutdown; stage 47 left misspelt variables and the zone for replay and report.
What you'll have at the end.
- a server that says ready when it's warm, and not before;
- restarts behind a load balancer that lose no orders;
- one setting for how long the drain is, checked like every other;
- ADR-0050: how TiffinBox starts and stops behind a load balancer.
Theory to own
- Liveness and readiness. What does each answer, and who acts on it? (The chapter, "Actuator, health and probes"; Companion, Theory 1.)
- Propagation delay. How long after a server says "not ready" can traffic still arrive? (Companion, Gaps 2 and Theory 2.)
- Warm-up. Why are the first requests slow, and what does a warm-up warm? (Companion, Gaps 1, 3 and Theory 3; chapter 40.)
- Nagle and delayed ACKs. Why does a write-write-read exchange wait about 40 ms? (Companion, Gaps 4 and Theory 4; chapter 45's lab.)
Build it
Step 1: how cold, and how many lost
Write exp/48/cold.py (start the server, time until the health URL says 200, then 600 requests on one connection, three starts) and exp/48/rolling.sh (two instances behind haproxy checking every 500 ms, eight clients placing orders through it, SIGTERM to one). Run both on stage 47's jar.
Checkpoint. exp/48/cold-before.out, rolling-before.out.
Step 2: live, ready and warm
Add the two endpoints, notReady, ready(), drain() and warmUp(n); in Main.serve, warm up, then ready(), then print the startup line. Add warm-up to Config and readinessFollowsWarmUpAndDrain.
Checkpoint. exp/48/cold-after.out for warm-up 0, 1000 and 10000.
Step 3: then wait, then stop
Run rolling.sh against /health/ready with --drain=0s. Then add the drain to the shutdown hook and run it with the default.
Checkpoint. exp/48/rolling-after.out.
Step 4: what the warm-up didn't warm
Write exp/48/coldpost.py: a fresh server's first order placements and kitchen pages after the default warm-up, against later ones.
Checkpoint. exp/48/coldpost-before-nodelay.out.
Step 5: 44 milliseconds
Write exp/48/nagle.py: one kept-alive connection, GET /kitchen/k1 as the number of open orders grows. Then set TCP_NODELAY on accepted sockets and measure again, and rerun coldpost.py.
Checkpoint. exp/48/nagle-before.out, nagle-after.out, coldpost.out.
Step 6: settings that say what they ignored
Add drain to Config, the warning for misspelt TIFFINBOX_* variables, and --zone= for replay and report. Run the release check.
Checkpoint. aMisspeltEnvironmentVariableIsAWarning; core 52, server 59, tbx 21 tests; golden master: 47 exchanges, identical.
Senior and architect lens
1. Health is a contract with other systems. Its meaning (live, ready), its timing (when it flips, how long the server keeps serving after) and the other side's settings (interval, threshold) are one design. Change one without the others and deploys start failing again.
2. ADR-0050: how TiffinBox starts and stops behind a load balancer. Write it with these points:
- Endpoints:
/health/live(process can answer; nothing else),/health/ready(warm and not draining);/healthkept for old checks, removed in 0.4. - Start: warm up with read-only requests through the router, then ready; warm-up size is a setting; write paths aren't warmed (no effect-free way yet).
- Stop: on
SIGTERM, not ready, keep serving fordrain, then close idle connections and finish requests in flight (stage 45);drainmust exceed the balancer's check interval × failures plus its reaction (default 2 s for haproxy at 500 ms × 1; Kubernetes deployments: 5 to 10 s, withterminationGracePeriodSecondsabove drain plus the longest request). - Sockets:
TCP_NODELAYon every accepted socket. - Verification:
exp/48/rolling.shcounts failed orders through a restart; zero is the requirement.
3. Find the defaults you inherited, or didn't. An embedded server would have given TiffinBox TCP_NODELAY, buffer sizes, and a graceful shutdown that still needs a drain. Writing your own means owning each; using one means knowing which you're relying on.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| first orders after a start are three times slower | write paths aren't warmed | stage 55 (backend testing: a scratch-log mode that the warm-up and the tests can share) |
| a slow dependency | readiness doesn't know about it | stage 53 (resilience: readiness and load shedding under overload) |
Server holds orders, kitchens, health and HTTP |
681 lines | stage 49 (Spring Web: controllers and services) |
| per-request metrics for dashboards | only access logs and RUM | Volume 9 (observability) |
Evidence
exp/48/cold-before.out,cold-after.out,rolling-before.out,rolling-after.out,coldpost-before-nodelay.out,coldpost.out,nagle-before.out,nagle-after.out./health/live,/health/ready,warmUp, the drain,TCP_NODELAY, the variable warning,--zone=forreplayandreport.- Core 52, server 59 and tbx 21 tests;
golden master: 47 exchanges, identical; the release check passes. - ADR-0050 is committed.
Carry forward
- New read paths join the warm-up if they're on the busy path.
- Restarts are tested under load with
rolling.shwhen shutdown or health changes. - Responses go out in as few writes as possible, and sockets keep
TCP_NODELAY.