Overview
SQL Server defaults to lock-based READ COMMITTED: readers take shared locks and are blocked by writers. Two database options switch to row versioning (MVCC-like) instead, so readers neither block nor are blocked by writers: READ_COMMITTED_SNAPSHOT (RCSI) and ALLOW_SNAPSHOT_ISOLATION.
Core concepts
| Setting | Behaviour |
|---|---|
READ COMMITTED (default, locking) | Shared locks while reading; blocks behind writers |
READ COMMITTED with READ_COMMITTED_SNAPSHOT ON | Each statement reads the last committed version as of statement start; no S locks. Transparent to applications |
SNAPSHOT (ALLOW_SNAPSHOT_ISOLATION ON, then SET TRANSACTION ISOLATION LEVEL SNAPSHOT) | The whole transaction sees a consistent version; update conflicts raise error 3960 |
REPEATABLE READ, SERIALIZABLE | Locks held to end of transaction; range locks for serializable |
Row versions are kept in a version store: in tempdb, or in the database itself when Accelerated Database Recovery (ADR) is enabled (2019+). Each versioned row carries a 14-byte pointer, so the first change to a row after enabling it widens it. Writers still block writers (they need the row lock).
Azure SQL Database enables RCSI by default; on-premises databases do not, including new ones.
Worked example
Session A: BEGIN TRAN; UPDATE dbo.Acct SET bal = bal - 10 WHERE id = 1; and wait. Session B: SELECT bal FROM dbo.Acct WHERE id = 1;. Measured on SQL Server 2022:
| Database option | Reader B |
|---|---|
READ_COMMITTED_SNAPSHOT OFF | returned 100 after 8 s, when A rolled back (waited on LCK_M_S) |
READ_COMMITTED_SNAPSHOT ON | returned 100 immediately (the last committed value) |
Enabling it needs exclusive access to the database for a moment:
ALTER DATABASE kb SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE; -- disconnects other sessions
SELECT name, is_read_committed_snapshot_on, snapshot_isolation_state_desc FROM sys.databases;Pitfalls and production notes
- Version store growth: a long-open transaction prevents cleanup. Monitor
sys.dm_tran_version_store_space_usageandtempdbspace (sqlserver-storage-tempdb). - Code that relied on blocking (for example “read a row, then update based on it” assuming nobody else changed it) can now lose updates; use
UPDLOCKhints or snapshot-mode conflict detection. - RCSI statement-level consistency still allows non-repeatable reads across statements (isolation-levels).
- Test workloads before enabling:
tempdbI/O and CPU rise because of version generation. - Prefer RCSI over
NOLOCK;NOLOCKreturns uncommitted data and can skip or duplicate rows. - PostgreSQL and Oracle behave this way by default (pg-mvcc).
Practice questions
- Which anomalies does RCSI still allow?
- What happens to a snapshot transaction that updates a row changed by another transaction since its start?
- Where do the row versions live with and without ADR?
References
- Microsoft Learn, Snapshot isolation in SQL Server; Accelerated database recovery
Related
isolation-levels · sqlserver-locking-blocking-deadlocks · sqlserver-storage-tempdb · pg-mvcc