Pessimistic Locking
Blocking every other writer up front for the duration of a transaction, betting that collisions are common enough to prevent rather than clean up.
Pessimistic locking takes a lock on a row (or a broader range) the moment a transaction intends to modify it, and holds that lock until the transaction commits or rolls back, so no other transaction can write — sometimes even read — the same data in the meantime. SELECT ... FOR UPDATE in a relational database is the usual entry point: it reads a row and simultaneously claims a lock on it, guaranteeing no other transaction can change it before this one finishes.
This is the opposite bet from Optimistic Locking: rather than letting everyone proceed and catching collisions at commit time, pessimistic locking prevents the collision from ever being possible, at the cost of every other writer queuing behind the lock even when a conflict was never going to happen. It's the right trade when contention on the same rows is genuinely common, or when the cost of a conflict discovered late — after a lot of wasted work, or a customer-visible inconsistency — is much higher than the cost of writers occasionally waiting.
Its characteristic failure mode is the deadlock: transaction A holds a lock on row 1 and waits for row 2, while transaction B holds row 2 and waits for row 1, and neither can ever proceed. Databases detect this pattern and forcibly abort one of the two transactions to break the cycle, which means code taking pessimistic locks has to be written to handle being killed and retried — the same discipline needed anywhere concurrent writers compete, just enforced by the database rather than left to occasionally corrupt data the way an unlocked Race Condition would.
See also6
Optimistic Locking
Letting concurrent writers proceed unchecked and catching conflicts only at commit time, betting that collisions are rare.
Data & Content11 connections
Race Condition
A defect whose occurrence depends on the relative timing of concurrent operations.
Testing & Verification36 connections
ACID
The four guarantees a transactional database makes so concurrent, interrupted work never leaves data half-done.
Data & Content17 connections
Two-Phase Commit
A protocol for committing one transaction across multiple databases atomically, at the cost of blocking if the coordinator dies.
Data & Content10 connections
Deadlock
When two or more processes each wait for something another holds, so none can ever proceed, and the four conditions that must all be true for it to happen.
Computation & Algorithms14 connections
Isolation Level
How much concurrent transactions are allowed to see of each other's unfinished work, a dial between correctness and speed that most databases set lower than people assume.
Data & Content9 connections
Linked from3
- DeadlockComputation & Algorithms
When two or more processes each wait for something another holds, so none can ever proceed, and the four conditions that must all be true for it to happen.
- Isolation LevelData & Content
How much concurrent transactions are allowed to see of each other's unfinished work, a dial between correctness and speed that most databases set lower than people assume.
- Optimistic LockingData & Content
Letting concurrent writers proceed unchecked and catching conflicts only at commit time, betting that collisions are rare.