Understanding Load Balancing: Types, Tools, and Algorithms Explained
This article explains what load balancing is, its role in distributing traffic across server clusters, outlines the various layer‑based classifications (L2‑L7), compares popular software solutions such as LVS, Nginx and HAProxy, and reviews common load‑balancing algorithms and their use cases.
What is Load Balancing
When a website grows beyond a single machine, performance and stability become challenges. Load balancing distributes incoming user requests (e.g., to www.taobao.com) across multiple servers in a cluster, providing better service and higher availability.
Most Internet systems deploy server clusters—Web, database, or cache clusters—behind a single entry point. A load balancer receives client requests and forwards them to the appropriate backend server, making the redirection transparent to the client.
Load Balancing Classification
Load balancing can be categorized by the OSI layer it operates on. The most common types are
四层(Layer 4) and
七层(Layer 7).
Layer 2 Load Balancing
The balancer provides a virtual IP (VIP). All servers share the same IP but have different MAC addresses. The balancer rewrites the destination MAC address to forward traffic to the selected server.
Layer 3 Load Balancing
Similar to Layer 2, but each server has a distinct IP address. The balancer selects a server based on load‑balancing algorithms and forwards the request using the server’s IP.
Layer 4 Load Balancing
Operating at the transport layer (TCP/UDP), the balancer modifies the packet’s IP and port information to route traffic to the appropriate application server.
Layer 7 Load Balancing
Operating at the application layer, the balancer can make routing decisions based on HTTP URLs, browser types, language, etc., in addition to IP and port.
Common Load Balancing Tools
1. LVS
LVS (Linux Virtual Server) is an open‑source project that provides high‑performance, high‑availability server clusters, mainly for Layer 4 load balancing.
LVS architecture consists of three layers:
Load Balancer layer : The front‑most director server runs the LVS module and a health‑check daemon (ldirectord) to monitor real servers.
Server Array layer : The actual application servers (Web, Mail, FTP, DNS, video, etc.) that handle client requests.
Shared Storage layer : Provides shared storage for all real servers, typically via NFS or a cluster file system.
2. Nginx
Nginx is a web server that also functions as a reverse proxy, HTTP/HTTPS, SMTP, POP3, IMAP proxy, load balancer, and HTTP cache.
It is primarily used for Layer 7 load balancing.
Modular design with extensible modules.
High reliability with master‑worker architecture.
Low memory consumption (10,000 keep‑alive connections use ~2.5 MB).
Hot deployment without stopping the server.
Strong concurrency (up to 50 k requests per second).
Rich feature set for reverse proxy and load‑balancing strategies.
3. HAProxy
HAProxy is an open‑source, high‑availability load balancer and proxy for TCP and HTTP applications, suitable for high‑traffic web sites.
It is mainly used for Layer 7 load balancing.
Typical Load Balancing Algorithms
Load‑balancing algorithms fall into two categories: static and dynamic.
Static Algorithms
Round Robin – sequentially distributes requests across servers.
Random – assigns requests randomly.
Hash – maps a key (e.g., client IP) to a specific server.
Ratio – assigns weighted traffic based on server capacity.
Priority – groups servers by priority, using higher‑priority groups first.
Dynamic Algorithms
Least Connection – sends traffic to the server with the fewest active connections.
Fastest – selects the server with the lowest response time.
Observed – balances based on a combination of connection count and response time.
Predictive – uses performance metrics to forecast the best server.
Dynamic Ratio‑APM – adjusts traffic based on real‑time performance data.
Dynamic Server Act. – adds backup servers when primary ones fail.
QoS – allocates bandwidth according to priority levels.
ToS – balances based on service type.
Rule Mode – custom routing rules defined by the user.
Other strategies include consistent hashing (to minimize impact of node failures) and pure dynamic load balancing based on CPU, I/O, or network utilization.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.