First, the key terms.
- Concurrent transactions
- Operations whose execution overlaps.
- Serializable
- An isolation guarantee equivalent to some serial ordering of committed transactions.
The idea, explained
Two operations can each appear valid in isolation yet conflict when they run together. Stronger isolation can prevent certain anomalies, but may cause a transaction to abort so the application can retry it. The correct retry must rerun the whole transaction using fresh state, and must respect the database’s documented behavior.
Find the shared assumption
Two buyers may both read that one ticket remains. Each decides a purchase is valid. If those checks are not coordinated with the update, both can act on the same capacity.
Choose a database-level control
Depending on the invariant, use a conditional atomic update, uniqueness constraint, locks or suitable isolation. Serializable execution can reject a transaction that would violate its guarantees, so applications must handle retries correctly.
Follow a worked example.
Two schedulers both check that a seat is available. Without a suitable constraint or concurrency strategy, both may try to assign it. A serializable transaction or an atomic conditional write can help enforce the intended invariant, with conflict handling.
Walk it through, one step at a time
- Inventory starts at 1. Two callers each try to buy one.
- An atomic update conditioned on inventory being at least 1 can let only one successful decrement occur. Check the affected-row count.
- If the transaction is retried after a serialization failure, also protect external charging from duplicate side effects.
Where people get stuck
Serializable execution does not mean that requests never fail or that retrying only the final statement is always safe.
Now try it yourself.
1. Check the core idea
What should an application expect when a serializable transaction encounters a conflict?
Reveal the answer & reasoning
It may need to abort and retry the complete transaction. That behavior belongs in the application’s design.
2. Apply what you learned
Would checking inventory in the browser prevent overselling?
Reveal the answer & reasoning
No. Other clients can act simultaneously and client checks can be bypassed. The invariant must be enforced at the trusted state-changing boundary.