how are concurrent requests handled in PHP (using - threads, thread pool or child processes)

prasun picture prasun · Nov 10, 2015 · Viewed 16.6k times · Source

I understand that PHP supports handling multiple concurrent connections and depending on server it can be configured as mentioned in this answer

How does server manages multiple connections does it forks a child process for each request or does it handle using threads or does it handles using a thread pool?

The linked answer says a process is forked and then the author in comment says threads or process, which makes it confusing, if requests are served using child-processes, threads or thread pool?

Answer

jankal picture jankal · Nov 28, 2015

As I know, every webserver has it's own kind of handling multpile simultanous request. Usually Apache2 schould fork a child process for each new request. But you can somehow configure this behaviour as mentioned in your linked StackOverflow answer.

Nginx for example gets every request in one thread (processes new connections asyncronously like Node.js does) or sometimes uses caching (as configured; Nginx could also be used as a load balancer or HTTP proxy). It's a thing of choosing the right webserver for your application.

Apache2 could be a very good webserver but you need more loadbalancing when you want to use it in production. But it also has good power when having multiply short lasting connections or even documents which don't change at all (or using caching).

Nginx is very good if you expect many long lasting connections with somehow long processing time. You don't need that much loadbalancing then.

I hope, I was able to help you out with this ;)

Sources:

https://httpd.apache.org/docs/2.4/mod/worker.html

https://anturis.com/blog/nginx-vs-apache/

I recommend you to also look at: What is thread safe or non-thread safe in PHP?