Operations 13 min read

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

This article explains how modern internet systems use server clusters and load balancers, compares the three most popular software solutions—LVS, Nginx, and HAProxy—covers their architectures, NAT and DR modes, advantages, disadvantages, and provides guidance on selecting the right tool for different scale scenarios.

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

Most modern internet systems rely on server clusters, deploying identical services across multiple machines to form a unified service; clusters can be Web servers, databases, or distributed caches.

In front of a Web server cluster, a load‑balancing server acts as the entry point, selecting the most suitable Web server and transparently forwarding client requests.

Cloud computing and distributed architectures essentially package backend servers as compute and storage resources behind a management server, presenting an illusion of a single, virtually unlimited server.

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:

Front‑end load‑balancing layer (Load Balancer)

Middle server array layer (Server Array)

Back‑end shared storage layer (Shared Storage)

LVS Load‑Balancing Mechanism

LVS operates at the transport layer (Layer 4), supporting TCP/UDP load balancing, which makes it highly efficient compared to higher‑level solutions.

It forwards traffic 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 perceive LVS as the source.

DR Mode

In DR (Direct Routing) mode, LVS and RS share the same VIP. LVS only rewrites the destination MAC, forwarding the packet to the appropriate RS without changing IP addresses, allowing the RS to reply directly to the client, which 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, including HTTP, databases, chat systems, etc.

Disadvantages of LVS

Lacks regex support, limiting static/dynamic content separation.

Complex deployment for large sites (DR + Keepalived) compared to Nginx/HAProxy solutions.

Nginx

Nginx is a high‑performance Web server and reverse proxy that handles massive concurrent HTTP requests with an event‑driven, asynchronous, single‑threaded architecture.

Nginx Architecture

It uses a master process and multiple worker processes; all workers are single‑threaded and communicate via shared memory, avoiding the heavy process/thread model of traditional servers like Apache.

Nginx Load Balancing

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 (session affinity)

Fair (response‑time based, third‑party)

URL hash (cache‑friendly, third‑party)

Advantages of Nginx

Cross‑platform support (Unix‑like OSes and Windows)

Simple configuration

Non‑blocking, high‑concurrency (tested up to 50 k concurrent connections)

Event‑driven using epoll

Master/worker model

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

Built‑in health checks

Bandwidth saving via GZIP and caching headers

High stability as a reverse proxy

Disadvantages of Nginx

Only supports HTTP, HTTPS, and email protocols.

Health checks limited to port probing; no URL‑level checks; session persistence requires IP hash.

HAProxy

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

Its strengths complement Nginx’s weaknesses, offering session persistence, cookie‑based routing, and URL‑level health checks.

HAProxy generally provides higher efficiency and better concurrency handling than Nginx.

It can load‑balance MySQL reads and perform health checks on MySQL nodes, often used together with LVS + Keepalived for database replication.

Available load‑balancing algorithms include round‑robin, weighted round‑robin, source (client IP preservation), request‑URL, 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 balancingNginxHAProxyLVSserver 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.