Reliability, Idempotency, and Race Conditions
Make sure one intended action happens only once, even when requests are retried or arrive at the same moment, by using request keys, database constraints, and transactions.
Content date: October 15, 2026.
Plain English
Software retries when a result goes missing or a call fails briefly. Idempotency means that repeating the same request does not cause a second effect. A race happens when two requests read or change the same data in an unsafe order. Disabling a Submit button, or checking a flag in the app's memory, cannot protect a rule once there are several browsers, several servers, or a restart.
Use this when
Use it before any request can reserve something scarce, charge money, grant access, import records, or start background work. Use it again whenever you add automatic retries or a second worker.
If you are just starting
Write the rule in one sentence first, such as "each class slot has at most one booking." Give every request a unique key, and store it with the booking and what the request asked for. Reject a reused key that comes with a different request. If the same request arrives again, return the original result. Let the database enforce the rule with a unique constraint, and wrap steps that must succeed together in a transaction (all commit, or none do). Identical details alone do not prove a duplicate, since two people can legitimately book the same kind of slot, so identify intent with a key.
When your project grows
Decide how long keys are kept, who a key belongs to, and what happens when two requests with the same key arrive at once. Use your real database's constraints, transactions, and locking, or a version number that an update must match. Save any follow-up job in the same transaction as the booking. For outside services such as payments, use the provider's own idempotency keys and reconcile against its records. Limit retries by count and total time, retry only failures that can pass on a second try, and add backoff and jitter (waits that grow and are randomized so clients don't retry in lockstep). A local transaction cannot roll back a charge or email that already left, so an unclear outcome needs a check with the provider or a planned undo step.
Worked example
A yoga studio has one spot left in the 9 a.m. class. Alice and Bob tap Book at the same moment. In the practice code, two SQLite connections start together, and the unique slot constraint lets one booking through and returns a conflict to the other. Alice's phone retries with the same key and gets her original booking ID back. A simulated failure while saving the confirmation job rolls back the booking too. All of this happens in one local database, so it says nothing about a payment provider or servers spread across locations.
Practice
Reproduce and repair a booking race
- Download the practice files, run the tests, and write down what happened for the duplicate, competing, and restarted requests.
- Find where the booking and its pending job are saved together, and where a failure undoes both.
- In a throwaway copy, remove the unique slot constraint and watch the competing-booking test fail. Then restore it and rerun.
- Write a short retry contract: attempts allowed, how long you keep keys, and which effects (a payment, an email) need the outside provider's own protection.
Deliverable: A one-page booking rule and failure table with what you observed for races, rollbacks, and replays, the constraint repair, and your retry contract.
Common mistakes to watch for
- Code that checks whether a slot is free and then writes lets two customers book the same 9 a.m. class.
- A new key on every retry means a slow connection charges the customer twice.
- The same key quietly accepts a different request, so a retry for Tuesday returns Monday's booking.
- The booking saves but its confirmation job is lost, so no email goes out and nothing records the miss.
Prompting
A weak prompt
Disable the Submit button after one click and retry on every error so each booking only happens once.
A better prompt
Our rule: [one sentence, e.g. each class slot has at most one booking]. Implement per-user request keys, reject a reused key with a different request, add a database unique constraint, and save the booking and its confirmation job in one transaction. Test with two separate connections, a restart and replay, a conflicting request, and failures before and after commit. Define bounded retries separately, and tell me exactly which database and failures you tested.
Without the rule living in the database, two customers can book the same slot and nobody finds out until both arrive.
Check before you move on
- The booking rule is written as one clear sentence.
- Each request key belongs to one user and one intent.
- A reused key with a different request is rejected.
- Two competing connections were tested.
- The booking and its job are saved in one transaction.
You are done when
- With the unique constraint removed in a practice copy, the tests show a double booking, and restoring it stops the double booking.
- You can explain the difference between one local effect and a guarantee that something happens exactly once across every system involved.
Questions to think about
A studio disables its Submit button after the first click, yet two people still book the same last spot. How?
The button only affects one browser. Other devices, retries, and servers can still send requests at once, so the rule must be enforced where the data is saved.
A request arrives with Alice's earlier key, but now asks for the 10 a.m. slot instead of 9 a.m. What should the system do?
Reject it as a conflict. A key identifies one specific request, and a different request under the same key is almost certainly a bug.
Booking code rolls back its database transaction after a failure, but a confirmation email already went out. Is the email undone?
No. A rollback only covers the database. The email needs its own duplicate protection or a planned correction message.