A Java developer joining a PHP team often asks where the application “lives”. In a Spring Boot service, the answer is obvious: one long-running JVM process that starts once, loads its classes, builds its objects and then handles requests for days. In classic PHP, there is no such place. Each request runs a script from the top, builds whatever it needs, sends a response and throws everything away. Understanding this “shared-nothing” model explains a lot about PHP: why a memory leak rarely takes a site down, why caching compiled code matters so much, and why newer runtimes that keep PHP alive between requests come with warnings.
Following one request
Take a Laravel or WordPress site behind Nginx. A browser asks for a product page.
- Nginx receives the request. Static files such as images it serves directly. For anything that needs PHP, it forwards the request over the FastCGI protocol to PHP-FPM.
- PHP-FPM, the FastCGI Process Manager, keeps a pool of worker processes. A free worker takes the request.
- The worker runs the entry script. The framework loads its configuration, registers routes, builds the service container, connects to the database, runs the controller and renders the response.
- The response goes back to Nginx and on to the browser.
- The worker frees everything that request created: objects, variables, static properties. The process itself stays alive, waiting for the next request, but no application state carries over.
Each worker handles exactly one request at a time. Concurrency comes from having many workers, not from threads or an event loop within one worker.
What the clean slate buys you
Throwing away state after each request sounds wasteful, and in some ways it is. But it brings real advantages. A bug that leaks memory or corrupts a global variable affects one request and then disappears. A fatal error in one request does not crash the server for everyone else. Deploying new code is simple: the next request reads the new files. Data cannot accidentally leak from one user’s request to another’s through a forgotten global, which is a genuine risk in long-running servers.
The cost is repeated work. Every request pays to bootstrap the framework, and without help it would also pay to read and compile every PHP file it uses. That is where OPcache comes in.
OPcache: compiling once instead of every time
PHP source code is not executed directly. The engine first parses it and compiles it into opcodes, a lower-level instruction format that the Zend engine runs. Without caching, this happens for every file on every request. A framework request that touches several hundred files would spend a large share of its time just compiling.
OPcache, bundled with PHP since version 5.5, stores compiled opcodes in shared memory accessible to all PHP-FPM workers. The first request to use a file compiles it; later requests reuse the compiled form. On a typical framework application, enabling OPcache is the single largest performance gain available, which is why it should always be on in production.
A few settings matter in practice. opcache.memory_consumption needs to be large enough to hold all your compiled code, or entries get evicted. opcache.validate_timestamps controls whether PHP checks files for changes; turning it off in production removes those checks entirely, but then a deploy must reset the cache, usually by reloading PHP-FPM, or the old code keeps running. PHP 7.4 added preloading, which loads chosen files into memory when the server starts, and PHP 8.0 added a JIT compiler, which helps CPU-heavy code more than typical database-bound web requests.
Sizing a PHP-FPM pool
Because each worker handles one request at a time, the pool size caps concurrency. PHP-FPM offers three process manager modes: static keeps a fixed number of workers, dynamic varies between configured minimum and maximum spare workers, and ondemand starts workers only when requests arrive. The key limit in all of them is pm.max_children.
The usual sizing approach is memory-based. Suppose a server has about 4 GB available for PHP after the operating system and other services, and each worker peaks around 60 MB. Then roughly 4,096 ÷ 60, about 68 workers, fit before the server starts swapping. Setting the limit higher does not add capacity; it adds swapping, which is far worse than queuing. If all workers are busy, new requests wait in the FastCGI listen queue, and when that fills, Nginx returns 502 or 504 errors. A slow database query therefore shows up as worker exhaustion, the same way slow dependencies exhaust thread pools in Java.
How it compares with Node.js and Java
| Aspect | Classic PHP-FPM | Node.js | Java (e.g. Spring Boot on Tomcat) |
|---|---|---|---|
| Process lifetime | Workers persist, application state does not | One long-running process per instance | One long-running JVM |
| Concurrency | Many processes, one request each | Single-threaded event loop with async I/O | Thread pool, or virtual threads in newer versions |
| In-memory state between requests | None by default; use Redis, APCu or the database | Yes, any module-level data | Yes, singletons and caches |
| Effect of a memory leak | Cleared after each request | Accumulates until restart | Accumulates until restart |
| Startup cost per request | Framework bootstraps every time | None after startup | None after startup |
PHP-FPM also has a setting, pm.max_requests, that recycles a worker after a set number of requests. It exists to contain leaks in extensions, and it reflects the same philosophy: start clean regularly.
When PHP stays alive: Swoole, RoadRunner, FrankenPHP and Octane
To remove the bootstrap cost, several projects run PHP as a long-lived application server. Swoole and OpenSwoole are extensions that provide an event-driven runtime. RoadRunner, written in Go, and FrankenPHP, built on the Caddy web server, keep PHP workers running with the framework already booted. Laravel Octane supports several of these.
The speed gains can be large, but they bring back the long-running server’s problems. A static property set during one request remains for the next. A singleton holding the current user leaks across requests. Memory that used to be freed automatically now accumulates. Code written for the shared-nothing model, which may have been perfectly correct, has to be reviewed with these risks in mind. The trade-off is the same one every server design faces: reuse state for speed, or discard it for safety.
Written by the RP Strativa Team. Memory and pool-size figures in the examples are illustrative.
Leave a Reply