Magento Image Cache Lock Contention Issue
Published: June 17, 2020
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
.
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.
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