Mastering Software Load Balancing: Nginx, LVS, and HAProxy Deep Dive

This article provides a comprehensive overview of software load balancing, detailing the architecture, features, performance characteristics, and various load‑balancing strategies of Nginx, LVS, and HAProxy, and explains how to choose and deploy them in different scenarios.

21CTO
21CTO
21CTO
Mastering Software Load Balancing: Nginx, LVS, and HAProxy Deep Dive

1. Overview of Software Load Balancing

Hardware load balancers offer superior performance but are expensive, so software load balancers like Nginx, LVS, and HAProxy are widely used in the Internet. This article compiles information from many sources.

2. Nginx Load Balancing

Nginx is a lightweight web server/reverse proxy that operates at the HTTP layer, offering high performance, high concurrency, and low memory usage. It supports up to 50,000 concurrent connections.

Supported OS: Linux, Windows, FreeBSD, Solaris, macOS, AIX. Development language: C. Official concurrency: 50,000 requests per second (typical 20,000 in China, up to 100,000 with optimization).

2.1 Features

Modular design with good extensibility.

High reliability with master and worker processes.

Low memory consumption (10,000 keep‑alive connections use ~2.5 MB).

Hot deployment without stopping the server.

Strong concurrency support.

Rich functionality including reverse proxy and flexible load‑balancing strategies.

2.2 Functions

Static web server.

Reverse proxy and caching for HTTP, SMTP, POP3.

FASTCGI (FPM) support.

Modules for compression, SSL, image resizing, etc.

Built‑in health checks.

Virtual hosting by name and IP.

Custom access logs.

Smooth upgrades.

Keep‑alive support.

URL rewrite and path aliasing.

Access control based on IP and username.

Rate limiting and connection limiting.

2.3 Performance

Nginx officially supports 50,000 concurrent connections; real‑world deployments often achieve 20,000–30,000. Ten thousand idle keep‑alive connections consume about 2.5 MB of memory. On a 24 GB machine, 200 000 concurrent requests were observed.

2.4 Architecture

A master process spawns one or more worker processes. The master (running as root) loads configuration and handles graceful upgrades, while workers handle request processing via a pipeline of modules.

2.5 Load‑Balancing Strategies

Weighted round‑robin (built‑in).

IP hash (built‑in).

Fair (extension, selects server with lowest response time).

Generic hash and consistent hash (extensions).

3. LVS Load Balancing

LVS (Linux Virtual Server) is an open‑source IP‑layer load balancer created in 1998. It distributes TCP/UDP requests across multiple real servers, providing high performance and high availability.

Supported OS: Linux. Development language: C. Default concurrency: 4,096 connections (configurable).

3.1 Functions

LVS offers three forwarding modes: NAT, TUN (IP tunneling), and DR (direct routing).

3.1.1 NAT

The director rewrites the destination IP and forwards packets to real servers; responses return via the director, which rewrites the source IP.

3.1.2 TUN

The director tunnels packets to real servers; responses go directly to the client, reducing load on the director.

3.1.3 DR

Direct routing sends packets straight to real servers; servers reply directly to clients, requiring all servers to be on the same LAN.

3.2 Architecture

Three layers: Load Balancer (director), Server Array (real servers), and Shared Storage (shared data). The director runs LVS modules and monitors server health.

3.3 Load‑Balancing Strategies

Round Robin

Weighted Round Robin

Least Connections

Weighted Least Connections

Locality‑Based Least Connections

Locality‑Based Least Connections with Replication

Destination Hashing

Source Hashing

4. HAProxy Load Balancing

HAProxy is a high‑performance, open‑source load balancer and proxy supporting TCP (layer 4) and HTTP (layer 7). It offers high availability, virtual hosting, and is suitable for traffic‑intensive web sites.

4.1 Features

Supports TCP and HTTP proxy modes with virtual hosting.

Simple configuration and URL health checks.

Higher throughput than Nginx under high concurrency.

TCP mode often used for MySQL read‑replica balancing.

Complements Nginx by handling session persistence and cookie routing.

4.2 Load‑Balancing Algorithms

roundrobin – simple round‑robin.

static‑rr – weighted round‑robin based on server capacity.

leastconn – prefers servers with fewest active connections.

source – hashes source IP (similar to Nginx’s ip_hash).

5. Summary

This week’s sharing covered the background of software load balancing, detailed the features, architectures, and strategies of Nginx, LVS, and HAProxy, and provided guidance on selecting appropriate solutions for various scenarios.

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.

Backendload balancingnetworkNGINXHAProxyLVS
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

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.