Simplicity in Load Balancing
By Randal L. Schwartz
I was thinking about e-commerce the other day, and chatting with my buddy Steven Lembark of Knights bridge Solutions about the typical tiny, dirty tasks related to it. He mentioned that he hadn't seen any of my columns address the task of load balancing incoming requests to Web servers, and I said, "Hey, thanks for the idea."
After chatting with Lembark for a bit, I came up with a very simple way to ensure that incoming requests are sent to the best host for processing. It's not the most elegant approach, but it's probably the fewest lines of code you'll see on this topic, so I call it the "simple load balancer." Basically, we run a daemon that gathers the load averages for the interchangeable machines in the cluster. (They're interchangeable in that they should have identical content and return the same response for the same URL.)
This daemon then writes a small database visible to a CGI script that can take an incoming URL and redirect the invoker to the same URL on the least-loaded machine, randomly redirecting the requests in proportion to the current load average. So, from the welcome page of the application, all the outgoing links are sent through this redirect script, and boomthe user ends up on the best machine at the moment. Not rocket science, but it'll do. So, let's take a look at how this works.
The System Load Average
The first script reports back the current load average on the system, and is presented in
|