UnderHost
Knowledgebase Docs

VPS Load Balancing: Distribute Traffic Across Servers

Setup load balancing for VPS. HAProxy, Nginx, round-robin, least connections, health checks, failover, session persistence.

On this page

Load balancing distributes traffic across multiple backend servers, preventing any single server from becoming a bottleneck. With load balancing, you can handle 10x more traffic, maintain uptime during maintenance, and scale horizontally. Essential for high-traffic applications.

Load Balancing Overview

Why load balancing matters:**

  • Scalability: Handle more traffic by adding servers
  • Redundancy: If one server dies, traffic routes to others
  • Maintenance: Update servers without downtime
  • Performance: Distribute requests evenly
  • Cost: Multiple small servers cheaper than one huge server

Typical architecture:**

Internet → Load Balancer (HAProxy/Nginx) → Web Server 1
                                        → Web Server 2
                                        → Web Server 3

Load Balancing Algorithms

AlgorithmDescriptionBest For
Round RobinDistribute requests sequentiallyEqual-capacity servers
Least ConnectionsSend to server with fewest active connectionsVariable request duration
IP HashRoute based on client IPSession persistence
WeightedAssign weight to each serverDifferent server capacities
RandomPick random serverSimple, rarely used

HAProxy Setup

Install HAProxy:**

apt update && apt install haproxy -y
systemctl enable haproxy
systemctl start haproxy

Configure /etc/haproxy/haproxy.cfg:**

global
    log stdout local0
    maxconn 4096

frontend web_in
    bind *:80
    mode http
    default_backend web_servers

backend web_servers
    mode http
    balance roundrobin

    server web1 10.0.0.1:80 check
    server web2 10.0.0.2:80 check
    server web3 10.0.0.3:80 check

    # Health check (every 2 seconds, fail after 3 failures)
    default-server inter 2000 fall 3 rise 2

Restart HAProxy:**

systemctl restart haproxy
systemctl status haproxy

Nginx Load Balancer

Nginx for load balancing (upstream):**

upstream backend {
    least_conn;  # Least connections algorithm

    server 10.0.0.1:8000 weight=1;
    server 10.0.0.2:8000 weight=1;
    server 10.0.0.3:8000 weight=2;  # Handle 2x more traffic

    # Health check
    server 10.0.0.4:8000 backup;  # Backup if others fail
}

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Health Checks

HAProxy health checks:**

# HTTP health check (GET /health endpoint)
server web1 10.0.0.1:80 check http \
  option httpchk GET /health \
  http-check expect status 200

Implement health check endpoint in your app:**

# Express.js example
app.get('/health', (req, res) => {
    res.json({ status: 'ok', uptime: process.uptime() });
});

# Flask example
@app.route('/health')
def health():
    return {'status': 'ok'}

Session Persistence

Sticky sessions (route same user to same server):**

# HAProxy: use JSESSIONID cookie
backend web_servers
    cookie JSESSIONID prefix nocache
    server web1 10.0.0.1:80 check cookie web1
    server web2 10.0.0.2:80 check cookie web2

# Or use source IP-based persistence
backend web_servers
    balance source  # IP hash algorithm

Horizontal Scaling

Add new server to load balancer:**

# In HAProxy config, add line:
server web4 10.0.0.4:80 check

# Reload without restarting
haproxy -f /etc/haproxy/haproxy.cfg -sf $(pgrep haproxy)

Remove server for maintenance:**

# Mark as backup (stops receiving traffic)
server web1 10.0.0.1:80 check backup

# Or set to drain (complete active connections, stop new ones)
set server backend/web1 state drain

Monitor Load Balancer

HAProxy stats page:**

# In haproxy.cfg, enable stats
listen stats
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 5s

# Access at: http://server:8404/stats

Monitor backend server health:**

tail -f /var/log/haproxy.log | grep "backend"
# Shows server up/down status
Load balancing enables horizontal scaling—add servers, not bigger servers

With load balancing and stateless applications, you can scale from 1 to 100 servers without architectural changes. Essential for growth.

Related: VPS clustering | High availability | Scaling strategy | Nginx reverse proxy

Was this article helpful?

Need a Cloud VPS?

Launch an UnderHost Cloud VPS when you need root access, dedicated resources, custom software, or more control than shared hosting.

Back to Cloud VPS