PHP OPcache Preload and validate_timestamps on a VPS: What Actually Saves CPU

Why validate_timestamps Quietly Burns CPU

Every PHP request with OPcache enabled runs a stat check on every included file when opcache.validate_timestamps=1, the default. On a WordPress site with 200 to 400 included files, that is hundreds of stat() calls per request. On local SSD it is cheap; on a VPS with a busy overlay filesystem or a network-backed volume it is not, and it scales linearly with traffic.

Measure first with php -i | grep -E "validate_timestamps|revalidate_freq". On a typical 2 vCPU box serving 40 requests per second with 300 files each, that is 12,000 stats per second that produce identical answers between deploys. The fix is not “turn it off blindly” — it is to understand what you are trading when you do.

What Preloading Actually Buys You

opcache.preload points at a file that is executed once at FPM worker startup, and every class and function it loads is compiled and held in shared memory for the life of the pool. With opcache.preload_user=www-data and a preload script that requires your framework bootstrap and hot classes, a cold worker start stops paying the compile cost for the core of the application.

The gain is real but bounded: preloading removes compile time from worker startup and improves the first-request latency after a reload. It does not speed up steady-state execution, because OPcache already serves compiled bytecode from shared memory. That is why a virtualserversvps.com box with a fixed vCPU count makes the measurement trustworthy — run with ab -n 200 -c 5 against a cold systemctl reload php8.3-fpm before and after, and expect single-digit millisecond improvements per request, not 10x.

Write a Preload Script That Does Not Recurse

A workable pattern is a small file that sets a fixed class list and loops over it. Put the list in /etc/php/8.3/fpm/preload.php and reference it with opcache.preload=/etc/php/8.3/fpm/preload.php. Use class_exists($class, true) inside the loop and skip anything that fails to load, because one missing class aborts the whole preload at startup and the pool fails to come up.

The common failure is preloading a class that depends on request-scoped state or on an extension loaded later. Keep the list to framework core, service container classes and frequently used value objects. If you see PHP Fatal error: Cannot declare class in the FPM log, you have a duplicate include or an autoloader conflict, not a memory limit problem.

Decide Whether Timestamps Can Be Disabled

Setting opcache.validate_timestamps=0 removes all per-request stat calls. It is safe only when deploys are explicit and followed by an FPM reload, which is how a controlled release pipeline works. If the site edits theme files through wp-admin or an FTP client, disabling it will leave users on stale code for hours, and that is a worse failure than the CPU cost.

The middle path is validate_timestamps=1 with revalidate_freq=60. That caps the check at once per minute per file rather than once per request, which cuts the stat volume by the request-rate factor while keeping edits visible within a minute. For a 2 vCPU VPS this is usually the right setting: predictable correctness with a small fraction of the overhead.

Size the Shared Memory Segment Honestly

Check opcache_get_status()['memory_usage'] under load, or run php -r 'print_r(opcache_get_status(false)["memory_usage"]);' from CLI with the same ini. If used_memory sits above 70 percent of opcache.memory_consumption, you are evicting scripts and recompiling them on the next request, which silently negates every other tuning change on this page.

Raise it in steps of 32M up to 256 or 384, watching opcache_statistics.num_cached_scripts and the oom_restarts counter. A non-zero oom_restarts with a climbing hash_restarts is the signature of an undersized segment. Each cached script costs a few kilobytes of shared memory plus the compiled bytecode, so 128M is often enough for a mid-size WordPress and far too little for a large Laravel application.

Check Hash Table and JIT Settings Together

opcache.max_accelerated_files must exceed the number of PHP files in the application. The default 7,963 is fine for small projects; a large framework with vendors can exceed 20,000. Set it to 16,257 or 32,509 — these are primes the engine rounds to — and monitor hash_restarts in the statistics; any increase at steady state means the table is too small.

JIT is a separate decision. opcache.jit=tracing with opcache.jit_buffer_size=64M helps CPU-bound code and often hurts database-bound WordPress, where it also reserves memory that the buffer pool wants. Test with ab rather than assuming — and remember that a 64M JIT buffer on a 1 GB VPS is a real subtraction from your PHP-FPM headroom.

Put It Together and Verify With Numbers

A defensible low-memory configuration is opcache.memory_consumption=192, opcache.max_accelerated_files=16257, opcache.validate_timestamps=1, opcache.revalidate_freq=60, opcache.preload pointing at a curated class list, and JIT disabled unless you have measured a win. That costs about 200 MB of shared memory on a 2 GB VPS and removes the compile path from every request.

Confirm with php -i | grep -i opcache, a cold wrk -c25 -d30s --latency against a dynamic page, and the post-reload first-request time. If the numbers move by less than 5 percent, keep the change anyway for the stat reduction — but do not claim a speed improvement you cannot show. When memory is the actual constraint, more RAM on a virtualserversvps.com plan solves it faster than shaving the OPcache segment, since a small segment means recompiles, not savings.

Leave a Reply