SQL Server · Reliability & Concurrency

Deadlocks

A deadlock is two transactions each waiting for a lock the other holds. SQL Server breaks it by cancelling one of them.

Advanced 5 min Lesson 38 of 44Examples run on SQL Server 2022 and Azure SQL Database.
0/44 in SQL Server
On this page · 5 min · Advanced

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#

SQLFind recent deadlocks from system_health Illustrative
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;
Expected output
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#

  1. system_health keeps deadlock reports without any setup.
  2. The query reads its .xel files.
  3. Each row is one deadlock with the time.
  4. 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#

Showing error 1205 to users

Retry the transaction automatically; the second attempt almost always succeeds.

Using NOLOCK to “fix” deadlocks

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
Text Illustrative
1205

Mini quiz#

Deadlocks quizMultiple choice Question 1 of 1

What does SQL Server do when it detects a deadlock?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.