Console Login
Home / Blog / Server Administration / Scaling the LAMP Stack: High Availability Load Balancing with HAProxy 1.4
Server Administration 8 views

Scaling the LAMP Stack: High Availability Load Balancing with HAProxy 1.4

@

Stop Letting Traffic Spikes Kill Your Uptime

It’s 3:00 AM on a Saturday. Your monitoring SMS just went off. Your primary Apache server has hit MaxClients, the swap file is thrashing, and your latency just spiked from 40ms to 4 seconds. You are officially a victim of your own success.

If you are running a growing e-commerce site or a media portal in Norway, relying on a single server—no matter how much RAM you shove into it—is a ticking time bomb. Vertical scaling hits a wall. You need horizontal scaling.

In the past, the solution was buying a $20,000 F5 Big-IP hardware appliance or a Cisco LocalDirector. But it's 2010. We don't need proprietary metal boxes anymore. We have HAProxy.

Why HAProxy 1.4?

HAProxy (High Availability Proxy) has quietly become the de-facto standard for software load balancing. Version 1.4, released earlier this year, finally brought native client-side HTTP keep-alive support, which changes the game for performance. It consumes very little CPU and can handle thousands of concurrent connections without breaking a sweat—solving the famous C10k problem on commodity hardware.

The Architecture

Instead of pointing your DNS A-record to your web server, you point it to a lightweight CoolVDS VPS running HAProxy. This node distributes traffic to two or more backend web servers (Apache or Nginx).

  • Frontend: 1x CoolVDS VPS (HAProxy)
  • Backend: 2x CoolVDS VPS (Web Servers + PHP)
  • Database: 1x Dedicated Server (MySQL Master)

Configuration: The Nuts and Bolts

Let's get dirty. Assuming you are running CentOS 5.5, you can grab HAProxy from the EPEL repository or compile from source (recommended for the latest 1.4 features).

Here is a battle-tested /etc/haproxy/haproxy.cfg that I use for high-traffic Norwegian media sites:

global log 127.0.0.1 local0 maxconn 4096 user haproxy group haproxy daemon defaults mode http log global option httplog option dontlognull retries 3 option redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen webfarm 0.0.0.0:80 mode http stats enable stats uri /haproxy?stats balance roundrobin option httpclose option forwardfor server web01 192.168.1.10:80 check inter 2000 rise 2 fall 5 server web02 192.168.1.11:80 check inter 2000 rise 2 fall 5

Breakdown of the flags:

  • balance roundrobin: Distributes requests sequentially. Simple, effective.
  • option forwardfor: Crucial. It adds the X-Forwarded-For header so your Apache logs see the real visitor IP, not the load balancer's IP.
  • check inter 2000: Checks the health of your backend servers every 2 seconds. If a server dies (kernel panic, power outage), HAProxy removes it from the pool instantly. Zero downtime for users.
Pro Tip: Enable the stats page (stats uri). It gives you a real-time table of sessions and health status. It is ugly, but it tells you the truth.

Why Infrastructure Matters

Software is only half the battle. You can have the best config in the world, but if the underlying virtualization is oversold, your load balancer will choke on I/O wait.

This is where CoolVDS is different. We don't use OpenVZ containerization where "noisy neighbors" can steal your CPU cycles. We use Xen virtualization. This ensures hard RAM allocation and genuine kernel isolation. When your load balancer needs to process 5,000 packets per second, that CPU time is guaranteed yours.

The Norwegian Advantage

For those of you targeting users in Oslo, Bergen, or Trondheim, latency is a killer. Hosting in the US or Germany adds 30-50ms of latency. By placing your Load Balancer on CoolVDS infrastructure in Norway, you are peering directly at the NIX (Norwegian Internet Exchange). Your "Time to First Byte" drops significantly.

Furthermore, under the Personopplysningsloven (Personal Data Act of 2000), keeping data within national borders simplifies your compliance with the Datatilsynet (Data Inspectorate), especially given the uncertainty regarding US Patriot Act data access.

The Verdict

Don't wait for your server to crash during the Christmas rush. High availability is not just for Google or Facebook anymore. With HAProxy and a couple of solid VPS nodes, you can build a resilient, redundant stack for less than the cost of a nice dinner in Aker Brygge.

Ready to harden your stack? Spin up a CoolVDS Xen instance today and start balancing.

/// TAGS

/// RELATED POSTS

Surviving the Spike: High-Performance E-commerce Hosting Architecture for 2012

Is your Magento store ready for the holiday rush? We break down the Nginx, Varnish, and SSD tuning s...

Read More →

Automate or Die: Bulletproof Remote Backups with Rsync on CentOS 6

RAID is not a backup. Don't let a typo destroy your database. Learn how to set up automated, increme...

Read More →

Nginx as a Reverse Proxy: Stop Letting Apache Kill Your Server Load

Is your LAMP stack choking on traffic? Learn how to deploy Nginx as a high-performance reverse proxy...

Read More →

Apache vs Lighttpd in 2012: Squeezing Performance from Your Norway VPS

Is Apache's memory bloat killing your server? We benchmark the industry standard against the lightwe...

Read More →

Stop Guessing: Precision Server Monitoring with Munin & Nagios on CentOS 6

Is your server going down at 3 AM? Stop reactive fire-fighting. We detail the exact Nagios and Munin...

Read More →

The Sysadmin’s Guide to Bulletproof Automated Backups (2012 Edition)

RAID 10 is not a backup strategy. In this guide, we cover scripting rsync, rotating MySQL dumps, and...

Read More →
← Back to All Posts