Understanding Server Clustering and Load Balancing: LVS, Nginx, and HAProxy
This article explains server clustering and load‑balancing concepts, detailing the architecture and operation of LVS, Nginx, and HAProxy, and compares their advantages, disadvantages, and typical deployment scenarios; it also discusses NAT and DR modes, load‑balancer placement, and best‑practice recommendations for different traffic volumes.
Most Internet systems use server‑cluster technology, where a cluster deploys the same service on multiple machines to present a single service endpoint; clusters can be web‑application servers, database servers, distributed caches, etc.
In practice a load‑balancing server sits in front of a web‑server cluster, acting as the entry point, selecting the most suitable web server and forwarding client requests, achieving transparent client‑to‑backend communication.
Modern cloud‑computing and distributed architectures essentially package backend servers as compute and storage resources, exposing them as a seemingly unlimited server while the real work is performed by the backend cluster.
The three most widely used software load balancers are LVS, Nginx, and HAProxy. Choice depends on site scale: small sites (PV < 10 million) can use Nginx; medium sites may use DNS round‑robin or LVS; large sites with many servers often adopt LVS.
A common architecture consists of a front‑end load‑balancer layer (e.g., Keepalived + Nginx/HAProxy), a middle server‑array layer (backend cluster), and a bottom shared‑storage layer; the backend often uses MySQL master‑slave with read/write separation, sometimes combined with LVS + Keepalived.
LVS (Linux Virtual Server) is built into the Linux kernel since 2.4, providing four‑layer (transport‑layer) load balancing for TCP/UDP. It works in NAT mode (modifying destination IP – DNAT – and source IP – SNAT – via a virtual IP) or DR mode (direct routing, only changing the destination MAC address, letting the real server reply directly to the client, which yields higher performance).
Advantages of LVS include strong load capacity, low CPU/memory consumption, simple configuration, built‑in high‑availability (LVS + Keepalived), no traffic passes through the balancer, and wide protocol applicability. Disadvantages are lack of regex or content‑based routing, limited health‑check capabilities, and more complex deployment compared with Nginx/HAProxy.
Nginx is a high‑performance web server and reverse proxy that uses an event‑driven, asynchronous, single‑threaded architecture with a master‑worker model. It handles high concurrency with low memory usage, supports multiple load‑balancing algorithms (round‑robin, weight, ip_hash, fair, url_hash), and offers features such as cross‑platform support, simple configuration, epoll‑based event handling, built‑in health checks, gzip compression, and high stability. Its drawbacks are support only for HTTP/HTTPS (and email), health checks limited to port probing, and lack of built‑in session persistence beyond ip_hash.
HAProxy supports both TCP (layer‑4) and HTTP (layer‑7) proxying and offers many load‑balancing strategies (round‑robin, weighted round‑robin, source, RI, rdp‑cookie). It complements Nginx by providing session persistence, cookie‑based routing, URL health checks, and generally higher efficiency and concurrency handling.
References: https://zhongwuzw.github.io, http://www.importnew.com/11229.html, http://edisonchou.cnblogs.com.
JD Tech
Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.