DrawCode Algorithm Visualizer • Load Balancing • Easy

Least Connections Algorithm

Tags: Load Balancing, Networking, System Design, Concurrency, Server

1. One-Liner

The Least Connections algorithm routes each new request to the backend server that currently has the fewest active connections.


2. The Problem It Solves

You have multiple backend servers behind a load balancer. If you use Round Robin (send requests in order: Server 1, 2, 3, 1, 2, 3...), it seems fair. But what if:

  • Server 1 is processing a heavy database query (takes 10 seconds).
  • Server 2 finished its request in 50ms and is now idle.
  • Round Robin still sends the next request to Server 1, which is already overloaded!
  • The problem: Not all requests take the same time. Some are fast, some are slow. Round Robin ignores this completely.

    Least Connections fixes this by always picking the server that is least busy right now.


    3. The Core Idea

    Imagine you’re at a supermarket with 4 checkout counters. Each counter has a line of customers:

  • Counter 1: 5 people waiting
  • Counter 2: 2 people waiting
  • Counter 3: 7 people waiting
  • Counter 4: 1 person waiting
  • Which counter do you go to? Obviously Counter 4 — the one with the shortest line.

    That’s exactly what Least Connections does. The "line" is the number of active connections (requests being processed). The load balancer always sends you to the server with the shortest line.


    4. How It Works (Step-by-Step)

    StepWhat Happens
    1. Request arrivesA new request comes to the load balancer
    2. LockAcquire a lock (thread safety in concurrent systems)
    3. Find minimumScan all servers, find the one with the fewest active connections
    4. Break tiesIf multiple servers are tied, pick the first one (or random)
    5. IncrementAdd 1 to that server’s active connection count
    6. RouteForward the request to the chosen server
    7. On completionWhen the request finishes, decrement that server’s count by 1

    Key insight: The count goes UP when a request is routed, and DOWN when processing finishes. This means slow servers naturally accumulate more connections and get fewer new requests.


    5. Dry Run Example

    Setup: 3 servers, all start at 0 connections

    Request #S1 ConnsS2 ConnsS3 ConnsRouted ToWhy
    1000S1All tied, pick first
    2100S2S2 has min (0)
    3110S3S3 has min (0)
    4111S1All tied, pick first
    S2 finishes request #2
    5201S2S2 has min (0)
    S1 finishes request #1
    6111S1All tied again

    Notice how when S2 finishes early, it immediately gets the next request. Slow servers naturally get less traffic.


    6. Key Properties

    PropertyValue
    Adapts to server speed?Yes — slow servers get fewer requests
    Handles unequal hardware?Yes — faster machines finish quicker, get more work
    Needs connection tracking?Yes — must track active count per server
    Thread-safe?Needs locking (mutex) for concurrent access
    Weighted variant?Yes — Weighted Least Connections adds server capacity factors

    7. Where It Is Used

    Company / SystemHow They Use It
    Nginxleast_conn directive in upstream blocks
    HAProxybalance leastconn in backend config
    AWS ALBLeast Outstanding Requests (similar concept)
    Envoy ProxyBuilt-in least request load balancing
    F5 BIG-IPLeast Connections as default LB method
    KubernetesAvailable in service mesh (Istio, Linkerd)

    8. Interview Tips

    What interviewers want to hear:

    1. You understand the weakness of Round Robin (ignores processing time differences)

    2. You can explain with the supermarket checkout analogy

    3. You know it needs connection tracking (counter per server)

    4. You can discuss thread safety — what if two requests arrive at the exact same time?

    5. You know about Weighted Least Connections for heterogeneous servers

    Common follow-up questions:

  • "When would Round Robin be better?" → When all requests take roughly the same time and servers are identical. Simpler, no counter tracking needed.
  • "What’s Weighted Least Connections?" → Each server has a weight (e.g., based on CPU cores). The formula becomes connections / weight. A powerful server with weight 4 and 8 connections is preferred over a weak server with weight 1 and 3 connections.
  • "How do you handle server failure?" → Health checks. Remove failed servers from the pool. Their connections will eventually time out and decrement.
  • "What about session stickiness?" → Use Least Connections for initial routing, then use cookies/hashing for subsequent requests from the same user.

  • 9. Comparison with Other Algorithms

    AlgorithmAdapts to Load?ComplexityNeeds Tracking?Best For
    Least Connections✅ YesO(n)Yes — active countVarying request durations
    Round Robin❌ NoO(1)NoUniform requests, identical servers
    Weighted Round Robin⚠️ PartialO(1)NoDifferent server capacities
    IP Hash❌ NoO(1)NoSession affinity
    Random❌ NoO(1)NoSimple, stateless
    Least Response Time✅ YesO(n)Yes — latencyLatency-sensitive workloads

    10. Complexity

    MetricValue
    Time ComplexityO(n) per request — where n = number of servers
    Space ComplexityO(n) — one counter per server
    Connection UpdateO(1) — increment/decrement

    Implementation Example (PYTHON)

    import threading
    
    class LeastConnectionLB:
        def __init__(self, servers):
            self.servers = servers
            self.connections = {s: 0 for s in servers}
            self.lock = threading.Lock()
    
        def get_server(self):
            with self.lock:
                min_conn = min(
                    self.connections.values())
                candidates = [
                    s for s, c in self.connections.items()
                    if c == min_conn]
                server = candidates[0]
                self.connections[server] += 1
                return server
    
        def release(self, server):
            with self.lock:
                self.connections[server] -= 1
                self.connections[server] = max(
                    0, self.connections[server])

    Interactive Visualizer Workspace

    Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

    Launch Interactive Visualizer