Every include, require, and file_exists() call in PHP resolves a path through realpath(), which issues a stat() syscall to verify the file exists and normalize symlinks. On a Symfony application with 8 000 autoloaded classes or a WordPress site with 200 plugins, that translates to thousands of stat() calls per request without an adequately sized realpath cache. This article covers what the cache stores, how to measure current saturation, and the specific values that fit modern frameworks.
How to verify
# current realpath cache settings
php -i | grep -E 'realpath_cache_(size|ttl)'
# runtime realpath cache usage
php -r 'print_r(realpath_cache_size()); echo "\n"; var_export(realpath_cache_get());' \
| head -20
# count PHP files loaded in a single request (strace method)
strace -f -e trace=stat,lstat,newfstatat \
-p $(pgrep -n php-fpm) 2>&1 | grep '\.php"' | wc -l
# measure stat syscall rate on an FPM worker
perf stat -p $(pgrep -n php-fpm) -e syscalls:sys_enter_newstat sleep 5
# check if realpath cache is full (returned entries == realpath_cache_size)
php -r '$max = ini_get("realpath_cache_size"); echo $max . " max, " . realpath_cache_size() . " used\n";'
If realpath_cache_size() equals realpath_cache_size ini value (in bytes), the cache is full and PHP is performing un-cached stat() calls on every overflow path — this is the performance hole you are fixing.
What’s happening
realpath() converts a path string to an absolute, symlink-resolved, canonical path. PHP calls it internally for every include, require_once, class autoload, and filesystem function. The result is stored in a per-process LRU cache keyed on the input path string, with a TTL after which the cached path is re-verified against the filesystem.
The cache is per FPM worker process, not shared memory. Each of your 30 FPM workers maintains its own realpath cache. A cache hit is a hashtable lookup in process memory — essentially free. A cache miss triggers one or more lstat() or stat() syscalls per path component, plus a readlink() if a symlink is encountered. On a path with 6 components, a cold lookup can issue 12 syscalls.
The default realpath_cache_size is 4 MB, which stores roughly 200–400 paths depending on path length. Symfony’s vendor directory alone can load 2 000+ unique file paths per request. The cache fills in the first few requests and then thrashes, costing syscalls proportional to the number of paths that do not fit.
realpath_cache_ttl controls how long a resolved path is trusted before re-stat()-ing. The default is 120 seconds. In a production environment with validate_timestamps=0 and atomic deploys, the TTL interacts with the deploy window: if a deploy completes in under TTL seconds, old paths may be served from the cache. The right response is to restart FPM after deploy (which clears per-worker caches), not to shorten TTL.
The procedure
-
Count the unique PHP files loaded by the application in a single request:
# via strace on a live worker strace -f -e trace=stat,openat -p $(pgrep -f 'php-fpm: worker') 2>&1 \ | grep -oP '"\K[^"]+\.php' | sort -u | wc -lMultiply that count by the average path length in bytes (typically 60–100 bytes), add 128 bytes of overhead per entry, and that is your minimum
realpath_cache_size. Round up to the next power of 2. -
For a Symfony/Laravel application, a calibrated starting point:
; /etc/php/8.3/fpm/conf.d/10-performance.ini realpath_cache_size=4096K ; 4 MiB — fits ~2000 paths of average length realpath_cache_ttl=600 ; 10 minutes — longer than any deploy windowA site with 5 000+ unique paths (large monorepo, many plugins) may need 8 MB or 16 MB:
realpath_cache_size=16384K -
Reload FPM and confirm the cache is not saturating:
sudo systemctl reload php8.3-fpm # send a warm-up request curl -s https://app.example.com/ > /dev/null # check usage — should be well under the limit php -r 'echo realpath_cache_size() . " bytes used\n";' -
For deployments using Capistrano-style symlink swaps, set
realpath_cache_ttl=0OR restart FPM after every deploy. Do not use a long TTL with symlink deploys — the cache will serve paths pointing at the previous release directory until the TTL expires:# post-deploy: restart FPM to clear per-worker realpath caches sudo systemctl restart php8.3-fpm -
On PHP 7.4+ with OPcache preload, the preload script runs before workers fork and populates the realpath cache for preloaded files. Those entries persist in the parent process address space and are inherited by workers (copy-on-write), effectively pre-warming the cache for free.
Common pitfalls
-
realpath_cache_size is parsed as a byte count, not a shorthand.
4Mis interpreted as 4 bytes (no suffix expansion) in some PHP versions. Use4096Kor4194304to be unambiguous. Test withphp -i | grep realpath_cache_sizeto confirm the parsed value. -
Symlink swaps invalidate cached paths silently. If your deploy changes which directory
/var/www/apppoints to, workers still hold cached realpath entries for files in the old directory. Those entries will resolve correctly — to the old files — until they expire or FPM restarts. This is the most common cause of “new code not visible” bugs when TTL is long and deploys use symlink rotation. -
The cache is per-process, not shared. A realpath cache hit on worker #1 does not benefit worker #2. Each worker independently populates its own cache over the first few requests it handles. High worker churn (FPM max_requests set very low) means every new worker pays the cold-cache cost on its first requests.
-
CLI scripts have their own realpath cache.
php artisanand queue workers have entirely separate realpath caches from FPM. Tuningrealpath_cache_sizein the FPM pool ini does not affect CLI scripts. Set CLI values in/etc/php/8.3/cli/php.ini. -
Very large TTL +
validate_timestamps=0= invisible deploys. Ifrealpath_cache_ttl=3600andvalidate_timestamps=0, a deploy that does not restart FPM will serve cached bytecode of old files for up to one hour. Always include an FPM restart oropcache_reset()call in the deploy pipeline.
The realpath cache is one of several filesystem-adjacent performance levers that compound with OPcache and FPM pool tuning. Stack Harbor’s managed operations practice configures all three in concert during initial provisioning, with monitoring on stat() syscall rate to catch regressions.