What you will be able to do
- Trace a blocking chain to the head blocker and name the operation pair that created it.
- Read a deadlock graph and fix the access order, not the victim.
- Explain the index read-write trade-off as a concrete cost, not a slogan.
- Choose RCSI, locking, or scheduling for a mixed read-write workload.
- Describe the shared-resource ceiling - log, tempdb, CPU - that every operation fights over.
Concepts covered
The per-operation lessons tune one statement. This one is about what happens when they run together - because on a real system every operation shares locks on the data, latches on the hot pages, the log device, tempdb, and the schedulers, and a “fix” for one is often a new cost for another.
The analogy is a shared kitchen at service time. One cook pinning a recipe card to read it stops another cook updating the card - a reader blocking a writer - and a cook rewriting the card holds the readers in turn. Two cooks who each grab one utensil the other needs and then wait are a deadlock, resolved by the head chef making one of them put theirs down. And even with no utensil in dispute, a whole brigade sharing one sink all washes slower - the shared substrate that makes every operation pay for the load of the others.
The four interference patterns
- Reader blocks writer - under locking READ COMMITTED a
SELECT’s shared locks hold a row against anUPDATE’s exclusive lock. A long report pins rows an updater needs; the writer queues. - Writer blocks reader - the writer’s
Xlock makes the reader’sSwait. Under locking semantics every read of a hot row pays the writer’s commit latency. - Writer blocks writer - two
UPDATEs, or an insert and an update on the same key, serialise on the row lock. Two inserts on a sequential key fight the latch on the last page. - Everyone fights the substrate - a mass delete and a reporting query share the log device, tempdb, and CPU. No lock is contended, yet both slow down.
The blocking investigation
Blocking and deadlock - the investigation
See the chain
dm_exec_requests: blocking_session_id per waiter
Follow the chain up to the head blocker - the session waiting on nothing
Name the pair
Which operations conflict - reader/writer or writer/writer
The wait type says lock (LCK_M) or latch (PAGELATCH) - different fixes
Deadlock?
A cycle - the graph in system_health names the two resources
Each took the locks in a different order - that order is the bug
Fix the order
Consistent access order, shorter transactions, or narrower locks
Kill the cycle at the ordering, not the application that happened to be the victim
Deadlocks - the access-order bug
A deadlock is two transactions holding what the other wants, resolved by the engine killing a
victim (chosen by rollback cost or DEADLOCK_PRIORITY). The fix is almost never the statement that
died - it is the inconsistent order the two paths took through the shared objects. If session A
updates orders then order_items, and session B updates order_items then orders, the cycle
is guaranteed eventually. Access shared objects in one consistent order and the cycle cannot form.
The index read-write trade-off, made concrete
Every index is a transaction between reads and writes:
| What the index buys | What it charges |
|---|---|
| A seek instead of a scan for reads | An extra key write, possible page split, on every INSERT |
| A covered query with no lookup | The INCLUDE columns widen every leaf - heavier writes |
| A filtered index for a hot subset | It serves only queries that prove the same WHERE clause |
The balance is set by the read-write ratio and the contention: on a write-hot table, a “read”
index that is rarely used costs write throughput for nothing. The audit
sys.dm_db_index_usage_stats shows seeks versus writes per index - an index with heavy writes and
near-zero seeks is pure write tax.
The levers that balance it
- RCSI - the highest-leverage reader/writer fix: readers read row versions, take no
Slocks, and stop blocking writers entirely. The cost is version-store traffic in tempdb and a 14-byte version tag per row. On a mixed OLTP workload this usually removes the reader-writer class of contention outright. - Consistent transaction order - free, and it removes whole classes of deadlock.
- Shorter transactions - the lock window shrinks; everything downstream queues less.
- Scheduling and governance - run the mass purge and the report off-peak, or put the report in a Resource Governor pool so it cannot starve the writers.
- Connection pooling and concurrency limits - an application that opens a thousand connections creates a thousand schedulable requests all contending; a bounded pool keeps active concurrency inside what the resources can serve.
The substrate every operation shares
When the operations stop blocking each other, they still share three things that can each become the real ceiling: the transaction log (every write’s flush), tempdb (version store, work tables, spills), and the schedulers (all the CPU there is). The internals lesson is where those shared resources are diagnosed when the contention is not a lock at all.
Guided practice
A beginner exercise, worked step by step in a scratch database. It reproduces the blocking chain and the deadlock the lesson describes, using two query windows against the same scratch tables.
- Build the two tables. Run
CREATE TABLE dbo.orders (id INT PRIMARY KEY, val INT NOT NULL);andCREATE TABLE dbo.order_items (id INT PRIMARY KEY, order_id INT NOT NULL, val INT NOT NULL);and add a handful of rows to each. Check: two tables a transaction can touch in either order - the deadlock needs that. - Create a reader-writer block. In session A run
BEGIN TRAN; UPDATE dbo.orders SET val = val + 1 WHERE id = 1;and leave the transaction open. In session B runSELECT * FROM dbo.orders WHERE id = 1;Check: session B hangs on anLCK_Mwait - the writer’sXlock blocks the reader’sS. If B returned immediately,READ_COMMITTED_SNAPSHOTis on for the scratch database - which is the RCSI behaviour the levers section describes, where the same read never queues. - Trace the chain to the head. While B waits, query
sys.dm_exec_requestsand readwait_typeandblocking_session_idfor the waiting session. Check: B shows anLCK_Mwait blocked by A, and A waits on nothing - A is the head blocker, the transaction that owns the problem. If you identified B as the problem, revisit the blocking investigation - the waiter is the symptom, not the cause. - Force a deadlock. Session A:
BEGIN TRAN; UPDATE orders ... WHERE id = 1; WAITFOR DELAY '00:00:05'; UPDATE order_items ...Session B: the same shape butorder_itemsfirst, thenorders. Start them inside the delay window so both first locks are held. Check: one session is chosen as the victim and killed, and thesystem_healthgraph names the two resources. If both completed, the timing missed - widen the delay so both first locks are held together, and revisit the deadlock section. - Fix the order, not the victim. Rerun both sessions touching
ordersfirst andorder_itemssecond. Check: no deadlock - one session simply waits for the other’s commit, because a consistent access order means the cycle cannot form. Revisit “fix the order” if you expected the engine to resolve it some other way.
Check your understanding
Ordered easy to hard. Answer each before reading the answer; a wrong answer names where to look.
- Under locking
READ COMMITTED, why does a writer block a reader? The reader’s shared lock is incompatible with the writer’s exclusive lock, so the reader waits out the writer’s commit latency. Revisit the four interference patterns if you said readers never wait - that is the row-versioning behaviour, not the locking default. - A deadlock graph shows two sessions deadlocked on
ordersandorder_items. Where is the bug? In the access order - each transaction took the locks in a different sequence, so a cycle was guaranteed eventually. The victim the engine killed is incidental. Revisit the deadlock section if your fix was “retry the killed statement”. - What does RCSI remove, and what does it charge? It removes reader-writer blocking in both directions by serving readers a row version, and it charges version-store traffic in tempdb plus a 14-byte version tag per row. Revisit the levers if you called it free.
- An index shows heavy writes and near-zero seeks in
sys.dm_db_index_usage_stats. What is it costing? Pure write tax - everyINSERTpays a key write and a possible page split for a read benefit no query takes. Revisit the read-write trade-off if you said indexes are cheap. - No session is blocked and no lock wait exists, yet a mass purge and a report still slow each other down. What are they fighting over? The shared substrate - the transaction log, tempdb and the schedulers. Locks explained the earlier contention; this is a shared-resource ceiling, which is why the fix is scheduling, pooling or governance rather than lock tuning. Revisit the substrate section.
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