OPcache keeps compiled PHP code in memory, so PHP does not reparse Magento’s thousands of files on every request. Magento has a very large codebase, and PHP’s default OPcache settings are far too small for it.

Why a full OPcache hurts

When OPcache runs out of file slots or memory, it does not drop a few old entries. It resets completely and recompiles everything from scratch, which stalls the store while every request waits.

Recommended settings for Magento

In the php.ini used by PHP-FPM (for example /etc/php/8.2/fpm/conf.d/10-opcache.ini):

opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=200000
opcache.validate_timestamps=0
opcache.save_comments=1

save_comments=1 is required, because Magento reads information from PHP comments. Then reload PHP-FPM:

sudo systemctl reload php8.2-fpm

About validate_timestamps=0

This makes PHP faster in production, because it stops checking files for changes. The catch: after every deploy you must reload PHP-FPM, or the old code keeps running. If you edit code directly on the server, keep it at 1.

Check how full it is

The command line has its own OPcache, so check through the web. Create a temporary file with <?php print_r(opcache_get_status(false));, open it in the browser, and delete it right after. Look at the number of cached scripts compared with the limit, and at the hit rate, which should be close to 100%.

Catch it automatically

MageMonitor checks for this every few minutes as part of its Cache Health checks, and alerts you by email, Slack or webhook the moment it appears, with the exact commands to fix it. See MageMonitor for Magento 2.

Related fixes