AlgoViz

Database locks and concurrent updates

medium 4 min

A row lock coordinates conflicting operations on the same records.

Video lesson · Chapter 1 of 5

Last copy at the lending desk

0:00 / 3:21

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?

Client A09:00 slotClient BFind → emptyFind → emptyInsert AInsert BTwo bookings can occupy the same slot.
Both transactions find no booking. In this row-lock example the empty result gives them no row to lock, so both can insert.

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.

Step 1 of 3

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.

Two names on the night roster
The night-shift rosterAdaon dutyOn call2 peopleBoon dutyTwo people cover the shift

Lock the shared shift before reading or retry the serializable transaction.

Sources