📡 You're offline — showing cached content
New version available!
Quick Access
Python Intermediate

Load Balancing Algorithms: Round Robin, Least Connections, IP Hash

Master load balancing — round robin, weighted round robin, least connections, IP hash sticky sessions, and L4 vs L7 load balancers.

EzyCoders Admin November 20, 2025 11 min read 0 views
Load Balancing Algorithms Complete Guide
Share: Twitter LinkedIn WhatsApp

Load Balancing Algorithms

A load balancer distributes requests across servers. The algorithm choice affects performance, fairness, and server utilization.

from itertools import cycle

class RoundRobin:
    def __init__(self, servers):
        self.pool = cycle(servers)
    def get(self): return next(self.pool)

class WeightedRoundRobin:
    def __init__(self, servers):
        expanded = [s for s, w in servers for _ in range(w)]
        self.pool = cycle(expanded)
    def get(self): return next(self.pool)

# WeightedRoundRobin([('s1',3),('s2',1)]):
# s1 gets 75%, s2 gets 25% of requests

Least Connections

import threading

class LeastConnections:
    def __init__(self, servers):
        self.conns = {s: 0 for s in servers}
        self.lock  = threading.Lock()

    def get(self):
        with self.lock:
            return min(self.conns, key=self.conns.get)

    def opened(self, s):
        with self.lock: self.conns[s] += 1
    def closed(self, s):
        with self.lock: self.conns[s] = max(0, self.conns[s]-1)

IP Hash (Sticky Sessions)

import hashlib

class IPHash:
    def __init__(self, servers):
        self.servers = servers
    def get(self, client_ip: str):
        h = int(hashlib.md5(client_ip.encode()).hexdigest(), 16)
        return self.servers[h % len(self.servers)]

Q: L4 vs L7 load balancing?

L4 (TCP): routes by IP/port — fast, cannot inspect content. L7 (HTTP): routes by URL, headers, cookies — can route /api to API servers, /static to CDN, supports SSL termination and content-based routing. AWS ALB, Nginx, and HAProxy are L7 balancers.

EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment