Database locks and concurrent updates
medium 4 minA row lock coordinates conflicting operations on the same records.
Video lesson · Chapter 1 of 5
Last copy at the lending desk
Can you lock a booking that does not exist?
1:14Reel
Theory
Read or listen first. About 1 min.
What it is
A row lock has a concrete target. Two clients looking up an unbooked time slot may both get an empty result, leaving them with no existing booking row to lock.
Can you lock a missing booking?
Why it exists
Representing the slot as a unique key gives the database a conflict it can detect even when two inserts arrive together.
Words to know
- Uniqueness constraint
- A database rule that prevents two rows from committing the same constrained key.
How it works
Step through the idea, or press Play.
Look for a booking. Both transactions see an empty result.
Understand it
Worked example
Database locks and concurrent updates in practice
Two clients insert a booking for a slot whose row does not yet exist. Locking the empty search result locks no booking row. Use a unique slot constraint, a durable slot row or an appropriate serializable transaction.
Related case
When two correct updates break a rule
Two technicians are on call. Each may leave only if another remains. Two transactions see two technicians and each takes a different person off duty.
Lock the shared shift before reading or retry the serializable transaction.