Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load balance a php application?

Tags:

linux

php

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.

like image 562
Chris Abrams Avatar asked Feb 23 '11 00:02

Chris Abrams


People also ask

What is load balancing in PHP?

Load balancing is where requests are distributed uniformly across servers in a server pool. Load balancers receive user requests and determine which server in a server pool to forward the request for final processing.

How do you load a webserver balance?

To load balance Web servers, traditionally you use the DNS round-robin feature to evenly distribute Web server IP addresses to clients; thus, your Web servers are equally accessible.

How do I load a balance in WordPress?

Connect to your WordPress instance via SSH to configure the managed database and IAM user credentials. Sign in to the dashboard of your WordPress website, and install the WP Offload Media plugin. Duplicate your WordPress instance. Create a load balancer in Lightsail, and attach your WordPress instances.


2 Answers

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!

like image 72
tucaz Avatar answered Nov 15 '22 17:11

tucaz


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.

like image 32
Sean Milheim Avatar answered Nov 15 '22 18:11

Sean Milheim