← Back to News Stack
Engineering3 min read

Spray Bottle Check Valves and Idempotency Keys: A Reliability Analogy

A spray bottle explainer is trending on Hacker News. Its one-way valves map loosely onto idempotency keys, ordered-write guards and circuit breakers, with limits worth knowing before you lean on the analogy.

Cutaway illustration of a spray bottle showing the trigger, piston, spring and two one-way valves inside the pump body

Mechanical Pencil published an interactive, illustrated walkthrough of how a spray bottle works, and it is climbing Hacker News. You see the trigger, piston, spring and two one-way valves move apart and back together, so you can watch why every pull pushes liquid out and none of it slides back into the bottle. I could not verify the page beyond that description, and its URL looks more like a product path than an article, so treat the details as the author's claims.

Explaining a spray bottle isn't new. An older write-up on check valves covered the same mechanism years ago. What the new page adds is interactivity: you can step the mechanism rather than read about it. That is a plausible reason it is getting attention, though I haven't read the thread closely enough to say what commenters are actually praising.

What the valves do

Two valves do one job each: flow goes this way, never that way. The piston and spring form a bounded pump that moves a fixed volume per stroke, then resets. Nothing inside tracks whether the last pull succeeded.

The analogy to software is loose, and it helps to say where. A fixed stroke volume is a capacity limit, not backpressure: backpressure is a feedback signal that tells the producer to slow down, and a spray bottle just drops extra effort. A check valve also enforces direction, which is not idempotency. Idempotency means a repeated operation has the same effect as one, and an idempotency key usually returns the stored result of the first call rather than "closing". Nor is the bottle free of state: the spring, the liquid level and whether it is primed all matter.

Where the comparison is useful

  • Payment retries. If a gateway times out and the client retries, something must stop the second request from moving money again. An idempotency key with a unique constraint on the payments table does this: the retry hits the constraint, and the service returns the first attempt's stored response.
  • Ordered writes in market data. A late tick from a replayed feed must not overwrite a newer price. Comparing a sequence number against the last applied one, and dropping anything older, behaves like a check valve on the write path.
  • Circuit breakers. This is the weakest match. A breaker such as Resilience4j's is a stateful controller that opens and closes based on failure rates, the opposite of a stateless valve. It earns a place here only because it also stops flow into a failing dependency.

Where I'd push back

The usual contrast is between simple guards and orchestration such as sagas or workflow engines. I don't think they compete. Valves solve local problems, like duplicates and ordering at one write. Orchestration solves coordination across systems, including partial failure, which a valve cannot handle. The spray bottle analogy breaks down exactly there.

The state point matters too. The bottle is easy to explain because its state is visible and small. Production systems hide state in caches, retry queues and half-finished jobs, so a guard is only as good as your picture of where it sits.

What to take from it

Pick one write path, such as a payout or ledger entry, and list where a duplicate or out-of-order message could get through. Put a guard at each point: a unique constraint on the idempotency key, a sequence check, or a bounded executor queue in front of a slow dependency.

Be careful with "fail closed". For a ledger write, rejecting an unverifiable request is usually right. For a payout, the same default can block legitimate money movement, so decide per path whether a failed check means reject, queue for review or allow with an alert. Write that choice down before the incident, not during it.