Chris Abrams
Chris Abrams

Reputation: 42420

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.

Upvotes: 11

Views: 25822

Answers (2)

Sean Milheim
Sean Milheim

Reputation: 47

There are PHP classes that allow you to store the session in MySQL.

http://php.net/manual/en/function.session-set-save-handler.php

I would put something like nginx on a front end server and use that for your load balancing. Behind that you could have your two web servers and even use nginx to serve your static content.

Behind your webservers you would have your mysql db.

I've designed a few networks that have received just over 1,000/imp /sec and started the network design with this. After the traffic increased I usually just scaled out the infrastructure from there adding more webservers and more read-only db's and swapping out nginx with a pair of F5's.

Upvotes: 1

tucaz
tucaz

Reputation: 6684

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!

Upvotes: 17

Related Questions