Search code examples
phpfpm

PHP-FPM long polling configuration


Let's say that I have the following configuration for php-fpm:

pm = dynamic
pm.start_servers = 10
pm.max_children = 400
pm.min_spare_servers = 8
pm.max_spare_servers = 16
pm.process_idle_timeout = 10s

Let's also say that every user must have an endless long polling request. If I have a limit of 10000 multiple requests and I have 10000 users connected to my website, does it means that my server will hang forever?

Another question: how servers and children are related to simultaneous requests? Does every request spawn a new process? What is the difference between servers and children? From my understanding, children are the processes.

PS: Please don't suggest Websockets or any other technologies.

Thanks.


Solution

  • When you use PHP-FPM with dynamic processes (which is the default and recommended value), you have those options:

    ; Choose how the process manager will control the number of child processes.
    ; Possible Values:
    ;   static  - a fixed number (pm.max_children) of child processes;
    ;   dynamic - the number of child processes are set dynamically based on the
    ;             following directives:
    ;             pm.max_children      - the maximum number of children that can
    ;                                    be alive at the same time.
    ;             pm.start_servers     - the number of children created on startup.
    ;             pm.min_spare_servers - the minimum number of children in 'idle'
    ;                                    state (waiting to process). If the number
    ;                                    of 'idle' processes is less than this
    ;                                    number then some children will be created.
    ;             pm.max_spare_servers - the maximum number of children in 'idle'
    ;                                    state (waiting to process). If the number
    ;                                    of 'idle' processes is greater than this
    ;                                    number then some children will be killed.
    ; Note: This value is mandatory.
    

    Responding your question, server's and children's are processes, which will respond to one user per process. Your server will support maximum of 400 concurrent connections.

    I've implemented a lot of systems with longpolling, which is more efficient and use less resource than refreshing at some interval.

    The big problem is that your PHP process will consume all your server memory, and I strongly recommend you to look for any alternative.

    One good alternative I use is the NGiNX HTTP Server Push module, and you can create scripts that will push information to your clients. Using this method you can scale to thousand concurrent clients, which is impossible to be done directly with PHP.

    For an example, I made an email client that has a daemon wrote in PHP that monitor the user mail box and push notification of new emails using this NGiNX module, and the file system monitoring are done using PHP inotify, in other words, I use almost 0 system resource and create a webmail system with realtime notification and very low network and processor usage, which are computational expensive using only PHP and longpolling or using refresh.

    I know you don't want Websockets suggestion, but depending your demand, it's almost unable to avoid it.