SQL Server · Reliability & Concurrency

Isolation Levels and Locking

When many users read and write at once, isolation levels decide what each transaction may see — and how long others must wait.

Advanced 5 min Lesson 37 of 44Examples run on SQL Server 2022 and Azure SQL Database.
Before you start:Transactions
0/44 in SQL Server
On this page · 5 min · Advanced

Learning objectives#

  • Explain shared and exclusive locks
  • Compare the isolation levels
  • Find blocking sessions
  • Turn on read committed snapshot

Explanation#

Writers take exclusive locks on the rows they change until the transaction ends. Under the default READ COMMITTED level, readers ask for shared locks and must wait for those writers — that waiting is blocking.

Levels from least to most strict: READ UNCOMMITTED (NOLOCK; may read uncommitted, later rolled-back data and even skip or double-read rows), READ COMMITTED, REPEATABLE READ, SERIALIZABLE (no phantom rows), and SNAPSHOT (reads a consistent version from when the transaction started).

READ_COMMITTED_SNAPSHOT (RCSI), a database option, makes READ COMMITTED read the last committed version of rows from tempdb instead of waiting. Readers no longer block writers or the other way round — the usual fix instead of sprinkling NOLOCK. Azure SQL Database turns it on by default.

Find blockers with sys.dm_exec_requests (blocking_session_id) or SSMS Activity Monitor. Keep transactions short: never wait for user input inside one.

Step-by-step screens#

Drawings of SQL Server 2022 Setup and SQL Server Management Studio (SSMS 20) with the ShopDB sample database. The red numbers match the numbered actions under each picture; names on your computer will differ.

Loading pictures…

Code example: See blocking, then remove it with RCSI#

SQLSee blocking, then remove it with RCSI Illustrative
-- Session 1
BEGIN TRAN;
UPDATE Products SET Price = Price * 1.1 WHERE Category = 'Laptops';
-- (not committed yet)


-- Session 2 waits here under READ COMMITTED
SELECT Name, Price FROM Products WHERE Category = 'Laptops';


-- Session 3: who is blocking whom?
SELECT session_id, blocking_session_id, wait_type, wait_time, command
FROM sys.dm_exec_requests
WHERE blocking_session_id <> 0;


-- Fix: readers use row versions instead of waiting (run when no one is connected)
ALTER DATABASE ShopDB SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
Expected output
session_id  blocking_session_id  wait_type  wait_time  command
----------  -------------------  ---------  ---------  -------
62          58                   LCK_M_S    8412       SELECT

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. Session 1 holds exclusive locks on the laptop rows.
  2. Session 2’s SELECT needs shared locks and waits (LCK_M_S).
  3. The DMV shows session 62 blocked by 58 for 8.4 seconds.
  4. With RCSI on, the SELECT reads the previous committed prices instead of waiting.

Real-world use case#

A shop’s product pages froze whenever the nightly price update ran. Turning on RCSI let pages read the old prices until the update committed — no code changes.

Common mistakes#

NOLOCK everywhere

It can return rows that are later rolled back, or count a row twice during page splits. Use RCSI instead.

Long transactions

Locks are held until commit. Keep transactions short and never open one while waiting for a user.

Best practices#

  • Keep transactions short
  • Prefer RCSI over NOLOCK
  • Index the WHERE columns of updates (fewer locks)
  • Monitor blocking regularly

Remember this#

Readers wait for writers under READ COMMITTED.

RCSI = readers read versions, no waiting.

NOLOCK = dirty reads.

Practice exercise#

Your task: Check whether READ_COMMITTED_SNAPSHOT is on for ShopDB.

Show solution
SQL Illustrative
SELECT name, is_read_committed_snapshot_on FROM sys.databases WHERE name = 'ShopDB';

Mini quiz#

Isolation Levels and Locking quizMultiple choice Question 1 of 2

Which isolation level allows dirty reads?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.