1. Introduction to Concurrency in DBMS
Concurrency in Database Management Systems (DBMS) refers to the ability of multiple users or transactions to access and modify the database simultaneously without leading to inconsistency or conflicts.
π‘ Example:
- Multiple users booking airline tickets at the same time.
- A banking system handling simultaneous deposits and withdrawals.
Why is Concurrency Management Important?
β
Prevents data inconsistency.
β
Avoids race conditions where multiple transactions compete for the same resource.
β
Ensures correct execution of transactions in a multi-user environment.
2. Problems in Concurrency Control
When multiple transactions execute simultaneously, they can interfere with each other, leading to concurrency issues.
1. Lost Update Problem
- Occurs when two transactions update the same data without proper synchronization, and one update is lost.
β Example:
- Transaction T1 reads
Balance = 5000
. - Transaction T2 also reads
Balance = 5000
. - T1 adds
1000
β New Balance =6000
. - T2 subtracts
500
β New Balance =4500
. - Final balance should be 5500, but T1’s update is lost!
2. Dirty Read Problem
- Occurs when a transaction reads uncommitted data of another transaction.
β Example:
- Transaction T1 updates
Balance = 7000
but hasnβt committed. - Transaction T2 reads this value.
- T1 rolls back, restoring the balance to
5000
. - T2 now holds an invalid value (7000 instead of 5000).
3. Unrepeatable Read Problem
- Occurs when a transaction reads the same data multiple times and gets different values.
β Example:
- T1 reads
Balance = 5000
. - T2 updates
Balance = 6000
and commits. - T1 reads again β Now Balance =
6000
. - Inconsistent results for the same query!
4. Phantom Read Problem
- Occurs when a transaction retrieves a different set of records during repeated reads.
β Example:
- T1 selects all employees with
salary > 50000
. - T2 inserts a new employee with
salary = 60000
. - T1 re-executes the query and sees an extra row!
3. Concurrency Control Techniques
To manage concurrency issues, DBMS implements various concurrency control techniques.
1. Lock-Based Concurrency Control
A lock is a mechanism that prevents multiple transactions from accessing the same resource simultaneously.
Types of Locks
π Shared Lock (S-Lock) β Allows multiple transactions to read but not write.
π Exclusive Lock (X-Lock) β Allows only one transaction to read and write.
β Example of Locking in SQL:
BEGIN TRANSACTION;
SELECT * FROM Accounts WITH (HOLDLOCK); -- Holds the lock until the transaction ends
UPDATE Accounts SET Balance = Balance + 1000 WHERE AccountID = 1;
COMMIT;
Two-Phase Locking Protocol (2PL)
- Phase 1 (Growing Phase): Transactions acquire locks but donβt release them.
- Phase 2 (Shrinking Phase): Transactions release locks but donβt acquire new ones.
β Guarantees serializability, but may cause deadlocks.
β Example:
LOCK TABLE Accounts IN EXCLUSIVE MODE;
2. Timestamp-Based Concurrency Control
- Each transaction gets a unique timestamp based on start time.
- Transactions execute in timestamp order.
π‘ Rules:
β If T1 (earlier) wants to write to an item read by T2 (later), T1 is allowed.
β If T2 (later) wants to write to an item read by T1 (earlier), T2 is rolled back.
β No deadlocks occur, but may lead to frequent rollbacks.
β Example:
ALTER DATABASE mydb SET ALLOW_SNAPSHOT_ISOLATION ON;
3. Optimistic Concurrency Control (OCC)
- Instead of locking, transactions execute without restrictions and validate changes before committing.
- Three phases:
- Read Phase β Transaction reads data without locks.
- Validation Phase β Before commit, check if any conflicts exist.
- Write Phase β If no conflicts, changes are applied.
β Works well when conflicts are rare (e.g., in read-heavy applications).
β Example:
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
4. Multi-Version Concurrency Control (MVCC)
- Instead of locking, multiple versions of a record are maintained.
- Transactions read previous versions instead of waiting for locks.
π‘ Used in: PostgreSQL, MySQL (InnoDB), Oracle.
β Example:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
β Prevents dirty reads while allowing high concurrency.
4. Transaction Isolation Levels
SQL provides isolation levels to balance concurrency and consistency.
Isolation Level | Prevents Dirty Reads? | Prevents Unrepeatable Reads? | Prevents Phantom Reads? |
---|---|---|---|
Read Uncommitted | β No | β No | β No |
Read Committed | β Yes | β No | β No |
Repeatable Read | β Yes | β Yes | β No |
Serializable | β Yes | β Yes | β Yes |
β Setting Isolation Level in SQL:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
5. Deadlocks in Concurrency Control
A deadlock occurs when transactions wait indefinitely for each otherβs resources.
Example of Deadlock:
- T1 locks
Account A
, T2 locksAccount B
. - T1 wants
Account B
, T2 wantsAccount A
. - Both wait indefinitely!
Deadlock Prevention Techniques
- Timeouts β If a transaction waits too long, it is aborted.sqlCopyEdit
SET LOCK_TIMEOUT 5000; -- Aborts transaction if locked for more than 5 seconds
- Wait-Die Scheme β Older transactions wait, younger ones restart.
- Wound-Wait Scheme β Older transactions force younger ones to rollback.
- Deadlock Detection β DBMS periodically checks for deadlocks and aborts one transaction.
6. Summary
- Concurrency ensures multiple transactions execute correctly in a multi-user system.
- Concurrency problems include Lost Update, Dirty Read, Unrepeatable Read, and Phantom Read.
- Concurrency control techniques:
- Locking mechanisms (2PL)
- Timestamp Ordering
- Optimistic Concurrency Control (OCC)
- Multi-Version Concurrency Control (MVCC)
- Transaction isolation levels prevent concurrency problems.
- Deadlocks can be handled using prevention, detection, and resolution techniques.