How to scale php application servers horizontally?
In a few words, the solution, generally speaking, is as follows:
- There are multiple PHP servers
- In front of another load balancer server
- If you have 3 PHP servers, then this load balancer will send 33% of the requests it receives on each PHP server.
A load balancer server can be specialized hardware; or a reverse proxy using Apache (see mod_proxy_balancer
for example), nginx (see Load balancing with Nginx , for example varnish, ...
The most important problems you will have to face when using multiple PHP servers instead are related to the file system: with multiple servers, each server has its own disks and file system.
For example, if a user is accidentally balanced on server 1 for one page and server 2 for another page, you cannot use file-based sessions: the session will be created on server 1, but not found on server 2 later.
In this particular case, you will need to use a different mecanism to store sessions - for example, store them in a database or memcached.
Same with images (e.g. uploaded by users): you need to either:
- Synchronize them between servers
- Or use some kind of network drive
Edit after comments: . For a multi-server deployment, I usually do the same thing as a single server.
For more information on which process I frequently use, you can take a look at the answer I gave for a while to this question: Updating a web application without any downtime .
The explanation given is for a single server, but to do the same on multiple servers, you can:
- Upload the archive to each of your servers.
- Extract it
- And almost at the same time, switch the symbolic link on all servers
- In the worst case, you will have 2 or 3 seconds of latency between servers, which means 10 seconds if you have 5 servers? This is probably not a big problem :-)
(I did this with 7 servers for one application and never experienced any problems)
a source to share