Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I address c10k problem if I am using PHP?

Tags:

php

nginx

apache

I am deciding on the architecture of an application where "Http KeepAlive" and "long polling" will be used for faster response. Is there anything for PHP which solves the problem that Tornado does for c10k?

I was thinking of using nginx + PHP-FPM. But then, for 1000 active connections, won't there be 1000 PHP-FPM processes?

Then I think we'll have the same problem that Apache has with many standing connections. Isn't it?

EDIT: I understand that nginx will be enough if I just want HTTP KeepAlive. But what if I also want long polling like tornado supports? Is there anything similar in PHP?

like image 744
Sabya Avatar asked May 27 '11 11:05

Sabya


2 Answers

For active connections (as in, loading and running a defined PHP script), yes, there will be as many PHP processes as active connections. But KeepAlive is about passive connections, and Nginx is very good at handling passive KeepAlive connections with very low resource usage - even for thousands of them.

The problem with Apache is that it, in the usual configuration with mod_php and mpm_prefork, needs a process for each connection even if it's just a passive KeepAlive. This means that most Apache servers in fact do need to have a PHP process in memory even if the connection is passive, but this is not the case if you run PHP as FastCGI. Apache can also handle lots of passive connections if you run PHP as FastCGI and choose the mpm_worker which will create a more lightweight thread per connection, but it's still not as good as Nginx.

like image 126
Emil Vikström Avatar answered Oct 15 '22 20:10

Emil Vikström


My answer to this would be to take a look at node.js, You specify a 100K Active connections which Node.js should handle fine as long as you have the correct hardware.

Node does not spawn new processes when a connection is made, the socket is placed in a queuing system but it's not a typical queuing system, where 1 connection is processed and then the next one.

Node.js is notorious at handling many many connection's and there HTTP Client Library is based on Keep-Alive style of transferring, as well as being very fast and powerful.

I've used it a fair bit and I must say it's extremely easy to use, it's based on the Super fast Google V8 Engine that's used for the JavaScript in Chrome, meaning it's extremely fast, you should really take a look at this and you will see that it's viable for this sort of thing.

  • Blog post about Plurk using Node.js
  • Plurk handling 100.000+ open Connections
like image 24
RobertPitt Avatar answered Oct 15 '22 20:10

RobertPitt