Lesson 45/100

Tutorials MySQL Tutorial

Deadlocks — Complete Guide

Deadlocks — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.

On this page

MySQL Tutorial · Lesson 45 of 100

Deadlocks

Basics ✓Advanced

Advanced · 2 — Production · ~6 min · MySQL — Transactions & Concurrency

What is this?

A deadlock is when two transactions each hold a lock the other needs — InnoDB detects the cycle and rolls back one transaction (victim).

Why should you care?

Concurrent transfers between accounts A↔B in opposite order cause deadlocks — apps must retry, not assume success first try.

See it live — copy this example

Run in MySQL Workbench or the mysql CLI.

-- Session 1                    -- Session 2
START TRANSACTION;                START TRANSACTION;
UPDATE accounts SET balance_inr = balance_inr - 100 WHERE account_id = 1;
                                  UPDATE accounts SET balance_inr = balance_inr - 100 WHERE account_id = 2;
UPDATE accounts SET balance_inr = balance_inr + 100 WHERE account_id = 2;
                                  UPDATE accounts SET balance_inr = balance_inr + 100 WHERE account_id = 1;
-- One session gets ERROR 1213 Deadlock found

What happened?

  • Each session locks one account then waits for the other — circular wait.
  • InnoDB kills one txn; survivor continues.
  • Fix: always lock accounts in same id order.

Practice next

  1. Two Workbench tabs with accounts table.
  2. Run opposing UPDATE order as above.
  3. Note 1213 error and which txn rolled back.
  4. SHOW ENGINE INNODB STATUS after deadlock — read LATEST DETECTED DEADLOCK.
  5. Use SELECT ... FOR UPDATE in fixed id order before updates.

Remember

Deadlock = circular lock wait. InnoDB picks a victim and rolls back. Consistent lock ordering prevents many deadlocks.

DataFlow concurrent transfers

API retries transfer up to 3 times on 1213 during festival traffic.

Outcome: Users see success after harmless automatic retry.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain SQL queries in the context of MySQL.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using MySQL?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a MySQL application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a MySQL project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

MySQL Tutorial
Course syllabus

MySQL Tutorial

MySQL — Foundations
MySQL — Queries & Clauses
MySQL — Joins & Relationships
MySQL — Functions & Window Functions
MySQL — Transactions & Concurrency
MySQL — Stored Procedures & Triggers
MySQL — Indexing & Performance
MySQL — Advanced MySQL
MySQL — Security & Cloud MySQL
MySQL — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details