What you will be able to do
- Read a blocking chain from sys.dm_exec_requests and find the head blocker, not just the symptom.
- Read a deadlock graph from the system_health session and name the cycle's resources and order.
- Distinguish a lock wait from a latch wait from an I/O wait by wait type alone - LCK_M_*, PAGELATCH_*, PAGEIOLATCH_*.
- Explain the last-page-insert latch convoy and the index-key fixes for it.
Concepts covered
“It is slow under load” is three different problems wearing the same symptom, and the wait type
tells them apart before you open a single tool. A session waiting on a lock (LCK_M_*) is held
by another session’s transaction. A session waiting on a latch (PAGELATCH_*) is held by
another thread’s claim on an in-memory page. A session waiting on I/O (PAGEIOLATCH_*) is
held by storage. The fixes are completely different, so the first job is never tuning - it is
naming which one you have.
The analogy is the different reasons to be standing still in an office. Queuing at a colleague’s door while they hold the signed contract open on their desk is a lock wait - another worker’s transaction holds it. People reaching for the same whiteboard marker at once is a latch wait - a brief fight over the shared surface itself, over in moments. Waiting for a file to come up from the archive is the I/O wait - nobody is holding anything, the document is simply in transit. “It is slow” is one symptom; who or what you are standing still for is the diagnosis.
Blocking - a queue behind a transaction
Blocking is ordinary locking doing its job - a session waits on LCK_M_* because another session
holds an incompatible lock. It becomes an incident when the head of the chain is a long
transaction everyone else queues behind. Find it, not the victim:
SELECT r.session_id, r.blocking_session_id, r.wait_type,
r.wait_time / 1000.0 AS wait_s, t.text AS waiting_sql
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;
Walk blocking_session_id up to the session blocked by nothing - that is the head blocker, and it
is almost always one of: a transaction left open by the application, a transaction doing work it
does not need to inside the lock window, or a batch touching far more rows than necessary. The fix
is at the head - shorten the transaction, commit earlier, touch fewer rows - not on the queued
victims.
Deadlocks - a cycle, resolved by killing someone
A deadlock is a cycle: session A holds what B wants, B holds what A wants, and neither can proceed.
The engine detects it and kills a victim - chosen by rollback cost, or by DEADLOCK_PRIORITY.
Error 1205 reaches one of the applications, which must be written to retry.
Read the cycle from the deadlock graph (always captured in the system_health extended-events
session; pull it with sp_readerrorlog-style queries or the XE UI). The graph names the two
resources, the two statements, and the lock order each took. The fixes all attack the ordering:
- Access objects in a consistent order - most application deadlocks are two paths through the same tables in opposite order.
- Shorten the lock window - smaller transactions mean less time overlapping.
- Index the access path - a query that scans a million rows to update ten holds locks across the scan’s range; a seek narrows it.
- Key-range locks under SERIALIZABLE deadlock differently - they lock the gap, so a deadlock on a key-range resource usually means an isolation level heavier than the workload needs.
Latches - the physical lock people confuse with a lock
A latch protects an in-memory page’s structure, not the data’s logical state. PAGELATCH_*
means threads are fighting over a buffer-pool page - most classically the allocation pages
(PFS/GAM/SGAM) in tempdb, or a hot data page all sessions converge on. The distinguishing line:
| Wait type | What is contended | The tell |
|---|---|---|
LCK_M_* |
A row/key lock held by a transaction | blocking_session_id points at a real blocker |
PAGELATCH_* |
An in-memory page (allocation or hot data) | Resource is a page like 2:1:1 (tempdb PFS) or a hot index page |
PAGEIOLATCH_* |
A page being read from storage into memory | I/O subsystem latency - the buffer is cold |
PAGELATCH is not waiting for disk. Adding faster storage does nothing for a PAGELATCH
convoy, and adding tempdb files does nothing for LCK_M_* blocking. The wait type decides the fix.
The last-page-insert convoy - the canonical PAGELATCH
Every insert on an ever-increasing key - IDENTITY, datetime DEFAULT, NEWSEQUENTIALID - lands
on the same physical last page of the index. Under concurrency, every inserter serialises on a
buffer latch for that page: throughput plateaus while CPUs sit idle, all waiting PAGELATCH_EX on
one hot page. The fixes attack the “one hot page” property:
- A non-sequential key (a hash, a reversed key,
OPTIMIZE_FOR_SEQUENTIAL_KEYin recent versions) spreads inserts across many pages instead of one. - More files / a different index shape - the tempdb variant is solved by files; the hot-page variant is solved by the key.
This is the textbook case of the wait type being the diagnosis: the symptom is a throughput ceiling,
the wait is PAGELATCH not I/O, and the fix is in the index key, not the storage.
Guided practice
A beginner exercise, worked in a scratch database with two or three query windows. The scenario: create a blocking chain on purpose, find the head blocker, then create and resolve a deadlock. Work each step and check yourself before moving on.
- Build the contention table. In a scratch database, create
dbo.accountswith an id and a balance, and insert a handful of rows. Check: a few rows are enough - contention needs two sessions wanting the same row, not a big table. If you built a large table, you missed the point: the fault is in the lock ordering, not the volume. - Open the head blocker. In session A:
BEGIN TRAN; UPDATE dbo.accounts SET balance = balance + 1 WHERE id = 1;- and leave the transaction open. Check: session A holds its locks with no commit. If you committed, there is no chain to read - the whole exercise is the open transaction, which the lesson names as one of the usual head blockers: a transaction left open by the application. - Queue a victim and read the chain. In session B, run the same
UPDATE. In a third window, run the lesson’s query onsys.dm_exec_requestswhereblocking_session_id <> 0. Check: session B showsLCK_M_*as its wait type and itsblocking_session_idpoints at A - and A is blocked by nothing, making it the head blocker. If you looked at B’s wait time rather than the chain, reread the method: the fix is at the head, not on the queued victims. - Resolve it and name the fix class.
COMMITin session A and watch B complete. Then say in one sentence what the application fix looks like. Check: B finishes the instant A’s locks release. The fix class is shorten the transaction, commit earlier, touch fewer rows - all at the head. If you answered “add an index” or “upgrade the hardware”, revisit the blocking section: ordinary locking was doing its job; the open window was the fault. - Produce a deadlock. Reset, then in session A
BEGIN TRANand updateid = 1; in session BBEGIN TRANand updateid = 2; then have A updateid = 2and B updateid = 1. Check: one session gets error 1205 - the engine detected the cycle (A holds what B wants, B holds what A wants) and killed a victim by rollback cost. If nothing deadlocked, both sessions must hold their first lock before requesting the second; the ordering is the mechanism, not timing luck. - Name the fix that attacks the ordering. Without any more tools, what one habit prevents this deadlock shape? Check: accessing objects in a consistent order - most application deadlocks are two paths through the same tables in opposite order, which is exactly what you just built. Shorter lock windows and a narrower indexed access path shrink the overlap further. If you answered “retry harder”, retries absorb the error but the cycle remains - revisit the deadlock fix list.
Check your understanding
Ordered easy to hard. Answer each before reading the answer; a wrong answer names where to look.
- Which wait type family means a session is queued behind another transaction’s lock?
LCK_M_*- a real blocker exists, andblocking_session_idinsys.dm_exec_requestspoints at it. The other families arePAGELATCH_*(an in-memory page fight) andPAGEIOLATCH_*(storage); if you mixed them up, the wait-type table is the section to reread - the type decides the fix. - How do you find the head blocker rather than the loudest victim?
Walk
blocking_session_idup the chain until you reach the session that is blocked by nothing - that is the head, and it is almost always an open transaction, a transaction doing needless work inside its lock window, or a batch touching too many rows. The fix lives there; killing victims only resets the queue. - A session waits
PAGELATCH_EXon one hot index page. Why does faster storage not help? The page is already in memory - the latch protects the page’s in-memory structure, not a disk read, so the fight is between threads, not between the engine and storage.PAGELATCHis not waiting for disk; onlyPAGEIOLATCHis. Revisit the three-way wait table if the distinction is shaky. - What does the deadlock graph give you that the error message does not, and where is it
captured?
The error only says someone died (1205); the graph names the two resources, the two statements,
and the lock order each took - which is the thing the fixes attack. It is always captured in the
system_healthextended-events session. Reproducing it works in a lab, but in production the graph is already there. - Why does an ever-increasing key produce a
PAGELATCHconvoy, and what are the fix families? Every insert lands on the same physical last page, so concurrent inserters serialise on one buffer latch - throughput plateaus while CPUs idle, all waitingPAGELATCH_EXon one hot page. The fixes attack the “one hot page” property: a non-sequential key (hash, reversed,OPTIMIZE_FOR_SEQUENTIAL_KEY) spreads the inserts, while the tempdb allocation-page variant is solved by more files, not by the key. If you proposed more files for a hot index page, reread the convoy section - files fix the allocation-page variant only.
Where SQL Server sits
How this component relates to the rest of the stack. Items without a link are not covered here yet.
Deployed with
Commonly run alongside
- Windows Failover Clustering