Introduction
In modern database-driven applications, multiple users access and modify data at the same time. This is called concurrency in SQL Server. While concurrency improves performance and scalability, it can also create issues like dirty reads, deadlocks, and inconsistent data if not handled properly.
SQL Server uses locking mechanisms, isolation levels, and execution monitoring tools to manage concurrency and ensure data integrity.
In this article, we will understand locking in SQL Server in simple words, explore real execution scenarios, visualize blocking vs non-blocking flows, and learn performance tuning techniques used in real production systems.
Understanding Concurrency in SQL Server
Concurrency means multiple transactions happening at the same time.
Example
User A updates account balance
User B reads or updates the same account
Without proper control, the data can become incorrect.
Locking Flow Diagram (Blocking vs Non-Blocking)
Blocking Flow (Traditional Locking)
Transaction A → Lock Row → Update → Commit
Transaction B → Wait → Wait → Execute after release
Explanation
Transaction A locks the data
Transaction B is blocked until A completes
This ensures consistency but reduces performance
Non-Blocking Flow (Row Versioning)
Transaction A → Update (new version created)
Transaction B → Reads old version → No wait
Explanation
SQL Server stores previous versions of rows
Transaction B reads old data without waiting
Improves performance and user experience
Types of Locks in SQL Server
Shared Lock (S)
Used for reading data.
SELECT * FROM Users;
Exclusive Lock (X)
Used for writing data.
UPDATE Users SET Name = 'John' WHERE Id = 1;
Update Lock (U)
Prevents deadlocks during update operations.
Intent Locks
IS (Intent Shared)
IX (Intent Exclusive)
Real SQL Server Execution Scenario (Step-by-Step)
Scenario: Updating Same Record
Step 1
Transaction A starts
BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 100 WHERE Id = 1;
→ Exclusive lock applied
Step 2
Transaction B tries to read
SELECT * FROM Accounts WHERE Id = 1;
→ Blocked (waiting)
Step 3
Transaction A commits
COMMIT;
→ Lock released
Step 4
Transaction B proceeds
SQL Server Profiler & Execution Plan
SQL Server Profiler
SQL Server Profiler helps monitor real-time database activity.
What You Can See
Blocking queries
Deadlocks
Execution time
Locking behavior
Example Scenario
Run two conflicting queries
Profiler shows one query waiting
Helps identify performance bottlenecks
Join the conversation! Your thoughts help the community grow.