Optimistic vs Pessimistic Concurrency, Deadlocks and Transactions
Overview
There are two main ways to share data safely: lock first, or check at the end. This lesson compares them, explains the deadlock trap locks can cause, and introduces transactions.
Detailed Notes
What is optimistic vs pessimistic concurrency
Pessimistic concurrency assumes conflicts are likely, so it locks first and holds the lock until done. Optimistic concurrency assumes conflicts are rare, works without locks and checks at the end whether someone else changed the data, retrying if so.
The problem it solves
Two editors open the same article at version 7. Editor A saves a new headline. A minute later Editor B saves a changed first paragraph. If the system keeps whatever arrives last, A's headline change vanishes silently (a lost update).
- Pessimistic fix: lock the article when A opens it, so B must wait.
- Optimistic fix: let both edit, but when B saves, notice the article is now version 8, not 7, and ask B to merge.
How it works
Pessimistic: lock the row before changing it (in SQL, SELECT ... FOR UPDATE inside a transaction, released on COMMIT). Simple and safe when conflicts are frequent (a hot row, a scarce item), but others wait and long locks hurt throughput.
Optimistic: read the data with a version number, and at write time update only if the version is unchanged (compare-and-swap): UPDATE articles SET title = 'New', version = version + 1 WHERE id = 42 AND version = 7. One row affected means success; zero rows means someone else changed it, so retry.
No waiting, ideal for read-heavy, low-conflict data. Under heavy conflict everyone keeps retrying, so use retry limits with backoff (see the Resilience chapter).
| Aspect | Pessimistic | Optimistic |
|---|---|---|
| Assumption | Conflicts are likely | Conflicts are rare |
| Mechanism | Lock first | Check version at write |
| Best for | Hot rows, scarce items | Read-heavy, low contention |
| Failure mode | Deadlocks, stuck locks | Retry storms under conflict |
Deadlocks. A deadlock is when tasks each hold a resource and wait for one held by another, so none can proceed: Task 1 holds Account A and wants B, while Task 2 holds B and wants A, so both wait forever.
Ways to deal with it: lock ordering (always lock the lowest account id first, so no cycle can form), timeouts (give up and retry), detection (the database aborts one transaction as the "victim"; applications must retry), and fewer, shorter locks.
Transactions. A transaction groups operations into one all-or-nothing unit, and with the isolation from ACID (covered in the Databases chapter) concurrent transactions do not see each other's half-finished work. Transactions spanning several machines are hard and slow, so designs prefer to keep a transaction within one partition.
A simple example
Pessimistic is reserving a meeting room. Optimistic is editing a shared document offline and being told "the file changed, please merge". Deadlock is two people in a narrow corridor, each waiting for the other to back out.
System design example
A booking site uses optimistic updates with up to 3 retries on listing calendars, since guests rarely collide. A flash-sale counter hit by thousands of buyers on one row uses an atomic conditional update instead. Money transfers always lock the lower account id first, so two opposite transfers never deadlock.
Summary
- Pessimistic locks first; optimistic checks a version at write time.
- Choose by conflict rate: optimistic for rare conflicts, pessimistic for hot data.
- Deadlocks come from circular waits; prevent with consistent lock ordering and short locks.
- Transactions give all-or-nothing updates; keep them short.
Architecture Diagram
The write succeeds only if the version is unchanged.
- 1Each editor reads the record and its version.
- 2The app writes with a condition that the version is unchanged.
- 3Zero rows updated means someone else changed it, so retry or merge.
Common Interview Questions
Chapter quiz
Concurrency and Coordination · 4 questions
1.A race condition is:
2.What does a fencing token protect against?
3.Optimistic concurrency control is best when:
4.Which prevents most deadlocks?
0 of 4 answered
Finished this lesson?
0 of 91 lessons done · 0% of the course

