PHP processes

Published

Updated

🔁

How many requests can your app keep busy?

Each PHP process answers one request at a time. The number of processes in the PHP plan and the PHP response time together decide how many visitors the app serves at once.

PHP processes

fortrabbit runs PHP with PHP-FPM, the FastCGI Process Manager. Its processes stay alive between requests. Each one handles one PHP request at a time. The PHP plan sets the number of processes and the memory they share.

PHP memory requirements

How much memory your website will require depends on multiple factors. Let's start with the theory:

  • Extensions: Each enabled PHP extension must be loaded into memory.
  • PHP libraries: You most likely are using Composer to manage your dependencies. This implies a set of PHP files which need to be interpreted and loaded into memory - specifically into the OPcache.
  • PHP code: The code you write yourself consumes memory in the OPcache as well. The more code you have, i.e. the more functions and complexity, the more memory is required.
  • Runtime variables: In each request your App fills variables ($foo = "bar"), which consumes memory. How much it will use depends on the amount of data used in variables. Measure it with memory_get_peak_usage().

The memory used by extensions and OPcache is shared. This means: If you run an app on a single process and use 10 MB OPcache, then you will also only use 10 MB of OPcache with two processes. The memory consumed by runtime variables is not shared. It's hard to pre-calculate the expected memory usage.

The tricky part is the data size. Say you have a blog with comments using a database. Now rendering a small blog entry with no comments will utilize less memory than rendering a large blog entry with thousands of comments. That depends on the code. It could load all of the thousands of comments into a PHP array and then render the site or load them in batches and print them right away, with very different memory use. Expect memory use to grow with the data. See out of memory.

PHP processes, PHP requests, PHP response time

  • A PHP request should finish quickly. The metrics show the PHP response time against lines that double from 250 ms.
  • A new request waits when all PHP processes are busy.
  • The PHP response time is most of the time to first byte (TTFB); network latency adds the rest.
  • The average PHP response time moves with every slow request, the 99ile shows how slow the slowest one in 100 was, see average, median and 99ile.

A simple example

  • The environment has 2 PHP processes
  • 1 PHP request takes 500 ms (PHP response time)
  • Each process handles 2 requests per second, so the environment handles 4 per second

A bad example

  • The environment has 4 PHP processes
  • 1 PHP request takes 1,000 ms
  • 1 page view creates 3 PHP requests
  • 3 visitors at the same moment create 9 requests: 4 run, 5 wait. With a steady stream of visitors the queue grows until requests time out

PHP processes VS page views

Page views refer to a single request made by a user to view a page on a website. This request will likely involve a number of HTTP requests, such as images, HTML, CSS, and JavaScript files, all of which must be downloaded from the web server.

For many websites, one page view is one PHP request. Often it is more: AJAX calls, API requests or image transforms each take a PHP process.

PHP processes VS max execution time

A higher max_execution_time lets long tasks finish, and lets them block a PHP process for longer, see the examples above. Keep it low in the PHP settings and move long tasks to jobs. A request to an external API that has not answered in 10 seconds rarely answers at all.

Written by humans and AI in collaboration.

AI use & editorial processEdit on GitHub ↗