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.
Comments (0)
No comments yet. Be the first!
Leave a Comment