Load balancing explained

A load balancer receives every client connection and spreads the work across two or more backend servers, skipping any backend that fails its health check. CheapVPS has no load balancer product in the console, so you run one yourself: a small VPS with HAProxy or Nginx in front of two app servers gives the same spreading, health checks and TLS handling.

Networking and security guide, updated 2026-12-03

What does a load balancer actually do?

Clients connect to one address, the load balancer, and it decides which backend server handles each connection or request. That one step brings several benefits at once:

  • Capacity: two app servers can handle roughly twice the traffic of one, if the app is built to run on several machines.
  • Availability: when a backend crashes or is taken down for updates, the balancer stops sending it traffic and users keep working.
  • Rolling deploys: update one backend at a time while the other keeps serving.
  • One place for TLS, redirects and headers instead of repeating them on every app server.

What it does not do is make a single database faster, or make an application that keeps state in local files work across servers. Sessions, uploads and caches must live somewhere shared, such as a database, Redis or object storage you run yourself, before a second backend helps.

What is the difference between L4 and L7?

The numbers refer to layers of the network model.

A layer 4 balancer works with TCP or UDP connections. It sees addresses and ports, picks a backend when a connection opens, and then copies bytes back and forth without reading them. It is fast, protocol neutral, and works for databases, mail, game servers or anything else on TCP. It cannot route by URL, because it never looks at the HTTP request.

A layer 7 balancer understands the application protocol, usually HTTP. It reads each request, so it can send /api to one pool and /static to another, add headers, rewrite paths, retry a failed request on another backend, and balance per request instead of per connection. The cost is more CPU, and TLS must be decrypted at the balancer to see the request.

HAProxy does both (mode tcp and mode http). Nginx does L7 in its http block and L4 in its stream block.

Which balancing algorithm should you choose?

AlgorithmHow it picksGood for
Round robinEach backend in turn, optionally weightedShort, similar requests on equal servers
Least connectionsThe backend with the fewest open connectionsMixed or long requests, WebSockets
Source hashA hash of the client IPKeeping one client on one backend without cookies
URI hashA hash of the pathCaches, so each object lives on one backend

Round robin is a fine default. Switch to least connections as soon as requests vary a lot in duration, for example a mix of quick page views and slow report exports. Weights let you mix server sizes: a backend on P8 can take weight 2 next to a P4 at weight 1.

Source hash has a known weak spot: many users behind one carrier NAT or office gateway share an IP and all land on the same backend.

How do health checks work?

The balancer tests each backend on a schedule and removes the ones that fail. Two kinds exist:

  • Active checks send their own probe, such as a TCP connect or an HTTP request to /health, every few seconds. A backend is marked down after a number of failures in a row (often 3) and up again after a number of successes (often 2), so one slow reply does not flap it.
  • Passive checks watch real traffic: if requests to a backend fail several times, it is skipped for a while. Open source Nginx uses this method with max_fails and fail_timeout.

Make the health endpoint meaningful but cheap. A good /health confirms the app process is alive and can reach its database with a trivial query, then returns 200. Avoid checks that run heavy queries: every balancer hits them every few seconds, forever.

What are sticky sessions and when do you need them?

Sticky sessions (session affinity) send all requests from one user to the same backend. The balancer sets a cookie naming the backend, or uses a source hash, and honours it on later requests.

You need them when the app keeps session data in memory or on local disk: without stickiness, a user logs in on backend A, the next request lands on B, and the session is gone. The better long-term fix is to move sessions to Redis or the database, after which any backend can serve any request and stickiness becomes optional.

Stickiness has costs: load spreads less evenly, and when a backend fails its users lose their sessions anyway. Use it as a bridge, not a design goal. WebSocket connections are a separate case: each connection naturally stays on one backend for its lifetime.

How do backends see the real client IP?

Behind a balancer, every connection to a backend comes from the balancer address, so logs, rate limits and geo lookups all see the same IP. Two methods pass the original address along:

  • X-Forwarded-For is an HTTP header that an L7 balancer adds, for example X-Forwarded-For: 198.51.100.77. HAProxy adds it with option forwardfor; Nginx with proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for.
  • PROXY protocol prepends a short header with the client address to the TCP stream itself. It works in L4 mode and for non-HTTP protocols. HAProxy sends it with send-proxy-v2 on a server line; Nginx accepts it with listen 8080 proxy_protocol.

Backends must trust these values only from the balancer. In Nginx, set_real_ip_from 10.20.0.10; with real_ip_header X-Forwarded-For; rewrites the client address only for requests from that source. A backend that accepts the header from anyone lets clients forge their IP. PROXY protocol must be enabled on both sides at once, or every connection fails.

Where should TLS be terminated?

Three patterns are common:

  • Termination at the balancer: the balancer holds the certificate, decrypts, and forwards plain HTTP to backends. Simple and allows L7 routing. Send that plain traffic over a WireGuard tunnel so it is never in clear text on the internet.
  • Re-encryption: the balancer decrypts, inspects, and opens a new TLS connection to each backend. More work, full encryption end to end without a tunnel.
  • Passthrough: an L4 balancer forwards the encrypted stream untouched and each backend holds the certificate. No L7 features, but the balancer never sees content.

For a small stack, termination at the balancer plus a tunnel to the backends is the easiest to run. Certificates are covered in TLS certificates explained.

How do you run your own load balancer on a small VPS?

The console has no load balancer button, and you do not need one. A typical layout:

  1. One small server for the balancer. HAProxy and Nginx use little memory; P1 (1 vCPU, 1 GB RAM, 20 GB NVMe) is plenty for many sites.
  2. Two app servers sized for the application, in the same city as the balancer for low latency.
  3. A WireGuard mesh joining all three, so backends listen only on tunnel addresses.
  4. DNS for the site pointing at the balancer only.
  5. Firewall on the app servers allowing the app port on wg0 and nothing public except SSH.

The complete config is in HAProxy load balancer. Then think about the balancer itself as a single point of failure; Failover and floating IPs covers the options.

How does a balancer change your traffic budget?

Responses now take two hops. Each backend sends its reply to the balancer, which is outgoing traffic for the backend, and the balancer sends it on to the visitor, which is outgoing traffic for the balancer. A site that sends 1 TB a month to visitors therefore uses about 1 TB on the balancer and about 1 TB spread across the backends.

Plan the balancer size with that in mind: the included outgoing traffic depends on plan and region, for example 20 TB on P1 in Europe, and extra transfer there costs $0.72 per TB. Serving large static files straight from the balancer with a small cache, or offloading downloads to a separate server, keeps the double counting down. Current figures for every plan are on the pricing page.

Questions about this guide

Does CheapVPS offer a managed load balancer?

No. You run HAProxy or Nginx on a server of your own, which gives you full control over algorithms, health checks and TLS. The HAProxy guide walks through it.

How many backends do I need?

Two is the minimum that lets you lose one. Add more when two run at high CPU during normal peaks, not before.

Can a load balancer sit in a different city from the backends?

Yes, but every request then pays the round trip between cities on top of the visitor's own. Keep the balancer and backends in one city and use a second city for failover.

Can I balance database traffic?

Reads, yes, with a TCP mode balancer in front of read replicas. Writes must go to the primary. Many apps handle this better in the database driver or with a tool built for the specific database.

Is DNS round robin a load balancer?

A rough one. Several A records spread clients between servers, but DNS has no health checks, so clients keep trying a dead server until their cache expires.

What size should the balancer be?

Start small. Watch CPU during peak hours with htop; TLS handshakes are the main cost. Moving to a larger plan means ordering it and switching DNS, so leave some headroom.