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 loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.