Deadlocks Everything you ever wanted to ask but were too shy Denis Reznik Deadlocks Everything you ever wanted to ask but were too shy
Sponsors Gold Sponsors Innovation Sponsor PASS Bronze Sponsors
About Me Denis Reznik Kyiv, Ukraine Data Architect at Intapp, Inc. Microsoft Data Platform MVP Co-Founder of Ukrainian Data Community Kyiv PASS Regional Mentor, CEE Co-author of “SQL Server MVP Deep Dives 2”
Agenda Classic Deadlock Common Deadlock When Deadlock Graph doesn’t Make Sense Object Deadlock Range Deadlock Implicit Deadlock NOLOCK Deadlock Hidden Deadlock One Query Deadlock Undetectable Deadlock
Agenda – More Background Locks (S) Shared (X) Exclusive (U) Update (Range*-*) Range (I*) Intent (Sch-S) Schema Stability (Sch-M) Schema Modification (BU) Bulk Update
Agenda – More Background Transaction Isolation Levels Pessimistic READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE Optimistic READ COMMITTED SNAPSHOT SNAPSHOT
Classic Deadlock DEADLOCK! wait wait ID City 1 Kyiv 2 Sofia 3 BEGIN TRAN UPDATE Users SET CityId = 2 WHERE Id = 4 UPDATE City SET Name = 'Dnipro' WHERE Id = 3 BEGIN TRAN UPDATE City SET Name = 'Dnipro' WHERE Id = 3 UPDATE Users SET CityId = 2 WHERE Id = 4 ID City 1 Kyiv 2 Sofia 3 Dnipropetrovsk X wait wait ID User City_Id 1 Dejan Sarka 2 Mihail Mateev 3 Marcin Sheliga 4 John Smith X
Demo Common Deadlock
Lock Types - Shared X S S
Lock Types - Exclusive S X X
Demo Common Deadlock
Demo When Deadlock Graph does not make sense
Lock Types - Update s U U X S U X X U
Return different results so they are non-repeatable NON-REPEATABLE READS BEGIN TRAN SELECT * FROM Users WHERE City = 'Sofia' ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 3 Sofia 4 5 Tokyo 6 New York 7 UPDATE Users SET City = 'Kyiv' WHERE Id = 2 X S Return different results so they are non-repeatable S ... S SELECT * FROM Users WHERE City = 'Sofia'
REPEATABLE READ ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 ID BEGIN TRAN SELECT * FROM Users WHERE City = 'Sofia' ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 3 Sofia 4 5 Tokyo 6 New York 7 UPDATE Users SET City = 'Kyiv' WHERE Id = 2 X S S ... S SELECT * FROM Users WHERE City = 'Sofia' COMMIT
Demo When Deadlock Graph does not make sense
Demo Object Deadlock
Lock Escalation … >= 25% 1 2 Table S S Buffer Pool n = 5000 Memory for locks 1 S >= 25% 2 S … 3 S n S n = 5000 if lock can’t be put on table, process will try this every n + 1250 rows
Lock Types – Intent locks IS IS S
Demo Object Deadlock
Demo Range Deadlock
PHANTOM RECORDS Phantom Record ID City 1 Kyiv 2 Sofia 3 4 8 5 Tokyo 6 BEGIN TRAN SELECT * FROM Users WHERE City = 'Sofia' ID City 1 Kyiv 2 Sofia 3 4 8 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 INSERT INTO Users VALUES (8, 'Sofia') S S ... S S SELECT * FROM Users WHERE City = 'Sofia' Phantom Record
SERIALIZABLE ID City 1 Kyiv 2 Sofia 3 4 8 5 Tokyo 6 New York 7 ID City BEGIN TRAN SELECT * FROM Users WHERE City = 'Sofia' ID City 1 Kyiv 2 Sofia 3 4 8 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 Sofia 3 4 5 Tokyo 6 New York 7 INSERT INTO Users VALUES (8, 'Sofia') RANGE S-S ... SELECT * FROM Users WHERE City = 'Sofia' COMMIT
Demo Range Deadlock
Demo Implicit Deadlock
READ COMMITTED SNAPSHOT SELECT * FROM Users WHERE City = 'Kyiv' BEGIN TRAN UPDATE Users SET City = 'Sofia' WHERE City = 'Kyiv' ID City 1 Sofia 2 3 4 5 6 New York 7 ID City 1 Kyiv 2 Sofia 3 4 5 6 New York 7 X SELECT * FROM Users WHERE City = 'Kyiv' Version Store ID City 1 Kyiv tempdb
Demo Implicit Deadlock
Demo NOLOCK Deadlock
READ UNCOMMITTED (NOLOCK) SELECT * FROM Users WHERE City = 'Kyiv' ID City 1 Kyiv 2 Sofia 3 4 5 6 New York 7 ID City 1 Kyiv 2 Sofia 3 4 5 6 7 BEGIN TRAN UPDATE Users SET City = 'Kyiv' WHERE City = 'New York' SELECT * FROM Users WHERE City = 'Kyiv' Dirty Read ROLLBACK X X SELECT * FROM Users WHERE City = 'Kyiv'
Demo NOLOCK Deadlock
Demo Hidden Deadlock
Demo One Query Deadlock
Demo Undetectable Deadlock
Three Rules of Deadlocks If deadlock is possible, it will occur. Deadlock should not be solved, before the root cause of it wasn’t found. There are no unsolvable deadlocks. But there can be solutions, which will not suit you completely
How to Avoid? Design database so, that it will be no possibility for a deadlock occur Ha-ha-ha Modify tables in the same order Choose appropriate Transaction Isolation Level Consider optimistic ones Keep transactions as small as possible Use stress-testing for your application or database
Sponsors Gold Sponsors Innovation Sponsor PASS Bronze Sponsors
Thank You! Denis Reznik Twitter: @denisreznik Email: denisreznik@gmail.com Blog: http://reznik.uneta.com.ua Facebook: https://www.facebook.com/denis.reznik.5 LinkedIn: http://ua.linkedin.com/pub/denis-reznik/3/502/234