Skip to content

Lesson 3 of 9 · 21m

OPcache in Production: The Free Performance You Can Still Misconfigure

How opcode caching works, which settings matter, how deploy invalidation really behaves, and the metrics that prove whether it is healthy.

OPcache is the closest thing PHP has to a free lunch. It stores compiled opcode arrays in shared memory so every request does not re-parse and re-compile the same PHP files. In production this is not optional. The interesting part is not whether to enable it — it is how deploys, memory pressure, and file timestamp validation interact with it.

The minimum production posture

zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=256
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=40000
opcache.validate_timestamps=0
opcache.save_comments=1

memory_consumption is shared across the FPM pool, not per worker. max_accelerated_files must exceed the number of PHP files in the deployed application, including vendor packages. Count them before copying a value from a blog post:

find /srv/app -type f -name '*.php' | wc -l

Leave save_comments=1 unless you are certain no library reads docblocks for annotations or metadata. Saving a few kilobytes is not worth discovering a runtime dependency during an incident.

Timestamps are a deployment decision

With validate_timestamps=1, OPcache checks file modification times and automatically notices deploys. It is convenient, but every check costs syscalls and cache bookkeeping. With validate_timestamps=0, PHP trusts whatever is in shared memory until it is told otherwise. That is the usual high-traffic production choice — but then deployment must reset OPcache atomically.

php -r 'opcache_reset();'      # wrong for FPM: this is CLI, not the web pool
systemctl reload php8.3-fpm   # common: graceful worker restart

A graceful FPM reload clears shared opcode cache and starts new workers. Better immutable deploys point a symlink at a new release directory, reload FPM, and keep the previous release for rollback. Do not call opcache_reset() from a public endpoint; it becomes a denial-of-service button.

Measure it, do not assume it

$status = opcache_get_status(false);
printf("%.1f%% hit rate\n", $status['opcache_statistics']['opcache_hit_rate']);

Export hit rate, used memory, cached keys, and restarts into metrics. Healthy systems routinely sit above 99% hits after warmup. Falling hit rates mean files are being invalidated, the cache is full, or workers are restarting.

JIT is useful only where CPU is the bottleneck

PHP 8's tracing JIT compiles hot opcode paths into machine code. It can help numeric loops, parsers, compression, and CPU-bound workers; it rarely moves CRUD applications because their time is object work plus I/O.

opcache.jit=tracing
opcache.jit_buffer_size=64M

Benchmark production-shaped endpoints with JIT disabled and enabled, comparing CPU time, wall time, and percentiles. JIT buffer memory is unavailable for opcode storage, and extension internals written in C do not become faster because PHP-level JIT exists.

Field notes

  • OPcache memory exhaustion degrades hit rate before it produces a loud error.
  • Composer vendor trees count against max_accelerated_files; modern dependency-heavy apps blow past small defaults easily.
  • Preloading can help large frameworks, but it makes deploy memory behavior more complicated and can defeat container copy-on-write assumptions.
  • CLI PHP and FPM PHP have separate OPcache lifecycles. Clearing one does not clear the other.

Was this lesson helpful?

Sign in to rate