Blog
AdvancedReferenceSQL Server Engineering

Locks, isolation, and the version store

The concurrency contract - lock modes and granularity, escalation to 5,000 locks, the anomalies each isolation level permits, how RCSI and SNAPSHOT trade version-store churn for non-blocking reads, and what optimized locking changes in 2025.

Applies to
SQL Server2016-2025 (13.x-17.x)optimized locking is 2025 (17.x) and Azure SQL DatabaseAzure SQL DatabasecurrentREAD_COMMITTED_SNAPSHOT is ON by default - opposite of box SQL Server
matureUpdated Reviewed
advancedAssumes production experience with the basics.

What you will be able to do

  • Name the lock modes and their granularity, and explain why a reader and a writer conflict under locking READ COMMITTED.
  • State the lock-escalation trigger - 5,000 locks on one reference - and the memory threshold behind it.
  • Choose between locking READ COMMITTED, RCSI, and SNAPSHOT for a workload, and say what version-store cost each charges.
  • Explain what optimized locking changes - the TID lock and lock-after-qualification - and that it leans on RCSI.

Every concurrent workload is two questions: what does a session hold, and for how long. SQL Server answers the first with locks (logical, protecting data) and latches (physical, protecting in-memory pages) - and the second with the isolation level, which decides how much consistency a read must see and therefore how much it must hold.

The analogy for the whole lesson is a records room with a sign-out board. A clerk who pulls a file to read it pins a marker on the shelf so nobody rewrites it mid-read - a shared hold - while a clerk who must amend the file pins a “do not touch” marker, an exclusive hold that queues everyone else. The isolation level is the house rule for how long markers stay up: for the glance only, or until the whole job is finished. Row versioning is the photocopy box on the counter - a reader takes a copy of the last filed version and never queues for the original at all.

Locks - the contract between sessions

A lock is a claim on data at some granularity - row, key, page, or the whole table - and a mode that says what it allows others to do:

Mode Acquired by Blocks
Shared (S) Readers under locking READ COMMITTED+ Writers (X); other readers coexist
Update (U) A reader that intends to write Other U locks - prevents a common deadlock shape
Exclusive (X) Writers Everything - readers and writers alike
Intent (IS/IX) A finer lock beneath a coarser one Signals “a lower-level lock exists” so a table lock can be checked without scanning rows
Schema (Sch-S/Sch-M) Compile/DDL Sch-M blocks everything while a table changes shape

Under the default locking READ COMMITTED, a reader takes S locks to stop the row changing underneath it. That is the entire mechanism of the classic OLTP fight: readers block writers and writers block readers, and on a hot row the queue forms immediately.

Escalation - when fine locks become a table lock

Taking a lock costs memory - the lock structure is about a hundred bytes - so the engine does not hold millions of them. When a single statement passes 5,000 locks on one table reference, the engine tries to escalate to a coarser HoBT or table lock, checking at every 1,250 locks acquired. There is also a memory tripwire: escalation fires when lock memory reaches roughly a quarter of the engine’s memory (the dynamic threshold behind the locks option’s default of 0).

Two properties worth knowing precisely, because they surprise:

  • Escalation is per statement and per reference - 3,000 locks on each of two indexes of the same table does not trigger it, nor does 3,000 in a self-join.
  • ALTER TABLE ... SET (LOCK_ESCALATION = AUTO) escalates to partition level on a partitioned table instead of the whole table - the one useful knob.

The cost of escalation is not memory; it is that a table X lock turns row-level concurrency into a queue of one. The fix is shorter transactions and smaller batches, not disabling escalation - DISABLE just trades the queue for lock-memory exhaustion.

The isolation menu - and what each level costs

Level How reads are protected The anomalies it permits The cost
READ UNCOMMITTED Nothing - reads take no S locks Dirty reads of uncommitted data Correctness - you can read rows that roll back
READ COMMITTED (locking) S locks held per statement Non-repeatable reads, phantoms Readers block writers
READ COMMITTED + RCSI Row versions in tempdb, per statement Non-repeatable reads, phantoms Version-store churn in tempdb; readers no longer block
REPEATABLE READ S locks held to commit Phantoms Longer-held locks, more blocking
SNAPSHOT Row versions, per transaction Update conflicts possible Version store + the conflict error on concurrent update
SERIALIZABLE S + key-range locks to commit None the model prevents Range locks - the heaviest contention

RCSI (READ_COMMITTED_SNAPSHOT) is the one to understand cold. With it ON, READ COMMITTED stops taking S locks entirely and instead reads a row version - the last committed image, reconstructed from the version store in tempdb. Readers stop blocking writers and writers stop blocking readers; the read sees a statement-consistent snapshot. The price is real: writers pay a 14-byte version tag per row plus tempdb storage and cleanup for every version they generate - and a long-running reader pins old versions so the store cannot purge.

The default is the trap: RCSI is OFF on box SQL Server but ON by default in Azure SQL Database. The same query can be a blocking reader on a VM and a non-blocking versioned read in Azure, and the only change was the platform’s default. Check sys.databases.is_read_committed_snapshot_on before assuming which behaviour you have.

Optimized locking - the 2025 change worth knowing

Traditional locking holds every row lock to commit. Optimized locking (Azure SQL Database, then SQL Server 2025) changes the shape: it takes a single TID lock on the transaction ID for the duration, and releases each page/row lock as soon as that row is updated - lock-after- qualification (LAQ). The effects: lock memory drops sharply, far fewer locks are held to commit, and the escalation path is reached far less often. It works with RCSI rather than replacing it - Microsoft’s guidance is explicit that RCSI is where the most benefit lands, and on Azure SQL it is on by default alongside RCSI.

Choosing deliberately

  • A read-heavy OLTP workload fighting readers-blocks-writers → turn on RCSI; the version-store cost is almost always cheaper than the blocking it removes.
  • A consistency-critical report that must see one point in time → SNAPSHOT for the statement or transaction, with the version store sized for it.
  • A hot-row updater where the queue is the problem → look at optimized locking on 2025+; on older versions the honest fixes are shorter transactions and touching fewer rows per commit.
  • NEVER reach for READ UNCOMMITTED or NOLOCK to fix blocking - it does not reduce the writer’s locks, it just lets you read rows that may be rolled back. It trades correctness for the appearance of speed.

Guided practice

A beginner exercise in blocking and versioning, run on a scratch database with two sessions open against it. The scenario is the classic OLTP fight - one writer, one reader, one hot row.

  1. Check which default the database is on. Query sys.databases.is_read_committed_snapshot_on for the scratch database. Check: 0 means locking READ COMMITTED - the box default; 1 means RCSI. If the value surprises you, that is the trap the lesson names: the same query is a blocking reader on one platform and a versioned read on another, and only this setting separates them.
  2. Open a blocking writer. In session one, BEGIN TRAN, update one row in a scratch table, and leave the transaction open. In session two, read the same row under the default isolation. Check: the reader waits - its S lock cannot be granted while the writer’s X lock stands, which is readers-block-writers under locking READ COMMITTED. If the reader returned instantly, the database is already on RCSI or the read took no locks; go back to step one.
  3. Repeat under RCSI. Roll back session one, set READ_COMMITTED_SNAPSHOT ON for the scratch database, then run the same writer-then-reader pair again. Check: the reader now returns immediately with the last committed image - the pre-update row from the version store in tempdb, not the writer’s uncommitted change. If it still blocked, check the setting actually took effect and that no other session held the database open.
  4. Force an escalation. In one transaction, update enough rows to pass 5,000 locks on one table reference, and inspect the locks held (for example through sys.dm_tran_locks). Check: past 5,000 locks on one reference the engine tries to escalate to a HoBT or table lock, checking at every 1,250 acquired - you see one coarse lock instead of thousands of row locks. If escalation did not fire, revisit the properties: it is per statement and per reference, so the same count spread across two references does not trigger it.
  5. Name the price RCSI charges. With RCSI still on, write repeatedly to one table while a long-running reader holds an open snapshot against it. Check: every written row carries a 14-byte version tag and generates a version in tempdb, and the open reader pins old versions so the store cannot purge them - the non-blocking read is paid for in the version store. If you expected the read to be free, revisit the RCSI paragraph.

Check your understanding

Ordered easy to hard. Answer each before reading the answer; a wrong answer names where to look.

  1. What is the difference between a lock and a latch? A lock protects data logically between transactions; a latch protects in-memory pages physically inside the engine. If you treated them as interchangeable, revisit the opening - they answer different questions and surface as different wait families.
  2. Why does a reader block a writer under default locking READ COMMITTED? The reader takes shared (S) locks per statement to stop the row changing underneath it, and S is incompatible with the writer’s X. That is the entire mechanism of the classic OLTP fight. If you said “because the reader is slow”, revisit the lock-mode table - it is the lock, not the speed, that queues the writer.
  3. A single statement suddenly holds a table lock and every other session queues behind it. What fired, and what is the real fix? Lock escalation - past 5,000 locks on one reference the engine trades many row locks for one coarse lock, turning row-level concurrency into a queue of one. The fix is shorter transactions and smaller batches; DISABLE only swaps the queue for lock-memory exhaustion. If you would have disabled escalation, revisit the escalation section.
  4. What does RCSI charge for making readers stop blocking writers? Row versions in tempdb - a 14-byte version tag per written row, plus the storage and cleanup of every version generated, and a long-running reader pins old versions so the store cannot purge. If you said “the same guarantee for free”, revisit the isolation menu - the permitted anomalies are identical to locking READ COMMITTED; only the protection mechanism and its cost differ.
  5. A transaction under SNAPSHOT aborts with an update conflict. Why does that error exist at all? SNAPSHOT versions per transaction, so two concurrent writers can both read the same committed image and then both try to change it - the engine aborts one rather than lose the update. It is the price of transaction-level consistency without blocking. If you expected SNAPSHOT to be a free upgrade over RCSI, revisit the isolation menu - per-transaction versioning adds a conflict that per-statement RCSI does not have.

Where SQL Server sits

How this component relates to the rest of the stack. Items without a link are not covered here yet.

Deployed with

Commonly run alongside

  • Windows Failover Clustering

Alternatives

Solves a similar problem differently