Deadlocks
A deadlock is two transactions each waiting for a lock the other holds. SQL Server breaks it by cancelling one of them.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Nov 26, 2026
Learning objectives#
- Explain how a deadlock forms
- Find deadlock graphs in system_health
- Read a deadlock graph
- Prevent and handle deadlocks
Explanation#
Transaction A locks row 1 and wants row 2; transaction B locks row 2 and wants row 1. Neither can continue. Every few seconds SQL Server’s lock monitor detects the cycle, picks a victim (usually the cheaper one to roll back), and fails it with error 1205.
SQL Server records every deadlock in the built-in system_health Extended Events session. In SSMS: Management → Extended Events → Sessions → system_health → package0.event_file, filter on xml_deadlock_report, and open the Deadlock tab to see the graph.
Fixes: touch tables in the same order in every procedure; keep transactions short; add indexes so updates lock fewer rows; consider RCSI so readers don’t take shared locks.
Deadlocks can never be removed completely under load, so applications should retry a transaction that fails with 1205 (a short delay, two or three attempts).
Step-by-step screens#
Drawings of SQL Server 2022 Setup and SQL Server Management Studio (SSMS 20) with the ShopDB sample database. The red numbers match the numbered actions under each picture; names on your computer will differ.
Loading pictures…
Code example: Find recent deadlocks from system_health#
SELECT
xed.value('@timestamp', 'datetime2') AS DeadlockTime,
xed.query('.') AS DeadlockGraph
FROM (
SELECT CAST(event_data AS XML) AS x
FROM sys.fn_xe_file_target_read_file('system_health*.xel', NULL, NULL, NULL)
WHERE object_name = 'xml_deadlock_report'
) AS t
CROSS APPLY t.x.nodes('/event') AS e(xed)
ORDER BY DeadlockTime DESC;
DeadlockTime DeadlockGraph ------------------------- ----------------------------------------- 2026-11-26 10:41:07.233 <event name="xml_deadlock_report" ...> (click to open)
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- system_health keeps deadlock reports without any setup.
- The query reads its .xel files.
- Each row is one deadlock with the time.
- Click the XML in SSMS, save it as .xdl and reopen to see the graph.
Real-world use case#
Checkout and stock-transfer procedures updated Orders and Products in opposite orders and deadlocked a few times an hour. Changing one to the same order (Products first) removed them.
Common mistakes#
Retry the transaction automatically; the second attempt almost always succeeds.
Write-write deadlocks still happen and you add dirty reads. Fix access order and indexing.
Best practices#
- Same table order everywhere
- Short transactions
- Index foreign keys and update predicates
- Retry on 1205 in the app
Remember this#
Deadlock = circular wait.
Victim gets error 1205.
system_health has the graphs.
Practice exercise#
Your task: Name the error number your application should retry for a deadlock victim.
Show solution
1205
Mini quiz#
What does SQL Server do when it detects a deadlock?
Want more? Practise all SQL Server MCQs · SQL Server interview questions
My notes#
Saved in this browser.