liveBook · Manning

Serializable isolation level

As explained in 100 SQL Server Mistakes and How to Avoid Them

Serializable is a transaction isolation level that provides the strongest standard protection against concurrent-read consistency anomalies, including phantom reads.

Serializable prevents all consistency anomalies, including phantom reads, but applying it by default can be an unnecessary mistake. Strong isolation may cause lock contention and deadlocks when Read Committed is sufficient. section 10.11 section 10.14

Mistake: protecting every report with Serializable

A reporting user counts ImpressionsArchive rows matching an operating system while a data steward deletes three matching rows and commits. Repeating the report can return a different count under weaker isolation; Serializable is reserved for cases that genuinely require this protection, such as actuarial calculations. section 10.11

Listing 10.27 Beginning the reporting user’s transaction
BEGIN TRANSACTION                              #1
 
SELECT COUNT(*)
FROM dbo.ImpressionsArchive
WHERE OperatingSystemID = (
    SELECT OperatingSystemID
    FROM dbo.ImpressionsArchive
    WHERE ImpressionID = 100
) ;

Run the reporting transaction in one query window and the data steward's delete in another, then compare the repeated counts shown in the paired listings. section 10.11

Apply these checks before choosing Serializable:

  • Keep Read Committed for conventional requirements unless stronger consistency is necessary. section 10.11
  • Consider optimistic isolation when performance suffers from lock contention, provided TempDB has capacity. section 10.14

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Explains phantom and nonrepeatable reads through concurrent SQL transactions, without detailing Serializable's internal implementation.
Examples
Uses reporting and data-steward transactions against ImpressionsArchive to demonstrate changing sums, counts, updates, and deletes.
What it assumes
Assumes basic transactions and concurrent reads, while introducing dirty, nonrepeatable, and phantom-read distinctions.
What you can do after
Diagnose phantom-read requirements and choose between default, strong, or optimistic isolation with operational consequences in mind.
How it connects
Connects Serializable with Read Committed, Repeatable Read, phantom reads, optimistic isolation, lock contention, and deadlocks.

Also explained in

Compare how 3 books explain serializable isolation level →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.