Limits that hold by construction
If a limit has to be checked in two places before a lock, two writers can each pass it. Prefer a limit that holds by construction, such as “at most N references per field”, over a computed total that has to be re-checked.
Read the lesson →Re-check eligibility at write time
When a job is queued from a snapshot of “who is eligible”, re-check eligibility at the moment you write the money row, not only when you build the queue.
Read the lesson →Put the status check inside the UPDATE
A status check followed by an update by ID is a race; put the status condition into the update's where so the database enforces it at write time.
List allowed statuses for every new path
Every new “who may do X” path widens the state machine; list the allowed source statuses explicitly, and where the database cannot enforce “only one pending”, serialise the check with a row lock.
Read the lesson →Make a column optional without breakage
Make a column optional only after no running code reads it; until then give it a default. And make “fill if empty” writes conditional in the database, not in the browser.
Read the lesson →Restate the condition in every destructive write
Every destructive or claiming SQL statement should restate the condition it was decided on (still trashed, still the slot I read, still the current run) in its own WHERE clause, because the read that justified it is already stale.
Read the lesson →Idempotency keys need an order of checks
An idempotency contract needs an order of checks, not just a list of rules. Compare the request fingerprint before looking at the in-flight status, so a changed request never waits behind, or replays, a different one.
Read the lesson →Pin the target before the first await
In a loop that awaits network calls, any reactive “current” value can change between iterations. Capture the target before the first await and pass it down, or the user's navigation will redirect your writes.
Read the lesson →Lock the rows before removing the last owner
“Refuse to remove the last owner” is a concurrency rule, not an if: unless the rows you count are locked in the same transaction as the delete, two admins can each remove “the other” owner at the same moment.