Lesson 2 of 2 Blocking and Deadlocks

Diagnosing and Resolving Deadlocks

This lesson explains how deadlocks are resolved, how to capture deadlock graphs and how to prevent common deadlock patterns.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how the engine chooses and rolls back a deadlock victim.
  • Describe how to capture deadlock graphs in Azure SQL Database.
  • Identify fixes for common deadlock patterns.

How deadlocks are resolved

In a deadlock, session A holds a lock that B needs while B holds a lock that A needs, so neither can proceed. The lock monitor detects the cycle, usually within seconds, chooses a deadlock victim (by default the session whose rollback costs least, unless SET DEADLOCK_PRIORITY says otherwise), rolls back its transaction and returns error 1205 to it. The other session continues. Because the victim's work is rolled back completely, applications should retry the transaction.

The following diagram shows the lock cycle and its resolution.

How a deadlock is resolved. Session A holds a lock on row 1 and waits for row 2, while session B holds row 2 and waits for row 1, so neither can proceed. The lock monitor detects the cycle, chooses a victim, by default the session whose rollback costs least unless DEADLOCK_PRIORITY says otherwise, rolls back its transaction and returns error 1205. The other session continues, and the application should retry the victim's transaction.
A deadlock is a cycle; one session must be rolled back.

Capturing deadlock graphs

The deadlock graph is XML that lists the processes, the statements they ran, the resources (keys, pages, objects) and which locks each held and requested. On SQL Server, the system_health extended event session records it. In Azure SQL Database, the Deadlocks category of diagnostic settings sends graphs to Log Analytics, and a database-scoped extended event session captures them in a ring buffer, as the following example shows.

CREATE EVENT SESSION DeadlockCapture ON DATABASE
ADD EVENT sqlserver.database_xml_deadlock_report
ADD TARGET package0.ring_buffer;
ALTER EVENT SESSION DeadlockCapture ON DATABASE STATE = START;

SELECT CAST(t.target_data AS xml) AS deadlocks
FROM sys.dm_xe_database_session_targets AS t
JOIN sys.dm_xe_database_sessions AS s ON s.address = t.event_session_address
WHERE s.name = 'DeadlockCapture';
Scenario questions

Capturing deadlocks

Exam skills 2.2.4

Choose where to find deadlock graphs on each platform.

Log in to try this activity Interactive activities are free with an account.

Preventing deadlocks

Most deadlocks come from a few patterns, each with a standard fix, as the following table shows.

PatternFix
Two procedures update the same tables in opposite ordersAccess objects in the same order everywhere
Read a row, then update it, in two concurrent transactions (conversion deadlock)Read with UPDLOCK, or combine into one UPDATE
A seek on a nonclustered index followed by a key lookup that conflicts with an updateMake the index covering so no lookup is needed
Long transactions holding many locksShorten transactions and batch large changes
Readers deadlocking with writersUse RCSI or SNAPSHOT so readers do not take shared locks

Even with good design, occasional deadlocks remain possible under high concurrency, so code that modifies data should catch error 1205 and retry the transaction a limited number of times.

Match the pairs

Deadlock patterns

Exam skills 2.2.4

Match each deadlock pattern with its standard fix.

Log in to try this activity Interactive activities are free with an account.
Worked example

Transfers that deadlock

A procedure transfers stock by updating the source warehouse row, then the target warehouse row. When two transfers between the same two warehouses run in opposite directions at the same time, one fails with error 1205. What should the developer change?

Update the two rows in a consistent order, for example always the lower WarehouseID first, regardless of transfer direction, and add retry logic for 1205. With opposite orders, each transaction locks the row the other needs next, which forms a cycle. Raising DEADLOCK_PRIORITY only chooses which transfer fails, and NOLOCK does not apply to the exclusive locks taken by updates.

Check yourself

What happens to the deadlock victim's transaction?

Show answer

It is rolled back completely and error 1205 is returned, so the application should retry it.

Exam Tips

Remember for the exam

  • Error 1205: chosen as deadlock victim; the transaction was rolled back, so retry it.
  • Same objects in opposite orders is the classic deadlock; use a consistent order.
  • Azure SQL Database deadlock graphs: Deadlocks diagnostic category or database_xml_deadlock_report.
  • DEADLOCK_PRIORITY chooses the victim; it does not prevent deadlocks.