System design · Question 43 of 50
What does a load balancer do? Layer 4 or layer 7, and which algorithms?
The basics of scaling out and staying up.
The answer
A load balancer spreads requests across several servers so none is overwhelmed, and stops sending to servers that fail their health checks, so one crash does not take the site down. Layer 4 balances TCP or UDP connections by IP and port: fast, and blind to the content. Layer 7 reads HTTP: it can route by path or header (/api to one pool, /images to another), end TLS, and set cookies.
Algorithms: round robin (in turn), least connections (good when requests vary in length), weighted (bigger servers get more), and hashing on the client IP or user id (the same user lands on the same server). Prefer stateless servers with sessions in Redis over sticky sessions, and make the balancer itself redundant.
The picture
┌──▶ server 1 ✓
users ─▶ [ LB ] ─────┼──▶ server 2 ✓
health checks └──▶ server 3 ✗ taken out
L4: by IP and port L7: by URL, header, cookieWhat are sticky sessions, and why avoid them?
Show the answer
The balancer pins each user to one server. If that server fails, its users are logged out, and the load gets uneven. Keep session state in a shared store instead.