Operations 6 min read

Master Nginx Load Balancing: Round Robin, Weighted, IP Hash & Least Connections Explained

This article explains Nginx's core load‑balancing algorithms—Round Robin, Weighted Round Robin, IP‑Hash, and Least Connections—detailing their working principles, configuration examples, advantages, disadvantages, and ideal use‑cases for building reliable backend services.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Master Nginx Load Balancing: Round Robin, Weighted, IP Hash & Least Connections Explained

Round Robin (default)

Nginx uses Round Robin as its default load‑balancing method, distributing incoming requests to backend servers sequentially like a circular queue.

Working principle: Requests are assigned to servers in time order, one after another.

Advantages: Simple and fair, ensuring each server receives an equal number of requests.

Disadvantages: It cannot detect the actual processing capacity of servers; a slower server may become overloaded.

Suitable scenario: When backend servers have similar performance and request processing times.

upstream myapp {
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080;
}

Weighted Round Robin

Weighted Round Robin enhances the basic algorithm by assigning a weight to each server, reflecting its processing capability.

Working principle: Requests are distributed according to the weight ratio; higher weight means more requests.

Advantages: Smart distribution based on server performance, making better use of hardware resources.

Disadvantages: Still sequential; cannot sense real‑time load.

Suitable scenario: When backend servers have unequal performance or when gradually increasing traffic to new servers.

upstream myapp {
    # Best server, receives 10 requests
    server 192.168.1.10:8080 weight=10;
    # Average server, receives 5 requests
    server 192.168.1.11:8080 weight=5;
    # Backup server, receives 1 request
    server 192.168.1.12:8080 weight=1;
}

IP Hash

IP Hash hashes the client’s IP address and consistently routes requests from the same IP to the same backend server.

Working principle: Nginx uses the ip_hash directive to map the client IP hash to a specific server.

Advantages: Session persistence – the same client always reaches the same server, which is ideal for stateful applications like shopping carts.

Disadvantages: Uneven distribution if some IPs generate much more traffic; a single server failure can affect all its bound clients.

Suitable scenario: When session affinity is required without implementing shared session storage.

upstream myapp {
    ip_hash;
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080;
}

Least Connections

Least Connections dynamically routes each request to the backend server with the fewest active connections.

Working principle: Nginx monitors current connection counts and selects the server with the smallest number.

Advantages: Real‑time load awareness, fairer distribution, and better handling of long‑lived connections such as WebSocket.

Disadvantages: Slight overhead to maintain connection state, though generally negligible.

Suitable scenario: Environments where request processing times vary widely or many long‑running connections exist.

upstream myapp {
    least_conn;
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080;
}
Load balancing illustration
Load balancing illustration
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 balancingRound RobinIP HashLeast Connectionsweighted
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

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.