Magento Image Cache Lock Contention Issue

Published: June 17, 2020

Tags:

During a recent Magento v2.3.5 upgrade (which was ultimately rolled back), we saw severely degraded performance when rolling back the the previous version. In New Relic we could see the most time consuming transaction was /unknown.

Screenshot most time consuming transactions in New Relic

Reviewing the transaction traces we could see that these were requests to generate resized images, and that almost all of the time was being spent on the Magento\Framework\Filesystem\Driver\File::fileLock function.

Screenshot of transaction trace in New Relic

It turns out that each request to generate a cached image (via get.php), attempts to acquire an exclusive lock on the var/resource_config.json file.

// Magento\MediaStorage\Model\File\Storage\Config::save
$file = $this->rootDirectory->openFile($this->rootDirectory->getRelativePath($this->cacheFilePath), 'w');
try {
    $file->lock();
    $file->write(json_encode($this->config));
    $file->unlock();
    $file->close();
} catch (FileSystemException $e) {
    $file->close();
}

This can lead to major lock contention issues if, for one reason or another, Magento needs to generate a large number of resized images (e.g. image cache deleted, hash generation logic change). Magento support was able to provide us with patch MDVA-26024 for this issue. The issue also looks to be resolved in the current 2.4-develop branch

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but also dabbles in Ruby and Go. Technical topics that interest me are monitoring, security and performance.

During the day I solve challenging technical problems at Something Digital where I mainly work with the Magento platform. I also blog about tech, work on open source and hunt for bugs.

If you'd like to get in touch with me the best way is on Twitter.