Sophomore Dev
The Dev You Say

Handling Multiple Requests Concurrently: A Guide to Improving PHP’s Built-in Server Performance

This is actually the post I originally wanted to make. The reason was simple. I didn’t find a lot of info about the topic, and this is what the official PHP docs have to say:

You can configure the built-in webserver to fork multiple workers in order to test code that requires multiple concurrent requests to the built-in webserver. Set the PHP_CLI_SERVER_WORKERS environment variable to the number of desired workers before starting the server. This is not supported on Windows.

If you have ever used PHP’s built-in server for more than, let’s say testing your site’s code locally you have probably run into concurrency issues. Servers are responsible for handling a vast amount of requests concurrently. In a single-process synchronous mode, requests are dealt with individually and can block the server from executing until they complete. This can lead to delays in processing and reduce overall server performance.

To tackle this issue, PHP 7.4 introduced support for handling multiple requests concurrently. This feature relies on fork() availability and doesn’t work on Windows. The server forks new workers to serve each incoming request in this mode.

Enabling Concurrent Request Handling

You can activate concurrent request handling by setting the PHP_CLI_SERVER_WORKERS environment variable to the number of workers you want:

PHP_CLI_SERVER_WORKERS=8 php -S localhost:8080

Of you can do the same in code. For example using a wrapper script to enabled the number of workers, like so:

putenv("PHP_CLI_SERVER_WORKERS=8");

This command spawns eight concurrent worker processes capable of handling simultaneous requests.

Things to Keep in Mind When Using Multiple Workers

While using multiple workers can significantly improve server performance, there are some things you need to keep in mind when using them:

  1. Memory Usage – Each worker process requires an independent copy of memory, which means that enabling too many workers could lead to high memory usage and slow down your system’s performance.
  2. Database Connection Limitations – If you’re using multiple database connections, make sure that your connection pool supports concurrent access without encountering deadlocks or other synchronization issues.
  3. Shared Resources Management – When working with shared resources such as files or disk space, ensure that multiple workers do not interfere with each other while accessing these resources at the same time.

Concurrent request handling is an excellent way of improving server performance by allowing the server to handle multiple requests simultaneously. However, it’s important to keep some things in mind while using multiple workers, such as memory usage, database connection limitations, and shared resource management.

Handling concurrent requests is still marked as experimental in PHP 8.1; however, it promises better stability and performance enhancements in the future.

Comments are closed.