Workbench / The TiffinBox Build
S46 · V3·45
Through the Middle
Volume 3, chapter 45. Put behind nginx configured as a shared cache, TiffinBox failed in three ways its own tests couldn't see: nginx spoke HTTP/1.0 to it and got a 505 for every request; responses without a caching header were stored for a minute, so a kitchen was told to cook an order that had already been delivered; and the menu's "no-cache" stopped nginx from storing it at all, so 1,000 requests were 1,000 full responses. This stage serves HTTP/1.0, gives every response a caching decision with a test over every route, lets shared caches keep the menu for 5 seconds (7 origin responses instead of 1,000), and adds a check that compares the API contract with the last release, which it uses to remove four fields deprecated in 0.2, as promised for 0.3.
Stage 46: through the middle
The brief
What you'll build.
- HTTP/1.0:
HttpRequestkeeps the request's version; 1.0 requests are served and the connection closed, and don't needHost; other versions still get 505. - A caching decision on every response:
writeaddsCache-Control: no-storeto any response whose route didn't choose;/menu.jsonsendspublic, max-age=0, s-maxage=5, stale-while-revalidate=30with its ETag. tools/contract_diff.py: comparescontract/api.jsonwith the newestcontract/released/*.json(0.2.0's is added), classifies each change as breaking, tolerant or compatible, and fails if a breaking change isn't allowed byVERSION(a new minor before 1.0, a new major after). The release check runs it.- The 0.3 removal:
Order.discountPaise,payablePaise,freeDeliveryandpricedByare gone from the server, the contract and the generated TypeScript;$deprecatedsays so. - A parser fix:
HttpRequest.readpassed its maps throughMap.copyOf, which scrambled the header and query order stage 36's constructor was written to keep. - A test timeout: the frontend unit tests run with
--test-timeout=30000. With the old fields still in its fixtures, the store test failed an assertion inside a listener that then waited forever, and the release check hung for over ten minutes instead of failing. - Tests:
everyResponseSaysWhetherItMayBeCached;strictParsingnow expects 200 for HTTP/1.0 and 505 for HTTP/2.0; the pricing tests readOrder.pricing. - Measurements in
exp/46:nginx.conf.inandedge.sh,contract-diff.out,order-size.out.
Why a real company needs this. Services rarely talk to their clients directly for long: a gateway, a CDN or a customer's proxy arrives, with its own defaults. An API that only works point-to-point fails at the moment someone puts something in the middle. And a removal without a check is a guess about who reads the field.
Why now. Chapter 45 is HTTP API design: resources, HTTP semantics, collections, and change over time. The exercises cover ETags, idempotency, deprecation and rate limits on a fresh API; this stage tests TiffinBox's existing one through the middle, and contracts what 0.2 expanded.
What you'll have at the end.
- an API that works through a default proxy and is never stored where it shouldn't be;
- a menu the edge can serve without asking the origin every time;
- a contract check, and the first contraction made with it;
- ADR-0048: caching and contract changes.
Theory to own
- Shared caches. What does a shared cache do with a response that says nothing about caching, and with one that says
no-cache? (Companion, Gaps 2 and 3, Theory 1, Code walkthrough.) - Freshness for whom. How do
max-ageands-maxagediffer, and what doesstale-while-revalidateadd? (The chapter, "Headers that matter"; Companion, Theory 1.) - Being liberal. Why accept HTTP/1.0 when the parser is strict about everything else? (Companion, Gaps 1 and Theory 2.)
- Breaking for readers. Why is removing an optional field breaking? (The chapter, "Backward and forward compatibility"; Companion, Gaps 4 and Theory 3.)
Build it
Step 1: put a proxy in front
Write exp/46/nginx.conf.in: a proxy_cache with proxy_cache_valid 200 1m, proxy_cache_use_stale updating, proxy_cache_background_update on, and X-Cache showing hit or miss; no proxy_http_version. Write exp/46/edge.sh: place an order, read the kitchen's next order and the order through nginx, move the order through the kitchen and place another, read both again, then send 1,000 menu requests and count the origin's access-log lines.
Checkpoint. exp/46/edge-http10.out.
Step 2: serve HTTP/1.0
Add the request's version to HttpRequest, accept 1.0 (closing after the response, no Host required), and change strictParsing. While there, stop passing the maps through Map.copyOf.
Checkpoint. exp/46/edge-http10-after.out shows real answers through the default configuration.
Step 3: a caching decision on every response
With proxy_http_version 1.1 in the configuration, run edge.sh against the stage 45 build, then add the default no-store and the test over every route.
Checkpoint. exp/46/edge-before.out and edge-after.out.
Step 4: let the edge keep the menu
Change the menu's Cache-Control and rerun the 1,000 requests.
Checkpoint. the origin served /menu.json 7 times (before: 1,000).
Step 5: a contract check, and the 0.3 removal
Save 0.2.0's contract as contract/released/0.2.0.json. Write tools/contract_diff.py. Then remove the four deprecated fields from Json.order, the contract and web-src/contract.ts (node contract/gen.mjs), update the tests, and re-record the golden master.
Checkpoint. exp/46/contract-diff.out, order-size.out; the golden master's diff shows only the four fields and the new cache-control lines.
Senior and architect lens
1. Test through the middle. exp/46/edge.sh runs the real server behind a real proxy. Its findings are about defaults (the proxy's version, the cache's lifetime, the meaning of no-cache), which only show up in combination.
2. ADR-0048: caching and contract changes. Write it with these points:
- Caching: every response carries
Cache-Control; the default isno-store; a route that wants caching states a lifetime for browsers and for shared caches, and the staleness it accepts. - Protocol: HTTP/1.1 and 1.0 are served; anything ambiguous is refused.
- Contract:
contract/api.jsonis compared with the last release on every release check; a breaking change needs a new minor version before 1.0, a major one after; released contracts are kept incontract/released/. - Removal: deprecate in one release (with the replacement shipped), remove in the next minor, never in a patch.
3. Defaults are decisions someone else made. The proxy's HTTP version, the cache's lifetime, the meaning of no-cache: each was chosen by someone who didn't know TiffinBox. Writing your own decision into every response is how you take it back.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| a menu change that must show at once | up to 5 s of the old menu at the edge | stage 54 (caching: purges and versioned keys) |
| a cache keyed only by URL in front of personal responses | someone else's order, if one were cacheable | stage 51 (security: authorisation on every order) |
| requests have no contract (line-based bodies) | changes to request formats aren't checked | stage 49 (Spring Web: typed request bodies) |
| clients don't learn about deprecations | removals surprise them | stage 49 (Spring Web: Deprecation and Sunset headers from the contract) |
Evidence
exp/46/edge-http10.out,edge-http10-after.out,edge-before.out,edge-after.out.exp/46/contract-diff.out,order-size.out;contract/released/0.2.0.json.tools/contract_diff.pyin the release check.- Core 52, server 54 and tbx 21 tests; the golden master re-recorded with only the intended changes; the release check passes.
- ADR-0048 is committed.
Carry forward
- A caching decision for every new route.
tools/contract_diff.pybefore every release; a released contract saved with each one.exp/46/edge.shwhenever something will sit between TiffinBox and its clients.