Console Login

Surviving the Slashdot Effect: High Availability with HAProxy and Keepalived

Surviving the Slashdot Effect: High Availability with HAProxy and Keepalived

It starts with a trickle. Then, a flood. You hit the front page of Digg or get a link from a major news outlet. Suddenly, your Apache error logs are screaming, MaxClients reached, and your server load average is climbing past 20. If you are running a single box, you are already dead in the water.

I have seen seasoned systems administrators watch helplessly as their single LAMP stack melts down under a traffic spike. The solution isn't just "add more RAM." It is horizontal scaling. Today, we are going to look at how to deploy a robust load balancing layer using HAProxy 1.3 and Keepalived to ensure your infrastructure stays up, even when the traffic hits hard.

The Architecture: Decoupling Traffic from Logic

The biggest mistake I see in the Nordic hosting market is relying on hardware load balancers like F5 Big-IP for small to mid-sized deployments. They are expensive and overkill. Software load balancing on Linux is mature, efficient, and free.

Here is the battle-tested setup:

  • 2x Load Balancers (Active/Passive): Running HAProxy and Keepalived.
  • 2x Web Nodes: Apache or Lighttpd serving the application.
  • 1x Database Node: MySQL 5.1 (master).

This setup allows you to pull a web server out of rotation for maintenance without anyone noticing. It also handles the traffic distribution intelligently.

Configuring HAProxy for Performance

HAProxy (High Availability Proxy) is a beast. It handles TCP and HTTP traffic with incredibly low overhead. While Nginx is gaining traction as a reverse proxy, HAProxy is still the king of pure load balancing logic in 2010.

Here is a production-ready snippet for /etc/haproxy/haproxy.cfg that we use on our internal clusters:

global
    log 127.0.0.1   local0
    maxconn 4096
    user haproxy
    group haproxy
    daemon

defaults
    log     global
    mode    http
    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
    server web02 192.168.1.11:80 check

The balance roundrobin directive ensures requests are distributed evenly. The check flag is vital—it tells HAProxy to constantly ping the web servers. If web01 goes down (kernel panic, anyone?), HAProxy instantly routes all traffic to web02. Zero downtime for the user.

The Single Point of Failure: The Load Balancer Itself

Great, you have redundancy for your web servers. But what happens if the VPS running HAProxy crashes? Your entire site vanishes. This is where Keepalived and the VRRP (Virtual Router Redundancy Protocol) come into play.

Keepalived allows two servers to share a single "Virtual IP" (VIP). If the Master node stops broadcasting VRRP packets, the Backup node claims the IP within seconds.

Pro Tip: When configuring Keepalived on a VPS, ensure your provider supports multicast traffic or unicast VRRP. On many budget hosting platforms, multicast is blocked at the switch level, breaking VRRP. CoolVDS networks are specifically configured to allow VRRP traffic for this exact scenario.

Why Infrastructure Choice Matters

You can have the perfect config, but if the underlying metal is garbage, your latency will suffer. In Norway, latency is a competitive differentiator. Routing traffic from Oslo to a datacenter in Germany adds 20-30ms. Routing it to the US adds 100ms+. For a dynamic database-driven site, that delay kills the user experience.

Furthermore, disk I/O is the silent killer. Most providers pack hundreds of users onto a single SATA RAID array. When one neighbor starts a backup, your database locks up. We are seeing a massive shift toward Enterprise SSD storage. It is still expensive in 2010, but the IOPS performance is orders of magnitude higher than 15k RPM SAS drives.

Feature Budget VPS CoolVDS Architecture
Virtualization OpenVZ (Oversold RAM) Xen / KVM (Dedicated RAM)
Storage SATA II (Shared) Enterprise SSD / SAS 15k
Network Congested Transit Peering at NIX (Oslo)

Legal Compliance in Norway

We cannot ignore the Personopplysningsloven (Personal Data Act). As a Norwegian business, you are responsible for where your customer data lives. Hosting outside the EEA can be a legal minefield. By keeping your load balancers and database nodes on servers physically located in Norway, you satisfy the requirements of Datatilsynet and ensure your customer data remains under Norwegian jurisdiction.

Final Thoughts

Building a high-availability cluster used to require a rack of expensive hardware. Now, with tools like HAProxy and robust VPS hosting, you can achieve 99.99% uptime for a fraction of the cost. Do not wait for the crash to upgrade your architecture.

If you need a testing ground for your HAProxy config, spin up a VPS Norway instance with us. We offer the raw IOPS and low latency you need to handle the load.