The curious case of dead-end-lock(Deadlock) | Mysql

Anuj Aneja
3 min readJun 5, 2021

Usually, deadlocks are hard to debug and often the main reason for the occurrence of deadlock is when a set of processes are in a wait state because each process is waiting for a resource that is held by some other waiting process. Therefore, all deadlocks involve conflicting resource needs by two or more processes.

Recently, in the Production system, we found a case wherein one of the API was getting a lot of failure of transaction due to deadlock. Strange thing was that it was not a total freeze deadlock of threads instead out of two conflicting transactions one was getting successfully completed.

After going through the logs and further analysis we conclude the following things:

NOTE: For finding last deadlock details you can use “SHOW Innodb engine Status\G”. This is really helpful in identifying which two transactions and queries are giving us the deadlock condition.

  • Deadlock is occurring at the MYSQL level
  • and MYSQL is doing a deadlock resolution by killing one of the transaction.

Re-production Steps:-

For better, understanding lets list down the steps to re-produce this kind of deadlock:

Open two MYSQL command prompt and create two separate transaction as given below(step by step in order)

Now, you have re-produced the deadlock scenario. Let’s understand the main reason behind the deadlock. Any guesses???

.

.

.

.

The main reason for Deadlock is Step 3 and Step 6. But, both queries are taking a lock on the different parent id? Strange isn’t it!!!

In order to understand this. We need to understand Gap Lock.

Gap Lock:

A gap lock is a lock on a gap between index records, or a lock on the gap before the first or after the last index record.

In addition to foreign-key constraint checking and duplicate key checking, gap locking is enabled for searches and an index scan if the transaction isolation level is above Repeatable Read. (Default one in MYSQL).

This locking mechanism helps to prevent other transactions from inserting into the gap while the transaction reads the range. As a result, InnoDB can prevent Phantom-Read anomaly even if it’s transaction isolation level is Repeatable Read.

Insert Intention Locks:-

An insert intention lock is a type of gap lock set by INSERT operations prior to row insertion. This lock signals the intent to insert in such a way that multiple transactions inserting into the same index gap need not wait for each other if they are not inserting at the same position within the gap.

Client B begins a transaction to insert a record into the gap. The transaction takes an insert intention lock while it waits to obtain an exclusive lock. i.e. Transaction 2(Terminal 2)- client B will wait.

Now coming back to our old example:

  • In our case, Transaction 1(Step 3) is taking a Pessimistic lock on “p.id=1” but this query is taking a left join on the child table (parent_id) foreign key of parent.
  • As there is no record inserted into child table for parent_id=1 so this above given query will take a gap lock on parent_id greater than the last index record in our case it is, all ids greater than 1. Which is making the Step 7 (Transaction 2) query to go into the wait state. (insert into child table, which is taking an `Insert intension lock`)
  • and Step 8(Transaction 8) is also doing a similar operation (insert into child, which is taking an `insert intension lock`) which is also blocked. Fortunately, MYSQL has a deadlock detection & resolution process in place which kills one of the transaction.

Solution

We removed the join of the child table and it worked fine. Instead, we fetch the child in a separate query.

select * from parent p where p.id=1 for update; — Step 3 // Pessimistic lock
select * from child c where c.parent_id=1; //Separate query…

--

--

Anuj Aneja

I am having more than 14 years of exp. in IT. Principal Software Engineer having sound knowledge of various backend techs and distributed systems.