Lesson 3 of 3 Database Configuration and Concurrency

Pessimistic, Optimistic and Optimized Locking

This lesson explains pessimistic locking, optimistic concurrency with rowversion and optimized locking.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish between pessimistic and optimistic concurrency.
  • Explain how a rowversion check detects lost updates.
  • Describe transaction ID locking and lock after qualification.

Pessimistic concurrency

Pessimistic control locks data before changing it so no one else can change it at the same time. Inside a transaction, WITH (UPDLOCK, HOLDLOCK) on a SELECT takes update locks that prevent another transaction from reading the same row for update, which fixes check-then-act races such as "read stock, then decrement". A simpler and often better alternative performs the check and the change in one statement, for example UPDATE ... SET OnHand -= @q WHERE OnHand >= @q and checking @@ROWCOUNT. Pessimistic control suits short transactions with frequent conflicts.

Spot the bug

A check-then-act race

Exam skills 2.2.2

Find the lines that let two sessions oversell the same stock.

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

Optimistic concurrency with rowversion

Web and API applications read data, show it to a user and save changes seconds or minutes later, so they cannot hold locks in between. Optimistic control detects conflicts at save time: a rowversion column changes automatically on every update, the application keeps the value it read, and the UPDATE includes it in the WHERE clause. If another user changed the row, the UPDATE affects zero rows, and the application reports a conflict instead of silently overwriting the other change.

ALTER TABLE Sales.Customer ADD RowVer rowversion;

UPDATE Sales.Customer
SET CreditLimit = @NewLimit
WHERE CustomerID = @CustomerID
  AND RowVer = @RowVerReadEarlier;

IF @@ROWCOUNT = 0
    THROW 50030, N'The customer was changed by someone else. Reload and try again.', 1;

The following diagram shows two users saving the same row.

Optimistic concurrency with a rowversion column. Two users read the same row with the same rowversion value. The first saves with an UPDATE whose WHERE clause includes that value; the row changes and its rowversion changes automatically. The second user's UPDATE, still using the old value, affects zero rows, so the application reports a conflict instead of silently overwriting the first change.
The second save fails instead of overwriting the first.
Put in order

An optimistic save

Exam skills 2.2.2

Put the steps of an optimistic concurrency save in order.

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

Optimized locking

Optimized locking, enabled by default in Azure SQL Database and available in SQL Server 2025, has two parts. Transaction ID (TID) locking holds one lock on the transaction ID instead of a lock per modified row until commit, which greatly reduces lock memory and lock escalation. Lock after qualification (LAQ), which works with RCSI, evaluates an UPDATE or DELETE predicate on the latest committed version without locks and locks only the rows that qualify, so concurrent updates of different rows block each other far less. It requires accelerated database recovery. The approaches are compared in the following table.

ApproachMechanismBest for
PessimisticLocks or UPDLOCK hints held until commitShort transactions with likely conflicts
Optimistic (rowversion)Detect changes at save timeWeb and API edits with user think time
Row versioning (RCSI, SNAPSHOT)Readers use versions instead of shared locksMixed read and write workloads
Optimized lockingTID locks and lock after qualificationReducing blocking and lock memory automatically
Match the pairs

Choosing a concurrency approach

Exam skills 2.2.2

Match each workload with the concurrency approach that suits it.

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

Two agents edit the same ticket

In a support web app, two agents open the same ticket, edit different fields and save a few minutes apart; the second save silently overwrites the first agent's changes. The team wants the second agent warned instead. What should the developer implement?

Add a rowversion column, return it with the ticket, and include it in the UPDATE's WHERE clause; when the update affects zero rows, report a conflict. Holding a transaction with locks while agents edit would block other users for minutes, and SERIALIZABLE or SNAPSHOT isolation applies only within a single transaction, not across the minutes between reading and saving.

Check yourself

Which feature must be enabled for optimized locking?

Show answer

Accelerated database recovery (ADR); lock after qualification also needs RCSI.

Exam Tips

Remember for the exam

  • Lost updates across user think time: optimistic concurrency with rowversion.
  • Check-then-act races in T-SQL: one conditional UPDATE, or UPDLOCK in a short transaction.
  • Optimized locking: TID locking plus lock after qualification; requires ADR.
  • Never hold locks while waiting for user input.