RCSI and Snapshot Isolation

SQL Server is lock-based by default, so reading a row that is being modified means waiting until the writing transaction ends. When you turn on row versioning, each modification copies the old version to the version store, and readers take no locks and read the version that matches their own point in time. There are two options. RCSI (READ_COMMITTED_SNAPSHOT) changes the existing Read Committed behavior to a statement-level snapshot, and snapshot isolation (ALLOW_SNAPSHOT_ISOLATION) enables the SNAPSHOT isolation level, which gives a transaction-level snapshot.

Key Specs

ItemRCSISnapshot isolation
OptionREAD_COMMITTED_SNAPSHOT ONALLOW_SNAPSHOT_ISOLATION ON
How to useOnce on, it applies to all of Read Committed immediately (no code change)SET TRANSACTION ISOLATION LEVEL SNAPSHOT in the session
Snapshot timePer statementThe transaction's first data access
Write conflictWaits on a lock (same as ordinary Read Committed)Modifying a row that another transaction modified first raises error 3960 and rolls back the transaction
Azure SQL Database defaultOnOn
SQL Server defaultOffOff
ItemValue
Version storetempdb. With ADR on, the persistent version store (PVS) inside the user database
Per-row informationA 14-byte version tag (transaction sequence number and previous-version pointer). Added to rows modified after the option is turned on
CleanupA background task periodically removes versions that are no longer needed (about every minute)

How It Works

Figure. Copying the old version to the version store on modification

flowchart LR
  A["T1: UPDATE balance 500K → 400K"] --> B["Table row: 400K<br/>14-byte tag holds previous-version pointer"]
  A --> C["Version store: 500K<br/>tempdb or PVS"]
  B -- "pointer" --> C
  D["T2: SELECT (started before T1 commit)"] --> B
  B -- "change T1 made that did not exist at T2's time" --> C
  C --> E["T2 reads 500K, no lock wait"]

Diagram explanation: When T1 modifies the row, the table row takes the new value 400K, and the old value 500K is copied to the version store. The row's 14-byte tag points to that previous version. When T2 reads the row, if the row's version is newer than its own point in time, it follows the pointer and reads the version from its own point in time. So T2 does not wait for T1's X lock.

Caution when turning on RCSI. Some code behaves differently once reads stop waiting. For example, if "read, check, then modify" logic relied on lock waits, it can now read an old value and move on, so use the UPDLOCK hint or the READCOMMITTEDLOCK hint in such places. The ALTER DATABASE that turns on RCSI finishes only when no other connections exist (WITH ROLLBACK IMMEDIATE).

When the version store grows. A long-open snapshot transaction or a long query prevents deleting versions created after its point in time, so tempdb (or the PVS) keeps growing. On a system with many updates, allow generous tempdb space and I/O.

Azure SQL Database. RCSI and snapshot isolation are both on by default and ADR is always on, so an application moved from on-premises SQL Server reads old versions instead of waiting on locks.

Configuration and Checks

SELECT name, is_read_committed_snapshot_on, snapshot_isolation_state_desc, is_accelerated_database_recovery_on
FROM sys.databases;

ALTER DATABASE tune SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
ALTER DATABASE tune SET ALLOW_SNAPSHOT_ISOLATION ON;

SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRANSACTION;
  SELECT balance FROM dbo.account WHERE id = 1;
COMMIT;

-- Version store usage by database
SELECT DB_NAME(database_id) AS db, reserved_space_kb / 1024 AS reserved_mb
FROM sys.dm_tran_version_store_space_usage;

-- Long-open snapshot transactions
SELECT session_id, transaction_id, elapsed_time_seconds
FROM sys.dm_tran_active_snapshot_database_transactions ORDER BY elapsed_time_seconds DESC;

Comparison with Other DBMSs

SQL ServerPostgreSQLInnoDBOracle
Old version locationtempdb version store or PVSInside the table (cleaned by VACUUM)Undo logUndo tablespace
Turning on and offDatabase optionAlways MVCCAlways MVCCAlways MVCC
Default Read CommittedLock-based (Azure SQL uses RCSI)Statement-level snapshot— (the default is Repeatable Read)Statement-level snapshot
Write conflict in a transaction snapshotError 3960Serialization failure errorModifies the latest rowORA-08177 (Serializable)

In the Articles

NOTE The version store location and defaults can vary by version, ADR setting, and Azure service.

Related pages