Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
Modern PHP 8.x & Internals 8-10 Years (Tech Lead) Hard Production Scenario

How does OPcache Preloading work in PHP 8.x, and how do you configure it in containerized production environments?

Real-World Scenario / Incident Description

Deploying a Docker container with PHP-FPM where cold requests experienced 200ms latency on the first 100 requests after deployment.

693 views • Updated 1 month ago

OPcache Preloading Mechanics

When PHP-FPM starts up, a designated preload script compiles specified classes, traits, and interfaces directly into permanent shared memory before any HTTP request is accepted:

opcache.preload=/var/www/preload.php
opcache.preload_user=www-data

Considerations & Gotchas

  • Preloaded classes are permanently bound in memory.
  • If code changes on disk, you must restart PHP-FPM (systemctl restart php-fpm) for preloaded memory to reload.
  • Preload only stable framework code (e.g. vendor/ and core service classes), not dynamically changing code.
Related Concepts: #OPcache #Preloading #Docker #DevOps #Performance