When a SQL Server application reads and writes data concurrently, two goals often compete: keeping results transactionally consistent and preventing unnecessary waiting between sessions. Transaction isolation levels, locking, and row versioning determine how that balance is achieved.
A poor isolation choice can produce dirty reads, unexpected results, long blocking chains, or avoidable deadlocks. A carefully tested row-versioning design can reduce read/write contention, especially in read-heavy workloads. It is not an automatic performance guarantee, however. Version-store capacity, transaction duration, write conflicts, and application retry behavior must all be considered.
What does a transaction isolation level control?
An isolation level defines how strongly one transaction is separated from changes made by other concurrent transactions. SQL Server supports READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SNAPSHOT, and SERIALIZABLE.
The setting primarily determines how reads are protected and what concurrent changes a transaction can observe. It does not remove the need to protect data modifications. A transaction that modifies data still obtains exclusive protection for the modified data and retains that protection until the transaction completes.
| Isolation level | Core behavior | Main trade-off |
|---|---|---|
READ UNCOMMITTED | Can read data that has not been committed. | Dirty reads and inconsistent results. |
READ COMMITTED | Default level; can use locks or row versioning depending on database configuration. | Blocking is possible when lock-based behavior is used. |
REPEATABLE READ | Locks read rows until the transaction ends. | More locking and blocking; it does not fully prevent phantom rows. |
SNAPSHOT | Reads a transactionally consistent row-versioned view from transaction start. | Version-store usage and possible update conflicts. |
SERIALIZABLE | Uses the strongest isolation and range protection to prevent phantoms. | Concurrency can decrease and blocking can increase. |
Lock-based READ COMMITTED versus RCSI
SQL Server’s default READ COMMITTED behavior depends on the database option READ_COMMITTED_SNAPSHOT. When the option is off, reads generally use shared locks. If another session holds an exclusive lock for a modification, the read can wait.
When READ_COMMITTED_SNAPSHOT is on, transactions using READ COMMITTED read row versions instead of using shared locks to protect the read. Each statement sees a transactionally consistent view of committed data as it existed when that statement began.
This is a statement-level guarantee. If a transaction executes two separate SELECT statements, the second statement can see changes committed after the first statement began. If the entire transaction must use one consistent view, SNAPSHOT isolation may be more appropriate.
When should you use SNAPSHOT isolation?
SNAPSHOT provides transaction-level consistency. After the transaction starts, it continues to read the appropriate row versions from its starting point even if another transaction commits changes.
This can be useful for reporting workflows, validation processes, and business operations that perform multiple reads against one consistent view. Snapshot isolation does not eliminate write conflicts. If two transactions read the same data and later attempt conflicting updates, SQL Server can fail one transaction. The application should detect that condition and decide whether a safe retry is appropriate.
Enable the database option first:
ALTER DATABASE SalesDb SET ALLOW_SNAPSHOT_ISOLATION ON;Then select the isolation level in the connection that needs it:
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRANSACTION;
SELECT OrderId, CustomerId, TotalAmount
FROM dbo.Orders
WHERE CustomerId = 42;
COMMIT TRANSACTION;How do you enable RCSI and SNAPSHOT?
The two database options solve different problems:
READ_COMMITTED_SNAPSHOT ONchanges the implementation of normalREAD COMMITTEDstatements to statement-level row versioning.ALLOW_SNAPSHOT_ISOLATION ONallows sessions to explicitly selectSNAPSHOTisolation.
To enable RCSI:
ALTER DATABASE SalesDb SET READ_COMMITTED_SNAPSHOT ON;When changing this option, no other open connection can be active in the database except the connection running the ALTER DATABASE command. In production, plan the change around connection pools, application traffic, and an appropriate maintenance window.
Inspect the current state with:
SELECT
name,
is_read_committed_snapshot_on,
snapshot_isolation_state_desc
FROM sys.databases
WHERE name = N'SalesDb';Where are row versions stored?
Row versioning preserves earlier row images for as long as active operations require them. Depending on the SQL Server configuration and version, the version store can reside in tempdb or, when Accelerated Database Recovery is enabled, in the database that owns the versioned data.
This makes capacity planning important. Long-running transactions can prevent old versions from being cleaned up and can cause the version store to grow. In environments where the version store is in tempdb, that growth must be considered alongside temporary tables, sorting, hashing, and other tempdb workloads.
Reduce version-store pressure by following these practices:
- Do not keep a transaction open while waiting for user input or a remote service.
- Split large batch operations into measured, recoverable units.
- Separate application request timeouts from transaction lifetime decisions.
- Make sure every error path reaches either commit or rollback.
A practical decision framework
- If dirty data is unacceptable, avoid
READ UNCOMMITTEDand uncontrolledNOLOCKusage. - If reads are frequently waiting behind writes, evaluate RCSI under representative workload conditions.
- If several reads must observe one consistent view, consider
SNAPSHOTand design explicit handling for update conflicts. - If inserts into a read range must be prevented, consider
SERIALIZABLE, but keep the protected range and transaction duration as small as possible. - If a business operation requires specific lock behavior, evaluate isolation levels and targeted table hints together. Hints should not be used to hide a general design problem.
How do you diagnose blocking and versioning issues?
For lock-based blocking, start by finding waiting requests and their blockers:
SELECT
r.session_id,
r.blocking_session_id,
r.status,
r.wait_type,
r.wait_time,
r.command,
t.text
FROM sys.dm_exec_requests AS r
OUTER APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
WHERE r.blocking_session_id <> 0;For more granular lock details, use sys.dm_tran_locks. Run diagnostic queries carefully in production because broad DMV queries can also consume resources.
For row-versioning workloads, monitor long-running transactions, version-store growth, transaction duration, write-conflict rates, and application retries. RCSI can reduce read/write blocking, but it cannot compensate for inefficient queries, excessive transaction scope, or a workload dominated by long-running writes.
Common design mistakes
- Enabling RCSI without testing: application code may have relied on blocking as an accidental coordination mechanism.
- Assuming SNAPSHOT prevents all conflicts: it changes read behavior, but competing writes still need conflict handling.
- Keeping transactions open too long: this increases lock duration and can retain old row versions.
- Using SERIALIZABLE broadly: the strongest isolation should be limited to operations that actually require it.
- Ignoring connection state: isolation level remains associated with a connection until it is explicitly changed, so pooled connections require deliberate handling.
Conclusion
Choosing an isolation level is not about finding one universally fastest setting. The correct design balances data-consistency requirements, blocking cost, transaction duration, write-conflict behavior, and version-store capacity.
RCSI is a strong candidate for systems with significant read/write contention. SNAPSHOT can help when several reads must share one transactionally consistent view. SERIALIZABLE provides the strongest protection but should be used with narrow scopes and short transactions. Test every change with representative concurrency, then validate blocking, conflicts, transaction duration, and version-store behavior before adopting it in production.
0 Comments