Mastering Traffic Distribution for Distributed Systems
By The Coding Gopher
Published: 2026-07-09
URL: https://thecodinggopher.substack.com/p/load-balancing-algorithms
Deconstructing the routing logic that prevents server clusters from collapsing under scale.

Routing incoming network traffic across a fleet of servers seems simple in theory: just divide the total number of requests by the number of servers.

In engineering reality, however, this approach is a recipe for catastrophic failure. Not all HTTP requests require the same amount of CPU. Not all servers possess the exact same hardware capabilities. If a load balancer blindly distributes traffic without understanding the state of the backend infrastructure, one server will inevitably hit 100% CPU utilization while the others sit completely idle.

\Static and dynamic load balancing determine how traffic is distributed across servers. Static load balancing divides traffic based on a fixed, predetermined schedule without checking server health. Dynamic load balancing assesses real-time server loads and health to route traffic.\
The intelligence of a reverse proxy or load balancer (like NGINX, HAProxy, or Envoy) is entirely defined by its routing algorithm. Here is a deep dive into the evolution of load balancing algorithms, from stateless static routing to probabilistic dynamic distribution.
Static Distribution: The Stateless Approach
Static algorithms do not monitor the health, CPU load, or active connections of the backend servers. They route traffic based purely on predetermined mathematical rules. Because they maintain zero state, they execute in microseconds.
Round Robin
The most fundamental algorithm. The load balancer maintains a list of server IPs and routes incoming requests sequentially: Request 1 goes to Server A, Request 2 to Server B, Request 3 to Server C, and Request 4 loops back to Server A.

While incredibly fast, Round Robin operates on a deeply flawed assumption: that every request takes the exact same amount of time to process. If Server A receives a complex database query request, and Server B receives a simple static image request, Server A will quickly become backlogged while Server B finishes instantly.
Weighted Round Robin
To account for varying hardware, engineers can assign a “weight” to each server. If Server A has 64GB of RAM and Server B has 16GB, you assign Server A a weight of 4 and Server B a weight of 1. The algorithm will sequentially route four requests to Server A before routing a single request to Server B.

IP Hashing (Sticky Sessions)
Some legacy architectures require a user to maintain a persistent session with the exact same backend server for the duration of their visit (often because session data is stored in the server’s local memory rather than a centralized Redis cache).

The load balancer takes the client’s IP address, runs it through a cryptographic hash function, and uses a modulo operation against the total number of servers to determine the route.
Because the hash output is deterministic, a user in Toronto with a static IP will always be routed to the exact same server instance. The severe downside is that if a corporate office shares a single public IP address, hundreds of employees will be aggressively routed to a single node, completely destroying the balance of the cluster.
𝐋𝐞𝐚𝐫𝐧 𝐭𝐨 𝐛𝐮𝐢𝐥𝐝 𝐆𝐢𝐭, 𝐃𝐨𝐜𝐤𝐞𝐫, 𝐑𝐞𝐝𝐢𝐬, 𝐇𝐓𝐓𝐏 𝐬𝐞𝐫𝐯𝐞𝐫𝐬, 𝐚𝐧𝐝 𝐜𝐨𝐦𝐩𝐢𝐥𝐞𝐫𝐬, 𝐟𝐫𝐨𝐦 𝐬𝐜𝐫𝐚𝐭𝐜𝐡. Get 40% OFF CodeCrafters: <https://app.codecrafters.io/join?via=the-coding-gopher>
This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.
Dynamic Distribution: The Stateful Approach
To prevent servers from buckling under heavy queries, modern load balancers must track the real-time state of the backend cluster.
Least Connections
Instead of blindly rotating traffic, the load balancer maintains a continuous count of how many active, open TCP connections exist on every backend server. When a new request arrives, it scans the entire cluster and routes the request to the server with the lowest number of active connections.

This naturally solves the Round Robin problem. If Server A is bogged down processing a heavy video upload, its connection stays open. The load balancer sees that Server A is busy and seamlessly redirects new, lightweight requests to the idle Server B.

Least Response Time
Tracking connections is effective, but it doesn’t account for latency. A server might only have 5 active connections, but if its underlying hard drive is failing, those 5 connections might be agonizingly slow.

The Least Response Time algorithm combines two metrics: the number of active connections and a moving average of the server’s Time to First Byte (TTFB). The load balancer mathematically determines which server is currently responding the fastest and aggressively routes traffic there, ensuring the end user receives the lowest possible latency.
Time to first byte (TTFB) is a measurement used as an indication of the responsiveness of a web server or other network resource.
The Distributed Systems Bottleneck
As infrastructure scales from dozens of servers to tens of thousands, the traditional dynamic algorithms start to break down due to a fundamental computer science problem: global state.
If a load balancer needs to route 100,000 requests per second using the Least Connections algorithm, it has to scan the connection count of every single server 100,000 times a second, sort them, and pick the winner. This requires establishing global locks on the connection data, which introduces massive CPU overhead on the load balancer itself.
To survive hyper-scale, engineers had to abandon absolute precision in favor of probabilistic mathematics.
The Power of Two Choices (P2C)
Adopted by massive distributed systems and modern proxies like Envoy, P2C is a marvel of probabilistic engineering.

Instead of scanning the entire cluster to find the absolute least loaded server, the load balancer randomly selects exactly two servers. It compares the active connection count of only those two servers, and routes the request to the lesser of the two.
Mathematically, this avoids the O(N) overhead of scanning the whole cluster, operating in strict O(1) constant time. Surprisingly, probability theory proves that consistently picking the better of two random choices prevents the “herd effect” and balances a massive cluster almost as perfectly as if the load balancer had scanned every single node, utilizing a fraction of the compute power.
The Hashing Problem: Consistent Hashing
When load balancing across stateful infrastructure—like a cluster of distributed Redis caches—standard IP Hashing introduces a catastrophic flaw.
If you have 4 cache servers, the standard routing formula is Hash(IP) % 4.
If Server 3 suddenly experiences a hardware failure and drops offline, the total number of servers becomes 3. The formula instantly changes to Hash(IP) % 3. Because the denominator changed, the routing destination for almost every single user changes simultaneously. Every user misses their cache, querying the primary database at the exact same second, resulting in a total system crash.
Consistent Hashing solves this by arranging the servers on a virtual, mathematical ring (from 0 to 2 to the power of 32). The user’s IP is hashed to a specific point on the ring, and the algorithm moves clockwise until it finds the first available server.

If a server dies, only the users directly mapped to that specific node are re-routed to the next node in the ring. The rest of the cluster remains entirely unaffected, preserving the cache hit rate and saving the database from a massive traffic spike.
This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.
Thanks for reading! This post is public so feel free to share it.