How Backend Engineers Prevent Double-Booking Bugs
For Backend engineers building concurrent systems · Based on SECourses Build Software People Can Trust
// TL;DR
Backend engineers building concurrent systems use this methodology to prevent the classic double-booking and double-sale failures where two actors each see 'success.' The core move is identifying the intended rule — like 'one confirmed booking per room per hour' — and enforcing it in the stored, authoritative state rather than on each screen or cached client copy. You then draw the system boundary, classify every dependency, and apply correctness as a condition of acceptance that eliminates unsafe designs before speed is ever compared. Evidence is stated with its exact scope, so passing one example never gets mistaken for proof under concurrency.
Why do two users both get a confirmation for the same slot?
Because the exclusivity rule was kept on each screen or cached client copy instead of where the records are stored. When two actors each check their own copy, both see 'free,' and both receive a confirmation — violating the intended rule even though every individual request and screen looks successful. This is the single most common concurrency failure, and it's a design flaw, not a testing gap. The fix is architectural: route the decision through one shared check that reads the stored, authoritative state before confirming.
Start every evaluation by stating the core rule precisely. 'One confirmed booking per room per hour' or 'one confirmed sale per unit of inventory' is specific enough to enforce. Then ask the decisive question: is this rule kept in the stored, authoritative state, or only on each individual client? A rule kept only on each screen can be violated simultaneously by multiple actors.
How do I draw the system boundary for a concurrent service?
Draw a dashed line around everything your team controls — your services, modules, and storage. List everything outside it: the payment processor, identity provider, external data sources, notification delivery, file systems, clocks, and networks. Then classify each arrow crossing the boundary. Is it a request, an identity assertion, reference data, or a notification?
This classification isn't academic. Treating an identity assertion like a request would let anyone ask to be signed in as someone else. Treating a notification report like a decision would let a lost message cancel a confirmed booking. For every dependency, name what the user sees when it's unavailable — the boundary identifies responsibility, but it never makes external failures disappear.
How do I compare design alternatives under concurrency?
Apply correctness as a condition of acceptance — a gate, not a score. In the e-commerce example, Design 1 has each checkout screen check its own cache. It violates the 'one confirmed sale per unit' rule, so it's eliminated before speed is ever compared. Design 2 routes every purchase through one shared inventory decision that reads the stored stock. It keeps the rule, so it survives the gate and can then be compared on response time, recoverability, and change effort.
Build a comparison table: one row per quality attribute, one column per design. Correctness first as a gate, then the tradeable attributes for survivors. A design that can produce two confirmations for one resource is out — you don't average its speed into a score and hope.
How do I state evidence honestly for concurrent code?
For every claim like 'this booking logic is correct,' name the check that supports it and write its conditions beside it: how many users, what calendar state, which rooms, which timing scenario. Then state what it did not cover. A test showing one successful purchase does not cover the simultaneous two-buyer scenario — the 'not covered' column is never empty. Passing one example establishes nothing about simultaneous requests.
Use the right representation for each question. A state diagram answers 'what states can this booking be in?' A sequence diagram answers 'what happened first between these two requests?' A dependency graph answers 'who depends on the inventory service?' Picking a flowchart because it's familiar gives a wrong or incomplete answer.
Next step: Take your most concurrency-sensitive feature, write its core rule in one sentence, and verify the rule lives in stored state — not on each client. Then run the four check questions on your analysis before you ship.
// FREQUENTLY ASKED QUESTIONS
Where should I enforce a uniqueness rule in a distributed system?
In the stored, authoritative state that all actors share — never on each individual screen or cached client copy. A rule kept per-client can be violated simultaneously because each actor sees its own stale 'free' view. Route every decision that depends on the rule through one shared check that reads the stored state before confirming, so concurrent requests can't both succeed.
How do I test concurrency correctly?
State the exact conditions each test covers — how many simultaneous actors, what shared state, which timing — and explicitly name what it didn't cover. A single-user success test proves nothing about two buyers racing for the last unit. Your evidence must cover the specific interaction condition you care about, and the 'not covered' column should never be empty.
Why is classifying boundary arrows important for reliability?
Because confusing the kinds of information crossing your boundary causes security and reliability failures. Treating an identity assertion like a request lets anyone sign in as someone else; treating a notification report like a decision lets a lost message cancel a confirmed booking. Classify every arrow as a request, identity assertion, reference data, or notification, and handle each differently.