Load Balancing Algorithms
| Item | Description |
Round Robin | Distributes requests sequentially across all servers — simplest |
Weighted Round Robin | Assign weights to servers with different capacities |
Least Connections | Sends to server with fewest active connections (sticky workloads) |
Weighted Least Conn | Least connections + server weight (best for mixed hardware) |
IP Hash | Hash client IP → same server every time (session persistence without cookies) |
Least Response Time | Sends to server with lowest latency (NGINX Plus / HAProxy) |
Random | Pure random distribution — surprisingly effective at scale |
URL Hash | Hash the request URL — good for caching proxies |
Layer 4 vs Layer 7
| Feature | Layer 4 (Transport) | Layer 7 (Application) |
| Works at | TCP/UDP level | HTTP/HTTPS level |
| Routing decisions | IP + port only | URL path, headers, cookies, method |
| SSL termination | Passthrough (or raw TCP) | Terminate/re-encrypt (inspect traffic) |
| Performance | Higher throughput, lower latency | More CPU, more features |
| Content switching | No | Yes (route /api to backend A, /static to B) |
| Protocol support | Any TCP/UDP | HTTP/HTTPS only |
Health Checks
| Item | Description |
TCP check | Can the port be reached? Simple, fast |
HTTP check | GET /health returns 200? More reliable |
Script check | Run custom script — most flexible |
Active checks | Load balancer probes servers (default) |
Passive checks | Monitor actual responses for errors (HAProxy observe) |
Unhealthy threshold | How many consecutive failures before marking DOWN |
Session Persistence (Sticky Sessions)
| Item | Description |
Cookie-based | LB inserts cookie; subsequent requests route to same backend |
IP-based | Same client IP → same server (IP hash algorithm) |
SSL Session ID | Stick to server based on TLS session ID |
Trade-off | Persistence breaks even distribution — use only when necessary |
Pro Tip: Layer 7 gives you content-based routing (URL, headers, cookies). Layer 4 is faster and protocol-agnostic. Start simple — round robin works fine for stateless apps.