Technical

OPcache

OPcache: the standard PHP extension that caches compiled bytecode in shared memory to skip recompiling on every request — the main PHP accelerator in production.

Trois plaques de verre en lévitation étagées au-dessus d'un bloc-moteur en fonte noire, la plaque supérieure teintée de bordeaux, les deux autres transparentes, sur un sol sombre sous un faisceau blanc
OPcache — Image générée par IA (Qwen-Image 2.1, ComfyUI local)

OPcache is a standard PHP extension, bundled with the language since PHP 5.5 and enabled by default in production, that caches the compiled bytecode of PHP scripts in shared memory. Its job is to eliminate the cost of recompiling on every request: instead of rereading, parsing and compiling the same PHP file thousands of times, OPcache compiles it once and then serves the bytecode from memory. For a PHP application in production, it's the most impactful performance accelerator — and one that many teams know little about.

Technical definition

How OPcache works

On the first request that loads a given PHP file, OPcache does the interpreter's normal work: reading the file, parsing it, compiling it into opcodes, then storing the bytecode in shared memory. Subsequent requests that load the same file skip straight to the last step: OPcache fetches the bytecode from memory and hands it to the Zend VM for execution. The source file on disk is no longer touched.

This shared memory is allocated when PHP-FPM (or whichever SAPI is in use) starts and persists between requests — which is why OPcache only makes sense with a persistent SAPI like PHP-FPM. On the CLI, OPcache is disabled by default, since each run starts and ends independently.

Key settings

  • opcache.enable — turns the extension on or off. Set it to 1 in production.
  • opcache.memory_consumption — size of the shared memory allocated to the cache, in megabytes. A typical value is 128 to 256 for a complex application like WordPress.
  • opcache.max_accelerated_files — maximum number of files the cache can hold. It must be higher than the total number of PHP files in the application (be generous: 10,000 to 20,000).
  • opcache.interned_strings_buffer — memory allocated to string deduplication. It improves memory savings on applications that handle lots of symbols (PHP interns class, function and constant names).
  • opcache.validate_timestamps — determines whether OPcache checks for file changes. 0 in immutable production, 1 in development.
  • opcache.revalidate_freq — revalidation frequency in seconds when validate_timestamps=1. Ignored when validate_timestamps=0.

Current challenges

The immutable filesystem case

OPcache delivers its biggest gains when file revalidation can be turned off entirely. That's exactly what modern PaaS architectures allow: the code is frozen into an immutable image at build time, and no process can modify it at runtime. In that context, validate_timestamps=0 is free and risk-free — there can be no drift between the version of the file on disk and the cached version.

On traditional hosting, where code can be changed over FTP or through a theme editor, this setting is dangerous: a modified file won't be picked up, which can cause baffling bugs. It's one of the reasons why PaaS platforms that enforce immutability naturally deliver better PHP performance than classic shared hosting.

Preloading

Since PHP 7.4, OPcache supports preloading: a script run when PHP-FPM starts loads a set of files into memory that are then available to every request, without even needing an explicit require. For heavy frameworks (Symfony, Laravel), this can further reduce request startup time — at the cost of restarting PHP-FPM on every deployment to refresh the preload.

Practical applications

On WordPress, OPcache is often underused: the host enables it with conservative defaults and validate_timestamps=1. Switching to an optimized configuration on an immutable PaaS — with validate_timestamps=0, memory_consumption=128, max_accelerated_files=20000, interned_strings_buffer=16 — transforms the application's performance profile. I detail this configuration in my article on securing and optimizing WordPress with Infrastructure as Code.

Frequently Asked Questions

Why does OPcache speed up PHP?

PHP is an interpreted language: on every request, the interpreter has to read the source files, parse them, compile them into intermediate bytecode (opcodes), then execute that bytecode. OPcache short-circuits the first three steps: the first time a file is loaded, it stores the compiled bytecode in shared memory, and every subsequent request reuses that bytecode directly without rereading or reparsing the source file. On a complex application like WordPress, that removes a large share of the startup work of each request.

What does the opcache.validate_timestamps setting do?

By default (validate_timestamps=1), OPcache periodically checks whether PHP files have changed on disk. If a file has changed, the cache is invalidated and the file is recompiled. That behavior is useful in development (you want to see your changes immediately) but costly in production: every request runs a stat() on every loaded file. With validate_timestamps=0, OPcache stops doing this check — but you become responsible for invalidating the cache manually on every deployment (or restarting PHP-FPM, which most modern PaaS platforms do automatically).

OPcache and immutable-filesystem PaaS: why are they a perfect match?

On a PaaS like Upsun, Fly.io or Heroku, the application code is built into an immutable image — once deployed, the PHP files can no longer change. In that context, opcache.validate_timestamps=0 is the ideal setting: PHP has no reason to recheck files that can't be modified. On every new deployment, the runtime is rebuilt, OPcache is initialized with the new bytecode, and all disk checks disappear. It's a free performance gain handed to you by the architectural constraint of immutability.

Resources and Documentation