Operations 14 min read

Mastering Load Balancing: LVS, Nginx, and HAProxy Explained

This article introduces server clustering and load‑balancing concepts, compares popular software such as LVS, Nginx, and HAProxy, explains their architectures, NAT and DR modes, and outlines each solution's strengths and weaknesses for building high‑performance web services.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Load Balancing: LVS, Nginx, and HAProxy Explained

Most modern Internet systems use server clusters, deploying identical services on multiple machines to form a unified service front‑end; these clusters can be web‑application servers, database servers, or distributed cache servers.

Typically a load‑balancing server sits in front of a web‑server cluster, acting as the entry point that selects the most suitable web server and forwards client requests transparently.

Cloud computing and distributed architectures essentially wrap backend servers as compute and storage resources behind a management server, presenting clients with the illusion of a single, virtually unlimited server while the real work is done by the backend cluster.

The three most widely used software load balancers are LVS, Nginx, and HAProxy.

LVS

LVS (Linux Virtual Server) is now part of the standard Linux kernel (since 2.4) and requires no patches.

It has matured since its inception in 1998.

LVS Architecture

The LVS cluster consists of three layers:

Load Balancer layer (front‑end)

Server Array layer (middle)

Shared Storage layer (bottom)

LVS Load‑Balancing Mechanism

LVS operates at layer 4 (transport layer), handling TCP/UDP traffic, which gives it higher efficiency compared to layer 7 solutions that parse HTTP content.

It forwards packets by modifying IP addresses (NAT mode: SNAT/DNAT) or MAC addresses (DR mode).

NAT Mode

In NAT mode, LVS acts as a gateway for real servers (RS). Incoming packets are DNAT‑ed to the RS IP; responses are SNAT‑ed back to the virtual IP (VIP), making the client see LVS as the source.

DR Mode

In DR mode, LVS and RS share the same VIP. LVS only rewrites the destination MAC address, leaving IP addresses unchanged, allowing the RS to reply directly to the client, which reduces bandwidth bottlenecks and improves performance.

Advantages of LVS

Strong load‑handling capability with low CPU and memory consumption.

Simple configuration reduces human error.

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

No traffic passes through the balancer itself, preserving I/O performance.

Broad applicability to any TCP/UDP service (HTTP, databases, chat, etc.).

Disadvantages of LVS

Cannot process regular expressions, limiting 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 requests.

Nginx Architecture

Unlike process‑oriented servers (e.g., Apache), Nginx uses an event‑driven, asynchronous, single‑threaded model with a master process and multiple worker processes that share memory.

The master handles signals and monitors workers; each worker processes requests independently, avoiding the overhead of per‑connection threads.

Nginx Load‑Balancing

Nginx performs layer 7 (application‑layer) load balancing via reverse proxy, supporting various upstream strategies:

Round‑robin (default)

Weighted round‑robin

IP hash (session affinity)

Fair (response‑time based, third‑party)

URL hash (third‑party)

Advantages of Nginx

Cross‑platform support

Simple configuration

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

Event‑driven using epoll

Master/worker process model

Low memory usage (e.g., 10 workers consume ~150 MB for 30 k concurrent connections)

Built‑in health checks

Bandwidth saving via GZIP and caching headers

High stability as a reverse proxy

Disadvantages of Nginx

Limited to HTTP, HTTPS, and email protocols

Health checks only via port, not URL; session persistence requires workarounds like ip_hash

HAProxy

HAProxy supports both TCP (layer 4) and HTTP (layer 7) proxying and virtual hosting.

It complements Nginx by offering session persistence, cookie‑based routing, and URL‑based health checks.

Performance‑wise, HAProxy often outperforms Nginx in raw load‑balancing speed and concurrency.

It can balance MySQL traffic and works well with LVS + Keepalived for database master‑slave setups.

HAProxy provides many load‑balancing algorithms, including round‑robin, weighted round‑robin, source IP preservation, request‑URL based, and cookie‑based methods.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Operationsload balancingHAProxyLVSserver clusters
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.