PHP-FPM – ondemand spawner mode

In the old, polish version of this blog, there are several posts about nginx server and PHP configuration. These entries are outdated now, but I will be writing them again in english soon. Meanwhile, today I would like to briefly share information on the ondemand mode in PHP-FPM. I have to used it and think it’s the best solution for most configurations.

I wrote about PHP in old entries and use dynamic PHP-FPM mode. In this mode, there is some processes waiting for requests – we set number of that processes, limit to create new and time to kill unused. It’s ok, but I think too complicated and also sometimes not optimal – there may be no requests, but all dynamic spawners will still consumes some memory. More according to starting dynamic processes. Solution? Use ondemand mode. Look at very, very simple configuration for that in PHP spawner configuration file:

pm = ondemand
pm.max_children = 10
pm.process_idle_timeout = 15s
pm.max_requests = 100

Yes, really it’s all. There is no start children, max servers etc, only simple configuration. We need PHP 5.3.8 or newer to use ondemand, but be serious – who uses older versions? PHP 5.3 or 5.4 are already outdated and should not be used anymore. Return to configuration. It’s simple, but I will also describe all lines. First does not require an explanation, it’s only mode type.

pm.max_children – our spawner will start with 0 active processes and will create new ondemand according to incoming requests. With this parameter we can simply set maximum number of processer per each spawner. Very nice, because we can limit “less popular” spawners and avoid memory leaks in strange situations.

pm.process_idle_timeout – similar to configuration for dynamic. After this time, inactive (idle) process will be killed and free our memory. What time should we use? It depends on our server, services or websites etc. 15 seconds for example it’s good base for start.

pm.max_requests – also similar to dynamic configuration. This setting will say PHP, after how many request should kill and respawn our spawner process. Why? Of course process will operate on any request separately, but this does not protect us from memory leaks from third party libraries using in apps and PHP. Processes should be spawning regularly.

And it’s all. Ondemand configuration is very simple and efficient, because we can save more memory – a lot of memory when we use many spawners. And we should use many spawners, because it allow us to separate websites, apps sections etc. (depends on configuration). Disadvantages, drawbacks?  I think only one: dynamic configuration with free process is ready to reply immediately for request. With ondemand, we must wait for process creation. But it’s it’s long time and our visitors can’t see that difference.