🔁 Load Balancer Fundamentals
Overview
Section titled “Overview”- Prevents single-node overload and removes single points of failure.
- Improves latency by routing to healthy, nearby, or least-loaded instances.
- Enables rolling deploys, blue/green releases, and maintenance without downtime.

Why Use a Load Balancer?
Section titled “Why Use a Load Balancer?”- Scalability: horizontally add or remove compute without impacting clients.
- Availability: continue serving traffic when instances fail.
- Performance: route requests using health, latency, or customized policies.
Core Algorithms ⚙️
Section titled “Core Algorithms ⚙️”| Strategy | Mechanism | Best For | Drawbacks |
|---|---|---|---|
| Round robin | Rotate through backend list | Homogeneous servers | Ignores load differences |
| Weighted round robin | Prefer nodes with higher capacity weights | Mixed instance sizes | Static weights; no live feedback |
| Least connections | Pick node with fewest active sessions | Long-lived connections | Needs accurate connection tracking |
| Least response time | Choose node with fastest current latency | Dynamic response environments | Requires continuous measurements |
| IP hash | Hash client IP to select backend | Session affinity | Can create uneven load distribution |
| Random | Randomly pick servers | Large pools with similar health | No awareness of hot nodes |
| Adaptive/dynamic | External metrics (CPU, queue depth) inform routing | Highly variable workloads | Complex to implement and tune |
Stateful vs. Stateless Balancing 🧭
Section titled “Stateful vs. Stateless Balancing 🧭”
| Type | How It Works | Pros | Cons |
|---|---|---|---|
| Stateful | Balancer tracks session/user state | Seamless affinity | Harder to scale/replicate balancer |
| Stateless | Each request routed independently | Easy to scale horizontally | Needs external store for session data |
Layer 4 vs. Layer 7
Section titled “Layer 4 vs. Layer 7”
🔒 Layer 4 Load Balancing
Section titled “🔒 Layer 4 Load Balancing”- Definition: Operates at the transport layer of the OSI model.
- Function: Directs traffic based on IP addresses and ports.
- Advantages: Simple and fast due to minimal data inspection.
- Disadvantages: Lacks ability to make decisions based on application-specific data.
🌐 Layer 7 Load Balancing
Section titled “🌐 Layer 7 Load Balancing”- Definition: Operates at the application layer of the OSI model.
- Function: Directs traffic based on HTTP headers, cookies, and request content.
- Advantages: Supports sophisticated traffic management such as routing by URLs or user agents.
- Disadvantages: More complex and slower due to deep packet inspection.