Memory consumption
Each request consumes resources, including RAM. Orders of magnitude:
- apache prefork : ~2MB - ~15MB (mod_php with some libraries)
- apache worker : ~300KB
- nginx : ~30KB
An apache with mod_php can therefore hardly accept more than 1000 permanent connections.
What can overload your web server
- KeepAlive HTTP
- Big files / slow connections
- Permanent connections (long polling, web sockets) for a chat, push...
- Slow requests (lack of CPU/IO or slow web-service/BDD/LDAP)
- System limits (max open files, TCP TimeWait/CloseWait)
KeepAlive HTTP
During KeepAlive, connections use a request. If you have a lot of clients, the recommended KeepAlive of 60s will block a prefork apache.
Simulation
Solutions:
- strongly reduce KeepAlive (1 second), or disable it
- use apache worker/event (in proxy mode) : the default KeepAlive is 5s
- use nginx : the low resource consumption allows a higher KeepAlive (its default value is 75s)
Slow clients
Slow connections use resources during download.
Simulation
Solutions :
- use FPM instead of apache mod_php. For files served by PHP for authorization reasons, you can use X-Sendfile or X-Accel-Redirect
- use a reverse proxy to buffer (nginx has proxy buffering by default) or cache files (difficult if many big files, impossible if authorized by a cookie)
Permanent connections (Tchat, Push)
It's hard to have a lot of permanent connections with PHP.
Slow queries
Queries can be slow or slow only in case of overload: lack of CPU/IO or slow web-service/BDD/LDAP.
Solutions
- specific optimizations
- check the opcode cache for PHP
- add load-balanced servers
System limits (max open files, TCP TimeWait/CloseWait)
Solutions
Manage the overloading of your web server
maxConnections/MaxRequestWorkers
In case of overload, apache / haproxy / tomcat / FPM / uwsgi use the
backlog to keep pending connections.
nginx / nodejs do not use the backlog.
If
worker_connections is exceeded, nginx:
- warns "worker_connections are not enough" in error.log
- returns an empty response to the client
- no entry in access log !!
So make sure you have a high
worker_connections!
Simulation
Avoid saturating the backend
Using apache
max option, you can limit the requests to the backend and keep the others waiting.
The equivalent in nginx (
queue) is not available in Open Source nginx.
Nginx
limit_conn returns an error to the client.
Simulation with or without queue
Various
List of simulations