codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🗄️

DBMS

23 chapters

1Intro & 3-Schema Architecture2ER Model & Diagrams3Generalization, Specialization & Aggregation4Relational Model & Codd's Rules5Relational Algebra6Tuple & Domain Relational Calculus7SQL: DDL, DML, DCL8Advanced SQL (Joins, Aggregates)9Views, Triggers & Stored Procedures10Functional Dependencies11Normalization (1NF, 2NF, 3NF)12BCNF & Lossless Decomposition13Transaction Concepts & ACID14Conflict & View Serializability15Concurrency Control & Locks162-Phase Locking (2PL)17Timestamp-Based Protocols18Indexing (Primary, Clustering)19B-Trees & B+ Trees20Hashing Techniques in DBMS21Database Recovery Techniques22NoSQL Databases Overview23Data Warehousing Concepts
SubjectsDBMS

Concurrency Control & Locks

Updated 2026-05-04
3 min read

Concurrency Control & Locks

In a real-world enterprise database (like Amazon or Netflix), millions of transactions are executing simultaneously. If the DBMS executed these transactions serially (one after another), performance would be unacceptably slow. The DBMS must interleave the operations of multiple transactions—this is called Concurrency.

However, unmanaged concurrency leads to severe data anomalies. Concurrency Control is the mechanism the DBMS uses to ensure that concurrent transactions produce the same results as if they were executed sequentially.

1. Concurrency Problems

If concurrency is not controlled, three major problems can occur:

1. The Lost Update Problem (Write-Write Conflict)

Occurs when two transactions read the same data, then both update it. The second update permanently overwrites the first one.

  • Example: T1 and T2 both read balance = 100. T1 adds $50 and writes 150. A millisecond later, T2 subtracts $20 from its original read and writes 80. T1's deposit is completely lost.

2. The Dirty Read Problem (Read-Write Conflict)

Occurs when a transaction reads data that has been updated by another uncommitted transaction. If the other transaction rolls back, the first transaction has read data that "never officially existed".

3. The Unrepeatable Read Problem

Occurs when a transaction reads the same row twice during its execution, and gets different data each time because another transaction updated the row in between the reads.

2. Lock-Based Protocols

To solve these problems, the DBMS uses Locks. A transaction must acquire a lock on a data item before it can access it.

Types of Locks

  1. Shared Lock (S-Lock): If a transaction holds a shared lock on item $Q$, it can read $Q$, but cannot write to it. Multiple transactions can hold shared locks on the exact same item simultaneously.
  2. Exclusive Lock (X-Lock): If a transaction holds an exclusive lock on item $Q$, it can both read and write to $Q$. Only ONE transaction can hold an exclusive lock on an item at any given time. No other locks (shared or exclusive) can be granted to other transactions until it is released.

3. The Deadlock Problem

The use of locks introduces a new problem: Deadlock.

Suppose transaction T1 holds an Exclusive Lock on row A and requests a lock on row B. Simultaneously, transaction T2 holds an Exclusive Lock on row B and requests a lock on row A.

Neither transaction can proceed. They will wait for each other infinitely. To solve this, the DBMS runs a Deadlock Detection algorithm in the background. If it detects a cycle, it automatically aborts and rolls back one of the transactions (the "victim"), releasing its locks and allowing the other to proceed.

4. Isolation Levels

SQL allows developers to choose exactly how strict the concurrency control should be for a specific transaction, trading off consistency for performance.

  1. Read Uncommitted: No locks are acquired for reading. Dirty reads are possible. (Highest performance, lowest consistency).
  2. Read Committed: (Default in Postgres/SQL Server). A transaction can only read committed data. Prevents dirty reads.
  3. Repeatable Read: (Default in MySQL). The DBMS locks all rows read by the transaction until it finishes. Prevents dirty reads and unrepeatable reads.
  4. Serializable: The strictest level. The DBMS executes transactions in a way that perfectly mimics sequential execution. (Lowest performance, highest consistency).


PreviousConflict & View SerializabilityNext2-Phase Locking (2PL)

Recommended Gear

Conflict & View Serializability2-Phase Locking (2PL)