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.
A check-then-act race
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.

An optimistic save
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.
| Approach | Mechanism | Best for |
|---|---|---|
| Pessimistic | Locks or UPDLOCK hints held until commit | Short transactions with likely conflicts |
| Optimistic (rowversion) | Detect changes at save time | Web and API edits with user think time |
| Row versioning (RCSI, SNAPSHOT) | Readers use versions instead of shared locks | Mixed read and write workloads |
| Optimized locking | TID locks and lock after qualification | Reducing blocking and lock memory automatically |
Choosing a concurrency approach
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.
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.