Tutorials System Design Mastery

Load Balancers: Layer 4 vs Layer 7 (Nginx vs HAProxy)

On this page

Mastering Load Balancers

The Load Balancer is the gatekeeper of your system. It sits between the user and your servers, distributing traffic to ensure no single server is overwhelmed.

1. Layer 4 Load Balancing (Network Layer)

Uses only the IP address and Port. It doesn't look at the packet content. Pros: Insanely fast, low CPU usage. Cons: No "Smart" routing (can't send /api to one server and /images to another).

2. Layer 7 Load Balancing (Application Layer)

Looks at the HTTP headers, cookies, and URL. Pros: Super smart—can handle SSL Termination, Session Persistence (Sticky Sessions), and Path-based routing. Cons: Higher CPU usage because it has to decrypt and parse the HTTP packet.

4. Interview Mastery

Q: "What are 'Sticky Sessions' and why are they an anti-pattern?"

Architect Answer: "Sticky Sessions force a specific user to always talk to the same backend server. This is usually used to 'fix' legacy apps that store user data in the server's local RAM. It's an anti-pattern because it makes **Auto-scaling** and **Failover** extremely difficult. If that one server dies, the user's session is lost even if there are 100 other healthy servers. A modern architect should always aim for a **Stateless** backend where any server can handle any request."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

System Design Mastery
Course syllabus
1. Distributed Systems Fundamentals
2. Database Scalability
3. Caching & CDN Strategies
4. Event-Driven Architecture
5. High Availability & Load Balancing
6. Microservices & API Gateway
7. Monitoring & Disaster Recovery
8. FAANG System Design Interview
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details