SlideShare ist ein Scribd-Unternehmen logo
1 von 11
Downloaden Sie, um offline zu lesen
Intro to
                          Deadlocks
                            February 4th, 2013
                          Panupan Sriautharawong




Monday, February 11, 13
What are
                   Deadlocks?
                          A situation that occurs in concurrent systems.

                          Happens when two or more competing
                          actions get stuck because they’re waiting on
                          one another to finish.

                          A bug or design flaw in most cases.

                          Preventable!



Monday, February 11, 13
InnoDB
                          Storage Engine




Monday, February 11, 13
InnoDB
                              Storage Engine
                          Transactions
                            Are used to group of database operations
                            ## Bank Transfer Example
                            BEGIN;
                            UPDATE account SET balance=balance+100 WHERE id=1;
                            UPDATE account SET balance=balance-100 WHERE id=2;
                            COMMIT;

                            Are atomic, meaning they can’t broken into
                            smaller pieces. Either everything goes
                            through or nothing does.



Monday, February 11, 13
InnoDB
                              Storage Engine
                          Shared vs Exclusive Locks
                          A lock must be acquired (either implicitly or
                          explicitly) before retrieving or modifying any data.
                          There are two types of locks:
                              - Shared (S)      = Read only
                              - Exclusive (X) = Read/Write

                          Table vs Row-Level Locking
                          Properly defined indexes prevent too
                          many rows from unintentionally getting locked and
                          also ensures optimal performance.


Monday, February 11, 13
Deadlocked Doggies
                                                        Jake

                                                                        Tony
                          Tony starts with 5 bones

                          Jake starts with 1 bone

                          Action #1: Jake steals a bone from Tony

                          Action #2: Tony, being a good doggie, gives
                          Jake a bone so they’ll both have 3



Monday, February 11, 13
Deadlocked Doggies
                     ## INITIAL SETUP
                     CREATE TABLE `doggies` (
                     `id` INT NOT NULL AUTO_INCREMENT,
                                                                                                              Jake
                     `name` VARCHAR(255) NOT NULL,
                     `bones` INT NOT NULL,
                     PRIMARY KEY (`id`)                                                                                        Tony
                     ) ENGINE=InnoDB;

                     INSERT INTO doggies (id, name, bones) VALUES (1, "Tony", 5);
                     INSERT INTO doggies (id, name, bones) VALUES (2, "Jake", 1);


                     ## ACTION #1
                     # Jake steals a bone
                     BEGIN;
                     UPDATE doggies SET bones=bones-1 WHERE id=1;            # X lock acquired on 1


                                                ## ACTION #2
                                                # Tony gives Jake a bone
                                                BEGIN;
                                                UPDATE doggies SET bones=bones+1 WHERE id=2;          # X lock acquired on 2
                                                UPDATE doggies SET bones=bones-1 WHERE id=1;!         # LOCK WAIT!
                                                COMMIT;


                     ## ACTION #1 (continued)
                     UPDATE doggies SET bones=bones+1 WHERE id=2;!            # DEADLOCK!
                     COMMIT;




Monday, February 11, 13
How to Debug’em
                          Inspecting MySQL Status
                          mysql> SHOW ENGINE INNODB STATUS;

                          mysql> SHOW PROCESSLIST;

                          mysql> EXPLAIN SELECT ...;


                          Grepping Rails Logs
                          $ cat log/production.log | grep -B 40 Deadlock


Monday, February 11, 13
------------------------
                                        How to Debug’em
       LATEST DETECTED DEADLOCK
       ------------------------
       130204 13:10:32
       *** (1) TRANSACTION:
       TRANSACTION 214FC, ACTIVE 12 sec starting index read
       mysql tables in use 1, locked 1
       LOCK WAIT 3 lock struct(s), heap size 376, 2 row lock(s), undo log entries 1
       MySQL thread id 182, OS thread handle 0x111c8c000, query id 120976 localhost 127.0.0.1 root Updating
       UPDATE doggies SET bones=bones-1 WHERE id=1
       *** (1) WAITING FOR THIS LOCK TO BE GRANTED:
       RECORD LOCKS space id 0 page no 1130 n bits 72 index `PRIMARY` of table `deadlocks`.`doggies` trx id 214FC lock_mode X locks rec but not gap waiting
       Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
        0: len 4; hex 80000001; asc ;;
        1: len 6; hex 0000000214fb; asc    ;;
        2: len 7; hex 7900000de72774; asc y 't;;
        3: len 4; hex 546f6e79; asc Tony;;
        4: len 4; hex 80000004; asc ;;

       *** (2) TRANSACTION:
       TRANSACTION 214FB, ACTIVE 44 sec starting index read
       mysql tables in use 1, locked 1
       3 lock struct(s), heap size 376, 2 row lock(s), undo log entries 1
       MySQL thread id 184, OS thread handle 0x111e2b000, query id 120977 localhost 127.0.0.1 root Updating
       ## CONNECTION #1
       # Jake steals a bone (cont..)
       UPDATE doggies SET bones=bones+1 WHERE id=2
       *** (2) HOLDS THE LOCK(S):
       RECORD LOCKS space id 0 page no 1130 n bits 72 index `PRIMARY` of table `deadlocks`.`doggies` trx id 214FB lock_mode X locks rec but not gap
       Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
        0: len 4; hex 80000001; asc ;;
        1: len 6; hex 0000000214fb; asc     ;;
        2: len 7; hex 7900000de72774; asc y 't;;
        3: len 4; hex 546f6e79; asc Tony;;
        4: len 4; hex 80000004; asc ;;

       *** (2) WAITING FOR THIS LOCK TO BE GRANTED:
       RECORD LOCKS space id 0 page no 1130 n bits 72 index `PRIMARY` of table `deadlocks`.`doggies` trx id 214FB lock_mode X locks rec but not gap waiting
       Record lock, heap no 3 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
        0: len 4; hex 80000002; asc ;;
        1: len 6; hex 0000000214fc; asc    ;;
        2: len 7; hex 7a00000cf122d8; asc z " ;;
        3: len 4; hex 4a616b65; asc Jake;;
        4: len 4; hex 80000002; asc ;;

       *** WE ROLL BACK TRANSACTION (2)

Monday, February 11, 13
Best Practices
                          Define proper indexes and use EXPLAIN SELECT to
                          verify. For example, make sure to define multi-column
                          indexes across [id, type] on our polymorphic models.

                          Most common method of deadlock exception handling
                          is to retry but this should only be used a last resort.

                          Easiest fix is to rearrange queries to make sure updates
                          occur before selects (not always possible).

                          You can also lock records manually at the beginning of a
                          transaction when you know they’ll need updating.


Monday, February 11, 13
Helpful Resources

                          http://dev.mysql.com/doc/refman/5.0/en/
                          innodb-lock-modes.html

                          http://rhnh.net/2010/07/01/debugging-
                          deadlocks-in-rails

                          http://www.chriscalender.com/?p=426




Monday, February 11, 13

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Intro to Deadlocks

  • 1. Intro to Deadlocks February 4th, 2013 Panupan Sriautharawong Monday, February 11, 13
  • 2. What are Deadlocks? A situation that occurs in concurrent systems. Happens when two or more competing actions get stuck because they’re waiting on one another to finish. A bug or design flaw in most cases. Preventable! Monday, February 11, 13
  • 3. InnoDB Storage Engine Monday, February 11, 13
  • 4. InnoDB Storage Engine Transactions Are used to group of database operations ## Bank Transfer Example BEGIN; UPDATE account SET balance=balance+100 WHERE id=1; UPDATE account SET balance=balance-100 WHERE id=2; COMMIT; Are atomic, meaning they can’t broken into smaller pieces. Either everything goes through or nothing does. Monday, February 11, 13
  • 5. InnoDB Storage Engine Shared vs Exclusive Locks A lock must be acquired (either implicitly or explicitly) before retrieving or modifying any data. There are two types of locks: - Shared (S) = Read only - Exclusive (X) = Read/Write Table vs Row-Level Locking Properly defined indexes prevent too many rows from unintentionally getting locked and also ensures optimal performance. Monday, February 11, 13
  • 6. Deadlocked Doggies Jake Tony Tony starts with 5 bones Jake starts with 1 bone Action #1: Jake steals a bone from Tony Action #2: Tony, being a good doggie, gives Jake a bone so they’ll both have 3 Monday, February 11, 13
  • 7. Deadlocked Doggies ## INITIAL SETUP CREATE TABLE `doggies` ( `id` INT NOT NULL AUTO_INCREMENT, Jake `name` VARCHAR(255) NOT NULL, `bones` INT NOT NULL, PRIMARY KEY (`id`) Tony ) ENGINE=InnoDB; INSERT INTO doggies (id, name, bones) VALUES (1, "Tony", 5); INSERT INTO doggies (id, name, bones) VALUES (2, "Jake", 1); ## ACTION #1 # Jake steals a bone BEGIN; UPDATE doggies SET bones=bones-1 WHERE id=1; # X lock acquired on 1 ## ACTION #2 # Tony gives Jake a bone BEGIN; UPDATE doggies SET bones=bones+1 WHERE id=2; # X lock acquired on 2 UPDATE doggies SET bones=bones-1 WHERE id=1;! # LOCK WAIT! COMMIT; ## ACTION #1 (continued) UPDATE doggies SET bones=bones+1 WHERE id=2;! # DEADLOCK! COMMIT; Monday, February 11, 13
  • 8. How to Debug’em Inspecting MySQL Status mysql> SHOW ENGINE INNODB STATUS; mysql> SHOW PROCESSLIST; mysql> EXPLAIN SELECT ...; Grepping Rails Logs $ cat log/production.log | grep -B 40 Deadlock Monday, February 11, 13
  • 9. ------------------------ How to Debug’em LATEST DETECTED DEADLOCK ------------------------ 130204 13:10:32 *** (1) TRANSACTION: TRANSACTION 214FC, ACTIVE 12 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 3 lock struct(s), heap size 376, 2 row lock(s), undo log entries 1 MySQL thread id 182, OS thread handle 0x111c8c000, query id 120976 localhost 127.0.0.1 root Updating UPDATE doggies SET bones=bones-1 WHERE id=1 *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 0 page no 1130 n bits 72 index `PRIMARY` of table `deadlocks`.`doggies` trx id 214FC lock_mode X locks rec but not gap waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000001; asc ;; 1: len 6; hex 0000000214fb; asc ;; 2: len 7; hex 7900000de72774; asc y 't;; 3: len 4; hex 546f6e79; asc Tony;; 4: len 4; hex 80000004; asc ;; *** (2) TRANSACTION: TRANSACTION 214FB, ACTIVE 44 sec starting index read mysql tables in use 1, locked 1 3 lock struct(s), heap size 376, 2 row lock(s), undo log entries 1 MySQL thread id 184, OS thread handle 0x111e2b000, query id 120977 localhost 127.0.0.1 root Updating ## CONNECTION #1 # Jake steals a bone (cont..) UPDATE doggies SET bones=bones+1 WHERE id=2 *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 0 page no 1130 n bits 72 index `PRIMARY` of table `deadlocks`.`doggies` trx id 214FB lock_mode X locks rec but not gap Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000001; asc ;; 1: len 6; hex 0000000214fb; asc ;; 2: len 7; hex 7900000de72774; asc y 't;; 3: len 4; hex 546f6e79; asc Tony;; 4: len 4; hex 80000004; asc ;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 0 page no 1130 n bits 72 index `PRIMARY` of table `deadlocks`.`doggies` trx id 214FB lock_mode X locks rec but not gap waiting Record lock, heap no 3 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000002; asc ;; 1: len 6; hex 0000000214fc; asc ;; 2: len 7; hex 7a00000cf122d8; asc z " ;; 3: len 4; hex 4a616b65; asc Jake;; 4: len 4; hex 80000002; asc ;; *** WE ROLL BACK TRANSACTION (2) Monday, February 11, 13
  • 10. Best Practices Define proper indexes and use EXPLAIN SELECT to verify. For example, make sure to define multi-column indexes across [id, type] on our polymorphic models. Most common method of deadlock exception handling is to retry but this should only be used a last resort. Easiest fix is to rearrange queries to make sure updates occur before selects (not always possible). You can also lock records manually at the beginning of a transaction when you know they’ll need updating. Monday, February 11, 13
  • 11. Helpful Resources http://dev.mysql.com/doc/refman/5.0/en/ innodb-lock-modes.html http://rhnh.net/2010/07/01/debugging- deadlocks-in-rails http://www.chriscalender.com/?p=426 Monday, February 11, 13