Lesson 2 of 3 Database Configuration and Concurrency

Transaction Isolation Levels and Row Versioning

This lesson explains concurrency phenomena, the five isolation levels and how RCSI and snapshot isolation use row versions.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe dirty reads, non-repeatable reads, phantoms and lost updates.
  • Distinguish between the isolation levels.
  • Explain the difference between RCSI and SNAPSHOT isolation.

Concurrency phenomena

Isolation levels are defined by which anomalies they allow. A dirty read reads data that another transaction has not committed and may roll back. A non-repeatable read reads the same row twice in a transaction and gets different values because another transaction committed a change in between. A phantom occurs when a repeated range query returns new rows inserted by another transaction. A lost update occurs when two transactions read the same value and both write, so one overwrites the other.

Match the pairs

Concurrency anomalies

Exam skills 2.2.2

Match each anomaly with its description.

Log in to try this activity Interactive activities are free with an account.

The isolation levels

SQL Server and Azure SQL support five isolation levels, set with SET TRANSACTION ISOLATION LEVEL, which the following table compares.

LevelDirty readsNon-repeatable readsPhantomsHow it works
READ UNCOMMITTEDPossiblePossiblePossibleNo shared locks (like NOLOCK)
READ COMMITTEDNoPossiblePossibleShort shared locks, or row versions when RCSI is on
REPEATABLE READNoNoPossibleShared locks held until the transaction ends
SERIALIZABLENoNoNoKey-range locks held until the transaction ends
SNAPSHOTNoNoNoReads a consistent snapshot from row versions as of the transaction start

Higher pessimistic levels hold more locks for longer, which increases blocking and deadlocks. READ UNCOMMITTED avoids blocking but can return uncommitted, duplicated or missing rows, so it is not suitable for data that must be correct.

Scenario questions

Choosing an isolation level

Exam skills 2.2.2

Choose the lowest isolation level that meets each requirement.

Log in to try this activity Interactive activities are free with an account.

Row versioning: RCSI and SNAPSHOT

With read committed snapshot isolation (RCSI) on, READ COMMITTED transactions read the last committed version of each row as of the start of each statement, from the version store, instead of taking shared locks, so readers and writers do not block each other. SNAPSHOT isolation reads as of the start of the transaction, giving a consistent view across several statements; if a snapshot transaction then updates a row that another transaction changed after the snapshot started, it fails with update conflict error 3960 and must be retried. In Azure SQL Database both are enabled by default; in SQL Server they are enabled with ALTER DATABASE ... SET READ_COMMITTED_SNAPSHOT ON and ALLOW_SNAPSHOT_ISOLATION ON. Row versioning costs version store space and some extra I/O.

SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRANSACTION;
    SELECT SUM(OnHand) FROM Inventory.Stock WHERE WarehouseID = 3;   -- consistent snapshot
    SELECT COUNT(*)    FROM Inventory.Stock WHERE WarehouseID = 3;   -- same snapshot
COMMIT;

The following diagram compares what two statements see under each level.

Row versioning with RCSI and SNAPSHOT. Under read committed snapshot isolation, each statement reads the last committed version as of the statement's start, from the version store, so readers and writers do not block each other, but two statements in one transaction can see different data. Under SNAPSHOT isolation, every statement reads as of the transaction's start, giving a consistent view; if it then updates a row changed after that, it fails with update conflict 3960.
RCSI is per statement; SNAPSHOT is per transaction.
True or false

Row versioning

Exam skills 2.2.2

Decide whether each statement about RCSI and SNAPSHOT is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

A consistent multi-statement report

A month-end report runs five SELECT statements in one transaction and must see the data exactly as it was when the report started, while order entry continues without being blocked. The database is in Azure SQL Database. Which isolation level should the report use?

Use SNAPSHOT isolation. It reads every statement from the same point-in-time version without taking shared locks, so it neither blocks nor is blocked by order entry. READ COMMITTED with RCSI is consistent only per statement, so totals in different statements could disagree, and SERIALIZABLE would block order entry for the duration of the report.

Check yourself

Does RCSI give a consistent view across several statements in one transaction?

Show answer

No. RCSI is consistent per statement; SNAPSHOT isolation is consistent for the whole transaction.

Exam Tips

Remember for the exam

  • Readers and writers do not block each other: RCSI (per statement) or SNAPSHOT (per transaction).
  • SNAPSHOT update conflicts raise error 3960; retry the transaction.
  • No phantoms with locking: SERIALIZABLE.
  • READ UNCOMMITTED and NOLOCK can return incorrect results.