Backend Development 8 min read

Comprehensive Guide to Load Balancing

This article provides an in‑depth overview of load balancing, covering its necessity, functions, principles, algorithms, and classifications across OSI layers, and compares popular software solutions for implementing four‑ and seven‑layer load balancers in modern distributed systems.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Comprehensive Guide to Load Balancing

Why Load Balancing Is Needed

When a system faces massive user traffic and high load, horizontal scaling by adding more servers and using clusters with load balancers improves overall processing capacity.

Moving from a single‑machine site to a distributed one involves splitting business logic and deploying components on different machines, creating a large‑scale distributed system.

Even after business splitting, each independent service still suffers from single‑point failures and the need for a unified entry point; redundancy and load‑balancing devices solve these issues.

Functions of Load Balancing

Alleviate concurrency pressure by increasing throughput and network handling capability.

Provide high availability through automatic failover.

Enable scalability by adding or removing servers.

Offer security protection via filtering, black‑/white‑lists, etc.

Load Balancing Principles

System expansion can be vertical (adding CPU, memory, storage) or horizontal (adding more machines). Vertical scaling cannot meet the demands of large‑scale, high‑traffic, high‑concurrency sites, so horizontal scaling with clusters and load balancers is required.

Typical architecture: multiple identical application instances form a cluster; a load‑balancing device distributes incoming requests to the cluster based on a chosen algorithm.

Application cluster: same application deployed on several machines, handling requests from the load balancer.

Load‑balancing device: forwards requests to a backend server according to the selected load‑balancing algorithm.

Load Balancing Algorithms

1. Round Robin

Requests are assigned sequentially to backend servers without considering current load; suitable when all servers have identical hardware.

2. Weighted Round Robin

Extends round robin by assigning weights based on server capacity; higher‑capacity servers receive more requests.

3. Random

Chooses a backend server randomly from the pool; with enough traffic the distribution approximates even load.

4. Least Connections

Tracks the number of active connections per server and forwards new requests to the server with the fewest connections (stateful, less commonly used).

5. Source IP Hash

Hashes the client’s IP address to consistently map the same client to the same backend server, provided the server list remains unchanged.

Load Balancing Classifications

1. Layer‑2 (MAC) Load Balancing

Uses virtual MAC addresses; external requests target the virtual MAC, which the balancer maps to real MACs of backend servers.

2. Layer‑3 (IP) Load Balancing

Employs virtual IP addresses; the balancer translates the virtual IP to the actual IP of a backend server.

3. Layer‑4 (TCP) Load Balancing

Operates at the transport layer, forwarding traffic based on IP + port without inspecting application data.

4. Layer‑7 (HTTP) Load Balancing

Works at the application layer, making routing decisions based on content such as URLs; enables content‑based routing (e.g., directing image requests to an image server).

Typical Software Implementations

Layer‑4: F5 (hardware, expensive), LVS (heavy‑weight software), Nginx (lightweight with caching), HAProxy (flexible).

Layer‑7: HAProxy (full L7 proxy with session persistence), Nginx (good HTTP/mail support), Apache (limited features).

In practice, LVS is often used for 4‑layer load balancing, while Nginx or HAProxy handle 7‑layer scenarios; performance ranking is LVS > HAProxy > Nginx, whereas feature convenience is Nginx > HAProxy > LVS.

Distributed SystemsAlgorithmbackend architecturescalabilityHigh AvailabilityLoad Balancing
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

login 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.