What Is Concurrency Control?
Concurrency control is the collection of techniques a database uses to let multiple transactions run at the same time while keeping the result correct. Pessimistic concurrency control prevents conflicts upfront with locks, as in Two-Phase Locking, while optimistic concurrency control lets transactions proceed and checks for conflicts just before commit, aborting on a conflict.
Concurrency control is the collection of techniques a database uses to manage multiple transactions running at the same time, ensuring the result is always correct (serializable, or whatever isolation level was requested) despite the real interleaving happening underneath.
Why it's needed
Running transactions one at a time (serially) would always be safe, but throws away all the performance benefit of a database serving many users simultaneously. Concurrency control is what lets a database have both: real concurrency for performance, and a guarantee that the outcome is never worse than if the transactions had somehow run one at a time.
The two broad families of approach
- Pessimistic concurrency control: assumes conflicts are likely, and prevents them upfront using locks. A transaction must acquire a lock on data before touching it, and other transactions wanting the same data have to wait. Two-Phase Locking is the classic protocol here.
- Optimistic concurrency control: assumes conflicts are rare, and lets transactions proceed without locking anything upfront, only checking for conflicts right before commit. If a conflict is detected at that point, the transaction is aborted and typically retried. This avoids locking overhead entirely when conflicts are genuinely uncommon.
A worked example: pessimistic locking in action
T1: BEGIN
T1: acquires lock on row A, then reads and updates it
T2: BEGIN
T2: tries to acquire a lock on row A, must WAIT since T1 holds it
T1: COMMIT, releases lock on A
T2: now acquires the lock on A, proceeds
T2 is forced to wait rather than reading a potentially stale value or racing T1 to write, guaranteeing no interference at the cost of T2 sitting idle until T1 finishes.
A worked example: optimistic concurrency in action
T1: reads row A (version 5), makes changes locally, no lock taken
T2: reads row A (version 5), makes changes locally, no lock taken
T1: COMMIT, checks version is still 5, succeeds, row A is now version 6
T2: COMMIT, checks version, finds it's now 6 (not the 5 T2 originally read), COMMIT is rejected, T2 must retry
Neither transaction blocked the other while working, but T2's late conflict is caught at commit time and it has to redo its work against the updated data.
Where this matters in real systems
Traditional relational databases handling write-heavy, high-contention workloads (like a ticket-booking system where many users might grab the same seat) typically lean on pessimistic locking, since conflicts there are genuinely frequent and better prevented upfront. Systems with mostly independent writes and rare actual conflicts (many collaborative editing or caching-heavy web applications) often prefer optimistic concurrency control, since paying locking overhead constantly for rarely-occurring conflicts would be wasteful.
What interviewers actually ask about concurrency control
- "What is concurrency control?" The techniques a database uses to let multiple transactions run simultaneously while still guaranteeing a correct, serializable outcome.
- "What's the difference between pessimistic and optimistic concurrency control?" Pessimistic locks data upfront to prevent conflicts before they happen; optimistic lets transactions proceed freely and only checks for conflicts at commit time, retrying if one occurred.
- "When would you choose optimistic over pessimistic concurrency control?" When conflicts are genuinely rare, avoiding constant locking overhead is worth occasionally having to retry a transaction.
- "What happens when an optimistic concurrency check fails at commit time?" The transaction is rejected and typically retried from scratch against the now-current data.
Key takeaways
- Concurrency control lets multiple transactions run simultaneously while still guaranteeing a correct result.
- Pessimistic concurrency control uses locks to prevent conflicts upfront, at the cost of transactions waiting.
- Optimistic concurrency control lets transactions proceed freely and checks for conflicts only at commit time, retrying on failure.
- The right choice depends on how frequent real conflicts actually are for a given workload.
More in Transaction Processing
Browse all DBMS topics
Log in to save which topics you've read across every subject.
Frequently asked questions
What is concurrency control?
The techniques a database uses to let multiple transactions run simultaneously while still guaranteeing a correct, serializable outcome.
What's the difference between pessimistic and optimistic concurrency control?
Pessimistic locks data upfront to prevent conflicts before they happen; optimistic lets transactions proceed freely and only checks for conflicts at commit time, retrying if one occurred.
When would you choose optimistic over pessimistic concurrency control?
When conflicts are genuinely rare, avoiding constant locking overhead is worth occasionally having to retry a transaction.
What happens when an optimistic concurrency check fails at commit time?
The transaction is rejected and typically retried from scratch against the now-current data.