The PHP OPcache is a PHP module that caches the PHP code of the web application (like Shopware or Magento) in memory.
What is OPcache?
The PHP OPcache is a PHP module that caches the PHP code of the web application (like Shopware or Magento) in memory. This makes them run much faster and the PHP scripts don't need to be parsed every time they are executed.
How to check the PHP OPcache?
The current status of the PHP OPcache can be easily checked via Samuel Gordalina's cachetool. It is important here that this must be done via the respective socket of FPM.
A command like
cachetool opcache:status --fcgi=/var/run/php7.4-fpm-default.sock
shows among other things the current occupancy and the hitrate.
What’s OPcache preloading?
Since PHP 7.4 it is possible to specify a script to be parsed directly via OPcache preloading when starting PHP. This can be used to ensure that all relevant PHP files are quickly available even after a deployment. The configuration for this is done via the variables opcache.preload and opcache.preload_user. This function should be used with caution, because after preloading all loaded functions and classes are available within PHP - even if they were not explicitly included in the PHP script that called them. More details about preloading can be found here: https://www.php.net/manual/de/opcache.preloading.php
What is the OPcache Just in Time Compiler (JIT)?
With PHP 8.0, the PHP OPcache was extended by a JIT. While the bytecode stored in the OPcache is still interpreted by the PHP VM, this step is omitted with the JIT. The JIT can therefore store frequently used code paths directly in machine code, which significantly improves performance for computationally intensive PHP applications. However, this effect is much less pronounced for classic PHP applications, which are heavily dependent on the database and similar services.
To use the JIT, it is sufficient to increase the opcache.jit_buffer_size value in the PHP default settings.
Which parameters should be configured and how?
In our experience, the following four settings are very important:
opcache.memory_consumption opcache.max_accelerated_files opcache.interned_strings_buffer opcache.revalidate_freq
For the first three settings, the optimal values depend on the PHP setting used. The opcache.revalidate_freq value controls how often a PHP file is checked for changes. opcache.memory_consumption controls the maximum amount of memory that can be used for the OPcache. Here you have to make sure that there is enough free memory available. For Shopware and Magento stores usually 512MB is sufficient. For smaller PHP projects less memory is sufficient.
opcache.max_accelerated_files controls the maximum number of files to be cached by the OPcache. The given number is rounded up to one of the following values:
In our experience, 65407 files are sufficient for Shopware and Magento stores.
Besides caching complete PHP files, OPcache can also cache strings. This is done via the value opcache.interned_strings_buffer. In our experience, Magento and Shopware stores get by with 64MB.
The value opcache.revalidate_freq controls how often it is checked whether a PHP file has changed. A value of 60, therefore, ensures that every 60 seconds is checked whether the PHP files have changed. For a busy store, we recommend a setting of at least 60. Alternatively, it is also possible to disable the check completely using the opcache.validate_timestamps setting. In this case, modified PHP scripts are only applied during a PHP reload or restart. As of PHP 8.0, it is also worth setting opcache.jit_buffer_size to benefit from JIT.
What to consider when deploying?
To free the OPcache from old data, we generally recommend emptying the OPcache completely after each deployment. This can be done automatically by a PHP reload via our cluster-control tool.
OPcache at maxcluster
At maxcluster, you can manage the OPcache settings yourself in the Application Center under Webserver > Apache or Webserver > NGINX via the customer interface.
If you have any questions about our OPcache or need support in adapting it to your application, please contact us at email@example.com.