Concurrency Control

Concurrency Control Definition
Concurrency control is a set of database mechanisms that prevent conflicts when multiple transactions access or modify the same data at the same time. For example, two users might try to update the same bank balance, reserve the same seat, or change the same inventory count simultaneously. Without concurrency control, one update could overwrite another or leave the data in an incorrect state.
Concurrency control helps keep data accurate, consistent, and reliable by coordinating how transactions are processed.
How Concurrency Control Works
Concurrency control manages how multiple transactions interact when they access the same data at the same time. The database checks each transaction against others that are still active and decides whether the operation can continue safely. If two transactions conflict, the database may pause one transaction, reject a change, or force a transaction to roll back and retry. If no conflict is detected, the transaction continues and eventually commits its changes.
Databases often use isolation levels to balance data consistency, concurrency, and performance. Higher isolation levels provide stronger protection against conflicts, while lower levels allow more simultaneous activity with fewer restrictions.
Common Concurrency Control Problems
- Dirty read: Uncommitted data appears in another transaction’s results. If the first change rolls back, the other transaction has used data that no longer applies.
- Lost update: One update replaces another update made to the same value. This leaves the database with one result instead of both intended changes.
- Non-repeatable read: The same record shows different values within one transaction because another transaction changed it between reads.
- Phantom read: A repeated search returns a different set of rows because another transaction added or removed matching records.
Common Types of Concurrency Control
- Lock-based concurrency control: A lock limits access to data while a transaction is using it. Other transactions may need to wait until the lock is released.
- Optimistic concurrency control: A transaction runs first, then the database checks it before saving the change. This works well when clashes are rare.
- Multiversion concurrency control (MVCC): The database keeps older and newer versions of data. The database keeps multiple versions of data so transactions can read a consistent snapshot while updates occur in parallel.
- Timestamp ordering: Each transaction gets a timestamp. The database uses it to decide the order of reads and writes.
Limitations of Concurrency Control
- Slower performance: Extra checks can add delays, especially when many transactions use the same data.
- Deadlocks: Two transactions may wait on each other indefinitely until the database aborts or rolls back one of them.
- Added complexity: Large or distributed databases need careful rules to keep transaction handling reliable.
Read More
FAQ
A simple example of concurrency control is an online store with one item left in stock. If two shoppers try to buy it, the database can let one checkout finish first and stop the other order from using the same item. This prevents the store from selling more items than it has.
Data consistency refers to the condition of the stored data. It means the data is accurate, valid, and follows the rules set for the database. Concurrency control is one process that helps preserve this condition when related transactions overlap.
Concurrency control can help prevent some lost updates, but it doesn’t protect against every kind of data loss. Databases still need backups, recovery tools, and safe storage if data is deleted, damaged, or lost for another reason.
