Backend Development 13 min read

Mastering Load Balancing: When to Choose LVS, Nginx, or HAProxy

This article explains the principles, architectures, and trade‑offs of three popular software load balancers—LVS, Nginx, and HAProxy—guiding you to select the right solution based on traffic volume, protocol layer, and operational complexity.

Efficient Ops
Efficient Ops
Efficient Ops
Mastering Load Balancing: When to Choose LVS, Nginx, or HAProxy

Most modern Internet systems use server clusters, deploying identical services on multiple machines to provide a unified service. Before a web‑server cluster, a load‑balancing server directs client requests to the most suitable web server, achieving transparent request forwarding.

Cloud computing and distributed architectures treat backend servers as pooled compute and storage resources, exposing them as a single, seemingly limitless service.

The three most widely used software load balancers are LVS, Nginx, and HAProxy, each suited to different scales and requirements.

LVS

LVS (Linux Virtual Server) is built into the Linux kernel since 2.4, requiring no patches. It has matured since its inception in 1998.

LVS Architecture

LVS clusters consist of three layers: the front‑end Load Balancer, the middle Server Array, and the back‑end Shared Storage.

LVS Load‑Balancing Mechanism

LVS operates at Layer 4 (transport layer), balancing TCP/UDP traffic. Unlike Layer 7 solutions that parse HTTP, LVS simply forwards packets based on IP address and port, offering very high efficiency.

Forwarding is achieved via NAT (SNAT/DNAT) or Direct Routing (DR) modes.

NAT Mode

In NAT mode, LVS acts as a gateway, performing destination NAT (DNAT) to rewrite the packet’s destination IP to the real server (RS). The RS replies to the client using its own IP; LVS then performs source NAT (SNAT) to replace the source IP with the virtual IP (VIP), making the client believe the response came directly from the load balancer.

DR Mode

In Direct Routing, LVS and the RS share the same VIP. LVS only rewrites the MAC address to forward the packet to a specific RS; IP addresses remain unchanged. The RS processes the request and replies directly to the client, bypassing LVS, which eliminates a bandwidth bottleneck and improves performance.

LVS Advantages

Strong load‑handling capacity with low CPU/memory usage.

Simple configuration reduces human error.

Stable operation with built‑in high‑availability (e.g., LVS + Keepalived).

No traffic generation; only request forwarding.

Broad applicability to HTTP, databases, chat services, etc.

LVS Disadvantages

Cannot process regular expressions; lacks content‑based routing.

Complex to deploy for large sites compared to Nginx/HAProxy + Keepalived.

Nginx

Nginx is a high‑performance web server and reverse proxy that excels at handling massive concurrent HTTP connections.

Architecture

Unlike process‑oriented servers (e.g., Apache), Nginx uses an event‑driven, asynchronous, single‑threaded model. It consists of one master process and multiple worker processes that share memory and handle connections via epoll.

Load‑Balancing Features

Nginx performs Layer 7 (application‑layer) load balancing for HTTP/HTTPS, using reverse‑proxy techniques. Supported upstream strategies include:

Round‑robin (default)

Weight‑based distribution

IP‑hash for session persistence

Fair (response‑time based, third‑party)

URL‑hash (third‑party)

Advantages

Cross‑platform (Unix‑like OS and Windows)

Simple configuration

Non‑blocking, high concurrency (tens of thousands of connections)

Event‑driven epoll model

Master/worker process model

Low memory footprint (≈15 MB per worker)

Built‑in health checks

GZIP compression and bandwidth savings

High stability as a reverse proxy

Disadvantages

Supports only HTTP/HTTPS and email protocols.

Health checks limited to port probing; session persistence relies on IP‑hash.

HAProxy

HAProxy supports both TCP (Layer 4) and HTTP (Layer 7) proxy modes and offers virtual hosting.

It complements Nginx by providing session persistence, cookie‑based routing, and URL‑based health checks. Generally, HAProxy delivers higher throughput and better concurrency than Nginx.

HAProxy can also balance MySQL traffic and supports numerous load‑balancing algorithms such as round‑robin, weighted round‑robin, source‑IP persistence, request‑URL, and cookie‑based methods.

Reference

Sources: Zhongwu, Wang Chen‑chun, Zhou Xulong, and related online articles.

backendLoad BalancingNetworkNginxhaproxyLVS
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.