Why Concurrency Control Is Needed

A plain-language look at why databases need concurrency control, the four classic anomalies it prevents, and how isolation levels trade strictness for speed.

Concept

Concurrency control is the set of rules a DBMS follows so that many transactions can run at the same time without corrupting the database. Running transactions strictly one after another is safe, but far too slow once hundreds of users touch the same tables at once.

The goal in one line: let transactions overlap in time, while guaranteeing the final result is the same as some one-at-a-time order of those same transactions.

Anomalies uncontrolled concurrency causes

AnomalyWhat goes wrong
Lost updateTwo transactions read the same value, both compute a new value, and the second write silently overwrites the first.
Dirty readA transaction reads a value written by another transaction that has not committed yet, and that write is later rolled back.
Unrepeatable readA transaction reads the same row twice and gets two different values because another transaction updated it in between.
Phantom readA transaction re-runs the same query and a new row appears or an old one disappears because another transaction inserted or deleted rows in between.

Lost update, worked example

Account balance = 1000

T1: read balance          -> 1000
T2: read balance          -> 1000
T1: balance = balance+200 -> writes 1200
T2: balance = balance-300 -> writes 700

Final balance = 700

Correct answer, if T1 then T2 had run one after another: 1000+200-300 = 900
900 was never written. T1's update was lost.

Why concurrency control matters

  • Throughput. Letting independent transactions overlap uses the CPU, disk and network far better than forcing a queue.
  • Isolation. It is the "I" in ACID: each transaction must behave as if it were the only one running.
  • Correctness without giving up parallelism. The DBMS decides, through locking or ordering, which overlaps are safe and which are not, instead of leaving it to chance.

Important terminology

TermMeaning
TransactionA sequence of read and write operations treated as one logical unit of work.
ScheduleThe actual order in which operations from one or more transactions execute.
Serial scheduleA schedule where transactions run one after another with no interleaving.
Concurrent scheduleA schedule where operations from different transactions are interleaved.
Serializable scheduleA concurrent schedule whose result matches some serial schedule of the same transactions.
Isolation levelA DBMS setting that trades strictness of isolation for performance, by permitting some anomalies.

Isolation levels and what they still allow

Isolation levelDirty readUnrepeatable readPhantom read
Read UncommittedPossiblePossiblePossible
Read CommittedPreventedPossiblePossible
Repeatable ReadPreventedPreventedPossible
SerializablePreventedPreventedPrevented

Every level below Serializable is a deliberate trade: it accepts a named anomaly in exchange for less locking and higher throughput.

Common mistakes

  • Treating isolation as all-or-nothing. Real systems offer levels, and most applications run below Serializable on purpose.
  • Confusing atomicity with isolation. Atomicity is about a transaction being all-or-nothing on failure; isolation is about transactions not seeing each other's half-finished work.
  • Assuming more concurrency is always better. Past a point, extra interleaving only adds anomaly risk without a real throughput gain.

Exam and interview questions

  1. Define concurrency control and state why serial execution alone is not acceptable in practice.
  2. Illustrate the lost update problem with a two-transaction example.
  3. List the four classic concurrency anomalies and describe each in one line.
  4. Which isolation level prevents dirty reads but still allows phantom reads?
  5. Explain the difference between a serial schedule and a serializable schedule.

Practice

  1. Write a two-transaction interleaving that produces a dirty read, then mark the exact point where the anomaly happens.
  2. Take a ticket-booking system where two users book the last seat at the same time. Name the anomaly this risks and the isolation level that would prevent it.
  3. Explain in two sentences why Serializable isolation, despite being the safest, is not always the default in production databases.

Conclusion

Concurrency control exists to let a DBMS run many transactions in parallel while still promising the one guarantee applications depend on: the outcome is exactly as if those transactions had happened one at a time, in some order.

Useful resources

Hand picked references for this topic
Written by Lorens Mishra

Software Engineer Notes Management System Administrator

Continue reading

All DBMS notes →

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.