Load Balancer
Master the traffic cop of distributed systems. Learn how to distribute requests across multiple servers for high availability.
π¦ Load Balancer: The Traffic Cop
A Load Balancer is a device (or software) that acts as a reverse proxy and distributes network or application traffic across a number of servers.
π‘ The Logic (ELI5)
Think of a Busy Restaurant:
- You have 10 Chefs in the kitchen.
- If every customer just yells their order at the same Chef, that Chef will crash!
- The Load Balancer is the Head Waiter standing at the door.
- They take the order and give it to the Chef who is currently the least busy.
- This ensures the kitchen stays fast and no single Chef is overwhelmed.
π The Deep Dive
Where does it sit?
Load Balancers typically sit between the User and the Web Server, or between the Web Server and the Database/API.
Common Algorithms
- Round Robin: Go in order (1, 2, 3, 1, 2, 3...). Simple but doesn't care about server load.
- Least Connections: Send the request to the server with the fewest active sessions. Best for long-running tasks.
- IP Hashing: Hash the user's IP address to decide which server to use. Ensures a user always goes to the same server (Sticky Sessions).
Health Checks
Load Balancers don't just send traffic blindly. They perform "Health Checks." If a server doesn't respond, the Load Balancer marks it as "Offline" and stops sending it traffic until it's healthy again.
π― Interview Pulse
Hardware vs Software
- Hardware: Expensive, dedicated boxes (e.g., F5). Used by massive data centers.
- Software: Flexible, cloud-native (e.g., NGINX, HAProxy, AWS ELB).
Layer 4 vs Layer 7
- L4 (Transport Layer): Distributes traffic based on IP and Port. Very fast (doesn't look at the data).
- L7 (Application Layer): Distributes traffic based on URL content, Cookies, or Headers. "Smarter" but slower.
Single Point of Failure
Wait! If the Load Balancer dies, the whole site goes down. The Solution: Use a pair of Load Balancers in an "Active-Passive" setup. If the primary one fails, the secondary one takes over instantly. βοΈ