Everything you ever wanted to ask but were too shy Deadlocks Everything you ever wanted to ask but were too shy Denis Reznik Data Architect at Intapp, Inc. Microsoft Data Platform MVP
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 Hidden Deadlock NOLOCK Deadlock One Query Deadlock Undetectable Deadlock
Classic Deadlock DEADLOCK! wait wait ID City 1 Kyiv 2 Ljubljana 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 Ljubljana 3 Dnipropetrovsk X wait wait ID User City_Id 1 Dejan Sarka 2 Mladen Prajdic 3 Matija Lah 4 John Smith X
How to Deal with the Deadlock? Catch the Deadlock Find the Root Cause Fix the Deadlock
Demo Common Deadlock
Lock Types - Shared X S S
Lock Types - Exclusive S X X
Deadlocks. When Graph does not make sense. Demo Deadlocks. When 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 = 'Ljubljana' ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 3 Ljubljana 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 = 'Ljubljana'
REPEATABLE READ ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 ID BEGIN TRAN SELECT * FROM Users WHERE City = 'Ljubljana' ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 3 Ljubljana 4 5 Tokyo 6 New York 7 UPDATE Users SET City = 'Kyiv' WHERE Id = 2 X S S ... S SELECT * FROM Users WHERE City = 'Ljubljana' COMMIT
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 Range Deadlock
PHANTOM RECORDS Phantom Record ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo BEGIN TRAN SELECT * FROM Users WHERE City = 'Ljubljana' ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 Ljubljana 3 4 8 5 Tokyo 6 New York 7 INSERT INTO Users VALUES (8, 'Ljubljana') S S ... S S SELECT * FROM Users WHERE City = 'Ljubljana' Phantom Record
SERIALIZABLE ID City 1 Kyiv 2 Ljubljana 3 4 8 5 Tokyo 6 New York 7 ID BEGIN TRAN SELECT * FROM Users WHERE City = 'Ljubljana' ID City 1 Kyiv 2 Ljubljana 3 4 8 5 Tokyo 6 New York 7 ID City 1 Kyiv 2 Ljubljana 3 4 5 Tokyo 6 New York 7 INSERT INTO Users VALUES (8, 'Ljubljana') RANGE S-S ... SELECT * FROM Users WHERE City = 'Ljubljana' COMMIT
Demo Range Deadlock
READ COMMITTED SNAPSHOT SELECT * FROM Users WHERE City = 'Kyiv' BEGIN TRAN UPDATE Users SET City = 'Ljubljana' WHERE City = 'Kyiv' ID City 1 Ljubljana 2 3 4 5 6 New York 7 ID City 1 Kyiv 2 Ljubljana 3 4 5 6 New York 7 X SELECT * FROM Users WHERE City = 'Kyiv' Version Store ID City 1 Kyiv tempdb
SNAPSHOT ID City 1 Ljubljana 2 3 4 5 6 New York 7 ID City 1 Kyiv 2 BEGIN TRAN UPDATE Users SET City = 'Ljubljana' WHERE City = 'Kyiv' BEGIN TRAN UPDATE Users SET City = 'London' WHERE City = 'Kyiv' X COMMIT Version Store ID City 1 Kyiv tempdb
Demo Implicit Deadlock
READ UNCOMMITTED (NOLOCK) SELECT * FROM Users WHERE City = 'Kyiv' ID City 1 Kyiv 2 Ljubljana 3 4 5 6 7 ID City 1 Kyiv 2 Ljubljana 3 4 5 6 New York 7 BEGIN TRAN UPDATE Users SET City = 'Kyiv' WHERE City = 'New York' SELECT * FROM Users WHERE City = 'Kyiv' ROLLBACK Dirty Read X X SELECT * FROM Users WHERE City = 'Kyiv'
Demo NOLOCK 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 25 |
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 26 |
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