All lessons Leer en español

Security in depth · Unit 20 · Lesson 25 of 27

Check and update must agree

Read a two-worker timeline and protect the last available seat.

4 minreadyShort lesson

Helpful before thisWeb applications

See all lessons in this topic

After this lesson you can

  • State a capacity invariant and identify the transition that must enforce it.

Two individually reasonable decisions can create an impossible combined result.

Name the rule that must survive

An invariant is a condition that must remain true through every allowed state transition. For a workshop with capacity one, the rule is: no more than one active reservation. Checking availability is useful only if the later reservation preserves that rule despite concurrent work.

Assume two application workers share one database. Both callers are authorized, and each requests one seat. Neither is repeating an earlier operation. This is a concurrency problem even when every input is valid and every person behaves normally.

Read condition → Concurrent change → Protected transitionRead conditionConcurrent changeProtected transition
A previously read condition may change before commitment. The protected transition is the required correction; the supplied T3 timeline does not have that protection.

Fictional timeline

  • T1: Worker A reads zero reservations and concludes one seat remains.
  • T2: Before A commits, worker B also reads zero and reaches the same conclusion.
  • T3: A commits one reservation. B commits another based on its earlier observation. The final count is two for a capacity of one.

Protect the transition

T1 and T2 can both be accurate at their observation times. T3 demonstrates the violated invariant in this supplied record. Rechecking only in the browser cannot coordinate the workers.

The design needs an atomic conditional transition, appropriate database constraints, or transaction isolation and locking that actually preserve this capacity rule. A transaction label alone does not establish that guarantee. A lock confined to one worker would leave the other outside its protection.

Define the losing outcome: at most one attempt commits, while the other receives unavailable or a controlled retry decision based on current state. Handle conflicts deliberately instead of showing success before a reservation commits.

Review deliverable: Name the invariant and show which shared boundary enforces it when observations overlap.

Terms you met

Invariant

Check yourself

No timer. No penalties. Read the explanation and try again whenever you like.

  1. Which requirement addresses T1-T3 across both workers?

    Show the answer

    Correct answer: Coordinate the availability decision and reservation commit so the one-seat invariant cannot be violated. The protection must cover both workers and the whole relevant transition; the losing attempt needs a defined outcome.

Try it

  • WriteWrite a four-line review: capacity invariant, the conflicting observations, the protected transition, and the outcome the second caller should receive.
References