Skip to content
Advertisement

How to load balance a php application?

I am looking for guides, advice, or samples of how to load balance a php application. My setup is Ubuntu 10.04 and PHP 5.3. I have never load balanced servers before and I am looking for any help that is offered.

Update:
It’s a web application that is expected to have a few hundred users using it at the same time. MySQL will be the database. There will be sessions used for the users but I have heard that sessions cannot be carried over multiple servers. There will be very frequent content updates. There will be files but I’ll just use a CDN for those.

Advertisement

Answer

load balancing a web application has not much to do with the application itself but more with hosting and infrastructure. However there are still some key points you have to pay attention when building an app that is supposed to be load balanced. Here are some:

  • Do not use state server. Meaning you cannot use the traditional session object because it is stored in the memory of the local machine so an object maybe available in one server but not in the other
  • Do not use disk to store anything. Same reason as the above. You might write a file to server 1 disk and try to read in server 2
  • Bottom line you should avoid any kind of resources that are server dependent. If you need to use something like this (state server, disk, etc) you should store a shared resource so all load balance machines access it

If this is a problem for an existing application, for example, you can workaround this by setting affinity on the Load Balancer meaning that all requests that come from a specific user will be served by the same server. Obviously this approach has the downside of making your app less scalable since one server can get more processing than the other.

So, regarding the software to do the load balancer I’m not a Linux guy but I see lots of people talking about Apache as WebServer and HAProxy as the load balancer app.

Hope it helps!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement