Concurrency Control

Measurement
Introduced by
Jim Gray and David Reed
Year
1978
Type
Database coordination mechanism
Family
Transaction Control & Data Consistency

What does it do?

Concurrency control is the set of mechanisms that coordinate concurrent transactions accessing shared data so that the database is not corrupted.

Its central correctness criterion is serializability: the outcome of interleaved transactions must equal the outcome of some serial execution of the same transactions. The formal theory of this problem is treated in Papadimitriou (1986).

The intuition behind the method

In a single-user system, transactions execute one after another with no interference. In multi-user databases, transactions may interleave, creating opportunities for conflicts: one transaction may overwrite another's changes, or read partially-committed data. Concurrency control mechanisms (locks, versioning, ordering) are designed to prevent these anomalies by ensuring that transactions either do not conflict or execute in a consistent order. Locks prevent conflicting access directly. Versioning avoids many conflicts by giving readers an earlier consistent version, but the guarantee it provides depends on the isolation level the system implements.

History

David P. Reed's 1978 MIT dissertation addressed naming and synchronization in decentralized computer systems. Jim Gray's 1981 paper 'The transaction concept: Virtues and limitations' examines the transaction concept itself. Two-phase locking is the classic lock-based protocol, and multiversion concurrency control (MVCC) is a widely used versioning approach.

When to use it

Use concurrency control whenever several transactions can read or write the same rows at the same time and the application depends on the database preserving isolation, for example for account balances or stock counts. Lock-based protocols suit workloads in which conflicts are frequent and aborted work is costly, because conflicts are handled before data is changed. MVCC suits read-heavy workloads in which readers should not wait for writers. It is unnecessary for a single-user database, or for workloads whose transactions never touch shared data. The protocol and isolation level should follow from the consistency the application actually needs, since a stronger level is not automatically the better choice. In operation, track lock wait times, deadlock counts and transaction abort rates and, under MVCC, the growth of stored versions. Rising waits or aborts can indicate that transactions hold locks for too long or that the chosen isolation level is stronger than the application requires.

Limitations and considerations

  • In lock-based protocols, waiting for conflicting locks reduces concurrency and throughput when many transactions contend for the same items
  • Two-phase locking can produce deadlocks, which require detection, rollback of one transaction, and a retry
  • MVCC keeps several versions of updated data, which adds storage use and requires cleanup of versions no active transaction can read
  • Snapshot-based MVCC does not by itself rule out every serializability anomaly, such as write skew, so the isolation guarantee the system actually provides has to be checked against the application's needs

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Serializable execution guarantees that interleaved transactions yield a result equivalent to some serial order, which rules out anomalies such as lost updates and dirty reads
  • Lock-based protocols detect conflicts before a transaction changes data, so work is not discarded because of a conflict found only at commit
  • Under MVCC, readers read a consistent earlier version and do not block writers, which keeps read-heavy workloads responsive

Applications

  • Funds transfers between two accounts, where row locks ensure that two concurrent transfers involving the same account do not overwrite each other's balance update
  • Reservation of the last available seat, where serializable execution ensures that only one of two concurrent requests succeeds
  • Stock decrements during checkout, where concurrent orders for the same item are serialized so that stock does not go negative through lost updates
  • Product catalogue browsing during a price update, where MVCC lets readers see the previously committed prices without waiting for the writer

How it works

How it works

1

Before reading or writing a data item, a lock-based transaction requests a lock of the matching type: shared for reads, exclusive for writes. Under MVCC, a read instead takes a consistent version of the item, and a write creates a new version rather than overwriting the old one.

2

The scheduler checks whether a lock already held by another transaction conflicts with the request, for example an exclusive lock on the same item. Two shared locks on an item are compatible; an exclusive lock is incompatible with any other lock on that item. Under MVCC, the check is whether another transaction has already written a newer version of the item since this transaction began. For a whole schedule, conflict-serializability can be tested with a precedence graph: each transaction is a node, and an edge runs from Ti to Tj when an operation of Ti precedes a conflicting operation of Tj on the same item. The schedule is conflict-serializable exactly when this graph has no cycle.

3

If a conflict exists, a lock-based scheduler makes the requesting transaction wait, and a cycle of mutually waiting transactions is a deadlock that must be detected. Under MVCC, a conflicting write is typically rejected, and the transaction is rolled back and may be retried.

4

The transaction proceeds once all required locks are granted or its version check succeeds. Under two-phase locking, locks are acquired in a growing phase, and once any lock has been released the transaction requests no new locks.

5

At commit or abort, the transaction releases its locks, or its new versions are made visible or discarded, so that waiting transactions can continue. Two-phase locking is designed so that the resulting schedule is conflict-serializable. The MVCC check described above is a snapshot-isolation-style write check: it prevents lost updates on the same item, but it does not by itself guarantee serializability, because anomalies such as write skew can occur. Serializable snapshot isolation adds further checks to close this gap, so the isolation level a given system actually provides should be confirmed before relying on it.

Common pitfalls

  • Holding locks while waiting for user input or unrelated work extends other transactions' waits and increases contention
  • Omitting deadlock handling or retry logic, so that a transaction chosen as deadlock victim fails without recovery
  • Choosing an isolation level weaker than the application's logic requires, which allows the anomalies that concurrency control is meant to prevent
  • Treating a snapshot-based (MVCC) result as fully serializable without checking which anomalies the chosen isolation level permits, such as write skew

Frequently asked questions

What is a deadlock and how is it resolved?

A deadlock occurs when two transactions wait for each other: Transaction A holds lock X and waits for Y; Transaction B holds Y and waits for X. Databases detect deadlocks and rollback one transaction, allowing the other to proceed.

What is the difference between optimistic and pessimistic locking?

Pessimistic locking (two-phase locking) acquires locks before accessing data, so conflicts are prevented before they occur. Optimistic approaches instead let transactions proceed without locks and check for conflicts later, typically at commit. MVCC is not purely optimistic: readers see an earlier committed version without taking locks, while writers still need write locks or a write-conflict check, depending on the implementation.

How do isolation levels affect concurrency?

Higher isolation levels (Serializable) prevent more anomalies but can reduce concurrency or increase aborts, depending on the protocol: lock-based systems may make transactions wait, while MVCC systems may roll back conflicting transactions. Lower levels (Read Uncommitted) can improve throughput but allow anomalies. Choose the level based on the consistency the application needs.

Should I use row-level or table-level locking?

As a general rule of thumb, row-level locking allows more transactions to proceed at once on large tables with selective access, but it adds lock-management overhead. Table-level locking is simpler and can suit small tables or full-table operations. The right granularity depends on the database system and the workload.

Related methods

Sources

  1. Gray, J. (1981). The transaction concept: Virtues and limitations. VLDB Endowment, 7(6), 519-539.

    Open publication reference
  2. Reed, D. P. (1978). Naming and synchronization in a decentralized computer system. Ph.D. Dissertation, MIT.

    Open publication reference
  3. Papadimitriou, C. H. (1986). The Theory of Database Concurrency Control. Computer Science Press.

    Open publication reference