Sub topic 2
Choosing a Balancing Algorithm
Assume you understand HTTP, TCP, IP addresses, ports, and basic configuration files. A load balancer selects a backend server for each client connection or request. The algorithm should match request duration, server capacity, and session behavior.
Choosing a balancing algorithm
| Algorithm | How it chooses | Good fit |
|---|---|---|
| Round robin | Cycles through servers | Similar servers and request costs |
| Weighted | Gives stronger servers more traffic | Different server capacities |
| Least connections | Chooses the server with fewest active connections | Long-lived or uneven requests |
| Hash-based | Maps a client or key to a server | Cache locality or controlled affinity |
Round robin is simple, but it can overload a server when requests take different amounts of time. Least connections reacts to current load, although connection count is only an approximation of real work. Hash-based selection can preserve locality, but backend changes can move many clients unless consistent hashing is used.
import itertools
servers = ["a", "b", "c"]
rotation = itertools.cycle(servers)
print([next(rotation) for _ in range(5)])
print(min({"a": 2, "b": 0, "c": 5}, key={"a": 2, "b": 0, "c": 5}.get))Health checks: keeping traffic off failed backends
An active health check sends a separate probe, such as GET /healthz, and removes a backend after repeated failures. A passive check learns from real connection or response failures. Use thresholds and recovery delays to avoid repeatedly sending traffic to a flapping server. A health endpoint should test dependencies that the application needs, but it should not perform expensive work.
Session persistence and when to avoid it
Session persistence, or sticky sessions, tries to send a client to the same backend. A load balancer can use a cookie, source IP, or a hash of a request value. Cookies are usually more reliable than source IP because many users can share one public address.
Prefer stateless applications with shared session storage when possible. Stickiness reduces balancing flexibility and can overload one backend. It also complicates failover because a replacement server may not have the session state.
TLS termination and passthrough
With TLS termination, the load balancer decrypts HTTPS, can inspect HTTP paths, and forwards traffic to backends over HTTP or HTTPS. This supports Layer 7 routing and central certificate management. TLS passthrough keeps encryption end to end, but the load balancer can generally route only using connection-level information such as the destination port or TLS SNI.
upstream app {
least_conn;
server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
location / {
proxy_pass http://app;
}
}Exercise
Which algorithm is a reasonable starting point for two similar servers handling short requests, and what should remove a failed server from rotation?
Show answer
Use round robin. An active or passive health check should mark a server unhealthy after configured failures.