The deadlock issue is one of the biggest and most difficult issues to fix on Magento 2, every Magento developer comes across it sooner or later. Once you start to search for how to fix deadlock issues in Magento, you will have a hard time finding an ultimate answer. 

So, in this article, we've added everything you need to fix deadlock issues or at least significantly reduce them.

Usually, the deadlock issue happens when a lot of resources are engaged with the product, like:

So, basically fixing Magento deadlock issues does down to making product saving faster.

1. Measure Product Saving Execution Time

To do this create the scripts folder in your Magento 2 root directory. Then add the test.php file to it with the following code:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get(Magento\Framework\App\State::class);
$state->setAreaCode('adminhtml');

$startTime = microtime(true);

$productRepo = $obj->get(Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepo->getById(39);
$product->setName($product->getName() . ' - test');
$product->setStoreId(0);
$productRepo->save($product);

$endTime = microtime(true);
$executionTime = ($endTime - $startTime);

echo " Execution time of script = ".$executionTime." sec";
?>

Then run php scripts/test.php so it measures the time required to save one product via the product repository.

Scripts.test.php

On vanilla Magento 2.4.4, it takes less than one sec. For regular Magento 2 stores, the acceptable value is up to 2 sec.

If product saving is executed in less than 2 sec, you can skip this part and go to the next section.

If you need to find out what extension causes this issue, the easiest way to do it is:

  1. Disable all third-party Magento 2 extensions and measure the time it takes to save a product.
  2. Enable all extensions one by one, measuring the product saving speed.

Doing this step by step for each extension you will find out what extension has the biggest impact on your performance.

2. Check Database Configuration

Magento recommends using the following DB configurations for better performance. So if you're experiencing the Magento deadlock issues, check it out.

My SQL Config Requirements in Magento

You might also face the following issue: 

SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction, query was...

You can try to change the lock node within your app/etc/env.php file and change the prefix within the inner config node.

<?php
return [
'lock' => [
       'provider' => 'db',
       'config' => [
'prefix' => 'new-prefix-here'
    ]
]
];

Then just reset the indexers and try to reindex again.

3. Monitor Cron Execution

If Magento deadlocks issues happen during the execution of the cron job, we strongly recommend installing some Magento 2 cron schedule extensions.

They allow you to view the cron job execution chart and change the cron job schedule to reduce the impact on the CPU and the number of requests to the database at the same time.

If you see that you have a few extensive processes (longer than 10 min) being executed at the same time try to reschedule them to distribute the load evenly.

 

Product Saving Time Via Repository