Pessimistic and optimistic locking, and how MVCC lets readers and writers work without blocking each other.
When many users change the same data, the database needs a rule to keep them from stepping on each other. There are two big families of rules: locking and MVCC.
Think of one row, a bank balance of 100. Two users both want to add money. If both read 100 and each write their own total, one update is lost. We need a way to make them take turns, or to notice the clash.
"Lock first, work second." A transaction takes a lock on the row before it reads it for update. Anyone else who wants the same row waits until the lock is released at commit or rollback. A shared lock allows many readers. An exclusive lock allows one writer and blocks everyone else.
BEGIN; SELECT balance FROM accounts WHERE id = 7 FOR UPDATE; -- others wait here UPDATE accounts SET balance = balance + 10 WHERE id = 7; COMMIT; -- lock released
"Work first, check at the end." Each row has a version number (or a timestamp). You read the row with its version, do your work, then write only if the version is still the same. If another user got there first, zero rows change and you retry.
UPDATE accounts SET balance = 110, version = version + 1 WHERE id = 7 AND version = 1; -- 0 rows changed? someone beat you, retry
Instead of one value per row, the database keeps versions. A write creates a new version rather than overwriting. Each transaction sees a snapshot: the versions committed before it started (or before its statement began, depending on the level). Readers read old versions while writers add new ones, so neither waits for the other.
| Pessimistic | Optimistic | |
|---|---|---|
| Idea | Lock first, so nobody can clash | Check at the end, and retry on a clash |
| Lock held during the work | Yes | No |
| Good when | Clashes are common, work is short, correctness is critical | Clashes are rare, work is long (like a user filling a form) |
| Bad when | Locks are held too long, or lock order differs (deadlocks) | Clashes are frequent, so retries pile up |
| Cost | Waiting | Wasted work and retries |
| Lock | Who can share it | Used for |
|---|---|---|
| Shared (read) lock | Many readers at once, no writer | Reading data that must not change meanwhile |
| Exclusive (write) lock | One holder, nobody else | Changing a row |
| Row lock vs table lock | Row locks are fine-grained, table locks block everything | Prefer row locks. Table locks are for maintenance |
| Range or gap lock | Protects a range of keys | Stopping phantoms in some databases |
| Version | Value | Written by | Committed at | This reader |
|---|---|---|---|---|
| v1 | 100 | T10 | t = 12 | older version, skipped |
| v2 | 110 | T20 | t = 24 | reads this one |
| v3 | 130 | T30 | t = 36 | not committed yet, invisible |
A simplified teaching model with made-up times and values. Real databases use transaction ids and visibility rules, and clean up old versions in the background.
| Term | Meaning in easy words |
|---|---|
| Lock | A flag on data that makes others wait or stay out |
| Shared lock | A read lock that many transactions can hold together |
| Exclusive lock | A write lock that only one transaction can hold |
| Pessimistic locking | Lock first, because a clash is expected |
| Optimistic locking | Do not lock, check for a clash when saving |
| Version column | A number that goes up on every change, used to detect clashes |
| MVCC | Keeping several versions of each row, so readers and writers do not block each other |
| Snapshot | The set of row versions a transaction can see |
| Deadlock | Two transactions each wait for a lock the other holds |
| Vacuum / purge | Background cleanup of row versions nobody can see any more |
| Lock timeout | How long a transaction waits for a lock before giving up |
| Choice | We gain | We lose |
|---|---|---|
| Pessimistic lock | No lost updates, simple to reason about | Waiting, deadlock risk, lower concurrency |
| Optimistic check | No lock held, great for rare clashes | Retries and wasted work under heavy contention |
| MVCC reads | Readers never wait for writers | Extra storage and cleanup work |
| Row-level locks | Fine-grained, many users in parallel | More lock bookkeeping |
| Table-level locks | Very simple | Everyone else waits |
| Long transactions | Bigger all-or-nothing units | Old versions pile up, locks held long |