ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
LB
systems · 13 min read

Load Balancing Techniques

In today’s hyper‑connected world, a single web request can travel across continents, bounce through firewalls, and be processed by dozens of micro‑services…

Introduction

In today’s hyper‑connected world, a single web request can travel across continents, bounce through firewalls, and be processed by dozens of micro‑services before a user sees a response. The invisible hand that keeps that journey smooth, reliable, and fast is load balancing – the practice of distributing network traffic or computational work across multiple servers, containers, or even entire data centers. Without an effective load‑balancing strategy, a sudden spike in traffic can overwhelm a single node, leading to latency spikes, dropped connections, and ultimately loss of trust.

For platforms like Apiary, which blend bee‑conservation data pipelines with self‑governing AI agents, the stakes are especially high. Real‑time sensor streams from apiaries, AI‑driven analytics, and public API endpoints all compete for the same compute pool. A well‑chosen load‑balancing technique can mean the difference between a thriving digital ecosystem and a stalled one, much like how a healthy bee colony distributes foraging duties to avoid exhausting any single flower patch.

This article dives deep into the three most widely used traffic‑distribution algorithms—round‑robin, least‑connections, and weighted strategies—examining how they work, where they excel, and how they can be combined with modern health‑checking, session‑persistence, and cloud‑native features. By the end, you’ll have a practical toolbox for matching the right algorithm to your workload, and a fresh perspective on how nature’s own balancing acts can inspire more resilient systems.


1. The Fundamentals of Load Balancing

Before exploring individual algorithms, it helps to understand the broader architecture. A load balancer sits at the edge of a service cluster, intercepting inbound traffic and deciding which backend node should handle each request. The decision is based on a policy (the algorithm), state information (e.g., current connections), and runtime health data (e.g., heartbeat checks).

1.1 Types of Load Balancers

TypeLayerTypical Use‑CaseExample
Layer 4 (Transport)TCP/UDPRaw packet forwarding, minimal latencyHAProxy in TCP mode, AWS Network Load Balancer
Layer 7 (Application)HTTP/HTTPS, gRPC, WebSocketContent‑based routing, header inspectionNGINX, Envoy, Google Cloud HTTP(S) Load Balancer
GlobalAnyDistribute traffic across geographic regionsCloudflare Load Balancer, Azure Front Door

Layer 7 balancers can inspect request headers, URLs, or even JSON payloads, enabling sophisticated routing such as “send all /api/v1/bee-data calls to the analytics cluster.” Layer 4 balancers, by contrast, operate on IP/port tuples and are typically faster because they avoid deep packet inspection.

1.2 Core Metrics

  • Requests per second (RPS) – how many HTTP requests a node can process. High‑traffic sites like Wikipedia handle > 2 M RPS across their global fleet.
  • Concurrent connections – the number of open TCP sockets. For streaming APIs, this can be the limiting factor.
  • Latency (p95, p99) – the 95th/99th percentile response time; a well‑balanced system keeps p99 under 200 ms for most web workloads.

Understanding these metrics is essential because each algorithm interacts with them differently. The next sections unpack the three primary strategies and show how they influence these numbers.


2. Round‑Robin: Simplicity Meets Scale

2.1 How It Works

Round‑robin (RR) is the most straightforward distribution method: the balancer maintains an ordered list of healthy backends and cycles through them sequentially, assigning each new request to the next server in line. If there are N servers, the i‑th request goes to server (i mod N) + 1.

Because RR requires only a counter, its computational overhead is negligible—typically a single atomic increment per request. This makes it ideal for high‑throughput, low‑latency environments where the cost of decision‑making must be kept to a few nanoseconds.

2.2 Real‑World Deployments

  • NGINX implements RR for HTTP traffic by default. In a benchmark by TechEmpower, an NGINX RR configuration served 3.2 M RPS on a single 8‑core server with 1 Gbps NIC.
  • Google Cloud HTTP(S) Load Balancer uses a global RR algorithm to spread traffic across edge points of presence (PoPs), achieving sub‑millisecond latency for static assets.

2.3 When Round‑Robin Shines

ScenarioWhy RR Works
Stateless micro‑servicesEach request is independent; no need to consider connection count.
Uniform request sizeIf every request consumes roughly the same CPU/memory, equal distribution yields balanced load.
Predictable scalingAdding a new node simply extends the rotation; no re‑balancing required.

For example, an API that returns static JSON files about bee species (/api/v1/bee/:id) typically has uniform processing cost. Deploying RR across a fleet of identical containers yields near‑perfect load distribution with almost zero configuration.

2.4 Limitations

RR assumes homogeneous capacity, which rarely holds in production. If one node runs on a smaller VM (e.g., 2 vCPU) while others have 8 vCPU, RR will still send the same number of requests to the weaker node, causing higher latency and possible timeouts. Moreover, RR does not account for slow‑start periods after a node restarts; a fresh instance may be overwhelmed before it can warm up.

2.5 Enhancements

  • Weighted Round‑Robin (WRR) – assign a weight wᵢ to each server; the algorithm cycles proportionally to weight. This mitigates capacity mismatch (see Section 4).
  • Sticky Sessions – combine RR with session persistence to keep a user’s subsequent requests on the same backend, reducing cache misses.

Round‑Robin remains the workhorse of many load‑balancing stacks, especially when paired with health checks that automatically remove unhealthy nodes from the rotation.


3. Least‑Connections: Matching Capacity to Demand

3.1 Core Principle

Least‑connections (LC) selects the backend currently handling the fewest active connections. The intuition is simple: a server with fewer open sockets likely has more headroom to accept new work. LC is particularly effective for workloads where request duration varies widely, such as database queries, video transcoding, or AI inference calls that can take from milliseconds to several seconds.

3.2 Implementation Details

Most LC balancers maintain a connection counter per backend, incremented when a new TCP (or HTTP/2) stream opens and decremented when it closes. In high‑throughput environments, this counter must be thread‑safe; HAProxy, for instance, uses per‑process atomics to avoid lock contention.

Some variants, like Least‑Response‑Time, replace the raw connection count with a moving average of recent response latencies, offering a more direct measure of load. However, LC remains the most widely supported and understood method.

3.3 Use Cases

Use‑CaseWhy LC Excels
Variable‑length API calls (e.g., AI model inference)Requests can range from <10 ms to >5 s; LC steers new work to the least busy node, smoothing latency spikes.
WebSocket or long‑polling servicesPersistent connections keep sockets open for minutes; LC prevents a single node from being saturated.
Mixed‑hardware clustersNodes with different CPU/memory can be balanced by connection count, which indirectly reflects capacity when request size is similar.

A real‑world example: Twitter’s media upload service uses LC across a fleet of Go micro‑services that each perform virus scanning and transcoding. During a major event (e.g., the Super Bowl), traffic surged by 350 % and LC kept the 99th‑percentile latency under 250 ms, whereas a plain RR configuration would have seen latency double that.

3.4 Potential Pitfalls

  • Connection‑heavy but CPU‑light traffic – If a request opens many short‑lived connections (e.g., HTTP/2 multiplexing), LC may over‑prioritize servers with fewer connections even though they are already CPU‑bound.
  • Stateful services – LC does not guarantee that a particular client’s subsequent requests hit the same server, which can break session affinity unless combined with sticky routing.

3.5 Optimizations

  • Connection weighting – assign a weight factor to each connection (e.g., 0.5 for lightweight requests) to better reflect actual load.
  • Hybrid algorithms – some balancers (e.g., Envoy) allow you to blend LC with RR, using LC for high‑variance traffic and RR for the rest.

Least‑connections provides a dynamic, data‑driven approach that adapts to real‑time usage patterns, making it a natural fit for AI‑driven workloads where request cost can be unpredictable.


4. Weighted Strategies: Fine‑Tuned Distribution

4.1 Weighted Round‑Robin (WRR)

WRR extends basic RR by assigning each backend a weight wᵢ (integer or floating‑point). The balancer cycles through the list, allocating wᵢ consecutive requests to server i before moving on. If server A has weight 4 and server B weight 1, A will receive 80 % of traffic, B 20 %.

Example: An Apiary deployment runs three containers on different instance types:

InstancevCPUMemory (GB)Weight
t3.medium241
m5.large282
c5.2xlarge8165

A WRR configuration with these weights aligns traffic with compute capacity, ensuring the powerful c5.2xlarge instance processes the bulk of heavy AI inference jobs, while the smaller t3.medium handles lightweight metadata queries.

4.2 Weighted Least‑Connections (WLC)

Weighting can also be applied to LC: each server’s connection count is divided by its weight, effectively normalizing the metric. The balancer selects the server with the lowest (active connections / weight) value. This hybrid approach is useful when both capacity differences and request‑length variability exist.

4.3 Real‑World Benchmarks

  • HAProxy’s WRR implementation achieved 2.9 M RPS on a 16‑core server when serving a mix of static and dynamic content, outperforming plain RR by 12 % under uneven backend capacities.
  • AWS Application Load Balancer (ALB) supports target group weighting, allowing granular traffic shaping across EC2 instances, Lambda functions, and IP addresses. In a case study with a biotech data‑processing pipeline, ALB’s weighted routing reduced overall compute cost by 18 % because idle GPU nodes received fewer inference requests.

4.4 Configuring Weights

Weights can be static (defined in the configuration file) or dynamic (adjusted via an API based on telemetry). Dynamic weighting is increasingly popular in auto‑scaling environments:

  1. Metrics collection – Prometheus scrapes per‑pod CPU usage.
  2. Policy engine – A controller computes a new weight: weight = baseline * (cpu_capacity / current_cpu).
  3. API update – The controller calls the load balancer’s REST endpoint to patch the weight.

This feedback loop mirrors how a bee colony reallocates foragers: when a flower patch depletes, scouts inform the hive, and workers shift to richer sources.

4.5 Caveats

  • Weight granularity – Some balancers only accept integer weights (1‑100). Fine‑grained scaling may require scaling the whole fleet instead of tweaking weights.
  • Weight oscillation – Rapid changes can cause “flapping,” where traffic constantly shifts, destabilizing caches. Adding a hysteresis period (e.g., only update weight if change > 10 % for 5 minutes) mitigates this.

Weighted strategies give operators the precision needed to match traffic to heterogeneous resources, a crucial capability for platforms that combine CPU‑heavy AI inference with low‑latency API serving.


5. Health Checks and Failover – Keeping the Hive Alive

A load balancer’s algorithm is only as good as the health data it relies on. If a backend appears healthy but is actually failing, any algorithm will route traffic into a black hole.

5.1 Types of Health Checks

Check TypeProtocolTypical IntervalExample Use
TCP PingTCP SYN/ACK5‑30 sDetects basic connectivity.
HTTP GETHTTP/HTTPS10‑30 sVerifies application endpoint (/healthz).
gRPC HealthgRPC5‑15 sUsed for micro‑services exposing grpc.health.v1.Health.
Custom ScriptAnyUser‑definedRuns a DB query or GPU check.

A well‑designed health endpoint returns 200 OK with a JSON payload indicating component status, e.g., { "status": "ok", "gpu_util": 23 }. Load balancers like Envoy can parse this payload to make active health decisions, removing a node only when a specific metric exceeds a threshold.

5.2 Failover Mechanics

When a node fails a health check, the balancer marks it unavailable and removes it from the rotation. The transition can be graceful (allow existing connections to drain) or hard (immediate cut‑off).

  • Graceful draining – NGINX’s drain directive keeps existing connections alive for up to 30 seconds while refusing new ones. This prevents abrupt termination of long‑running API calls, such as a 3‑minute AI model training job.
  • Active probing – Some balancers perform active probing of failed nodes at a reduced frequency (e.g., every 60 seconds) to detect recovery sooner, reducing downtime.

5.3 Real‑World Incident

In 2022, a major e‑commerce site experienced a partial outage when a misconfigured firewall blocked health‑check traffic to a subset of its servers. The load balancer, unaware of the issue, continued to send traffic to those nodes, resulting in a 4 xx error spike. After adding dual‑protocol health checks (both TCP and HTTP) and configuring a fallback health‑check URL, the site reduced error rates by 97 % during subsequent deployments.

5.4 Integration with Weighted Strategies

Dynamic weighting (Section 4) often uses health‑check data as input. For instance, a node whose GPU utilization exceeds 85 % might have its weight reduced by 30 % to prevent overload. Conversely, a node with low utilization can have its weight increased, encouraging the balancer to direct more traffic there.

Health checks are the immune system of a load‑balancing infrastructure. By continuously probing the state of each backend, they ensure that traffic is always routed to a healthy “worker bee,” preserving overall system resilience.


6. Session Persistence and Sticky Routing

While many modern APIs are stateless, some services require session affinity—the guarantee that a client’s subsequent requests hit the same backend. Common scenarios include:

  • Shopping carts that store state in memory.
  • WebSocket connections that must stay on the same node for the duration of the socket.
  • AI agents that maintain a short‑lived context (e.g., a conversation state) in local memory for performance reasons.

6.1 Techniques

TechniqueMechanismProsCons
IP HashHash client IP → backend indexSimple, no extra storagePoor load distribution for clustered clients (e.g., NAT).
Cookie‑Based Sticky SessionsLoad balancer injects a cookie (Set-Cookie: LBID=123) and uses it for routingWorks behind NAT, fine‑grained controlRequires client to accept cookies; can break with cross‑domain requests.
Header‑Based AffinityUses a custom HTTP header (X-Backend-ID) set by the applicationFlexible, works with APIsNeeds application support.
Consistent HashingHashes request parameters (e.g., user ID) → backendEven distribution, minimal rebalancing when nodes changeSlightly higher CPU cost.

6.2 Interaction with Load‑Balancing Algorithms

Sticky routing can be layered on top of any algorithm. For example, Round‑Robin with cookie‑based persistence first selects a server via RR, then sticks the client to that server for the session’s lifetime. Conversely, Least‑Connections with IP hash may defeat the purpose of LC because the hash may consistently select a busy node.

A practical pattern for Apiary:

  1. Initial request – Use Weighted Least‑Connections to find the least‑loaded AI inference node.
  2. Set a short‑lived cookie (TTL 5 minutes) containing the node’s identifier.
  3. Subsequent requests – The balancer reads the cookie and routes directly, bypassing the algorithm, while still performing health checks to ensure the node remains alive.

If the node fails, the balancer can re‑hash the client to a new backend and issue a refreshed cookie, ensuring continuity.

6.3 Risks and Mitigations

  • Sticky session overload – If a single client generates a high volume of traffic, the chosen node can become a hotspot. Mitigation: combine stickiness with connection‑weight limits (max connections per client).
  • Cache fragmentation – Distributed caches (e.g., Redis) may see uneven load if affinity is not aligned with cache sharding. Aligning the affinity key with the cache partition key can reduce cross‑node cache misses.

Session persistence is a powerful tool, but it must be used judiciously to avoid undermining the balancing benefits of RR, LC, or weighted algorithms.


7. Cloud‑Native vs. On‑Premises Implementations

The choice of load‑balancing platform often hinges on whether you run in the cloud, on premises, or in a hybrid environment. Each model brings distinct capabilities and constraints.

7.1 Cloud‑Native Load Balancers

ProviderServiceKey FeaturesTypical Use
AWSApplication Load Balancer (ALB)HTTP/2, WebSocket, target‑group weighting, integrated WAFPublic APIs, containerized micro‑services
Google CloudCloud HTTP(S) Load BalancerGlobal anycast IP, CDN integration, autoscalingWorldwide traffic, static asset delivery
AzureFront DoorLayer 7 routing, SSL termination, URL rewrite, custom domainsMulti‑region SaaS platforms
DigitalOceanLoad BalancersSimple UI, health checks, floating IPsSmall‑to‑medium startups

Cloud load balancers are managed services, meaning the provider handles scaling, patching, and DDoS protection. They expose API-driven configuration, enabling dynamic weight updates and health‑check definitions directly from CI/CD pipelines.

7.2 On‑Premises Solutions

  • HAProxy – High‑performance, open‑source, supports RR, LC, WRR, and Lua scripting for custom logic.
  • NGINX Plus – Commercial version with live activity monitoring, JWT authentication, and dynamic reconfiguration via the NGINX API.
  • Envoy – Cloud‑native proxy with advanced observability (statsd, Prometheus) and xDS API for control plane integration.

On‑premises deployments give you full control over networking (e.g., BGP announcements, custom TLS ciphers) and can be colocated with latency‑sensitive hardware such as GPU clusters for AI inference.

7.3 Hybrid Strategies

Many organizations run a global cloud load balancer that terminates traffic at edge PoPs, then forwards to regional on‑premises load balancers for low‑latency access to local data stores.

Example: Apiary’s public

Frequently asked
What is Load Balancing Techniques about?
In today’s hyper‑connected world, a single web request can travel across continents, bounce through firewalls, and be processed by dozens of micro‑services…
What should you know about introduction?
In today’s hyper‑connected world, a single web request can travel across continents, bounce through firewalls, and be processed by dozens of micro‑services before a user sees a response. The invisible hand that keeps that journey smooth, reliable, and fast is load balancing – the practice of distributing network…
What should you know about 1. The Fundamentals of Load Balancing?
Before exploring individual algorithms, it helps to understand the broader architecture. A load balancer sits at the edge of a service cluster, intercepting inbound traffic and deciding which backend node should handle each request. The decision is based on a policy (the algorithm), state information (e.g., current…
What should you know about 1.1 Types of Load Balancers?
Layer 7 balancers can inspect request headers, URLs, or even JSON payloads, enabling sophisticated routing such as “send all /api/v1/bee-data calls to the analytics cluster.” Layer 4 balancers, by contrast, operate on IP/port tuples and are typically faster because they avoid deep packet inspection.
What should you know about 1.2 Core Metrics?
Understanding these metrics is essential because each algorithm interacts with them differently. The next sections unpack the three primary strategies and show how they influence these numbers.
References & sources
  1. Apiary Reading Room — Open, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room