Here’s a FAANG-level deep dive into Layer 4 (L4) and Layer 7 (L7) Load Balancers, covering:
OSI layer concepts
Real-world usage
Design trade-offs
Use cases in distributed systems
Interview-level comparisons
Visual diagrams
Debugging & metrics
Questions for interviews
🔹1. OSI Context & Basics
🔹2. Layer 4 Load Balancer (L4)
🧠Concept:
Operates at the transport layer (TCP/UDP). It forwards packets without parsing application data.
🔧 Mechanism:
Load balancing happens based on:
Source IP
Destination IP
Port
Protocol (TCP/UDP)
Uses techniques like round-robin, least connections, or IP hash.
✅ Pros:
Fast (packet-level routing, minimal overhead)
Protocol-agnostic (works for non-HTTP traffic like FTP, SMTP)
Efficient for backend services like databases, gRPC, etc.
❌ Cons:
No application-level routing
Cannot do SSL termination or cookie-based session stickiness
📦 Example Tools:
Linux IPVS
AWS NLB (Network Load Balancer)
HAProxy (in TCP mode)
Envoy (L4 config)
🧪 FAANG Use Case:
Database proxy balancing between primary/replica nodes using IP-based routing for high throughput.
🔹3. Layer 7 Load Balancer (L7)
🧠Concept:
Operates at the application layer. Understands HTTP headers, paths, cookies, etc.
🔧 Mechanism:
Reads HTTP headers, query params, and content
Makes smart decisions like:
/api/* → Microservice A
/static/* → CDN/Bucket
Handles SSL termination, compression, and caching
✅ Pros:
Fine-grained routing
Better observability (status codes, user agents)
Can enforce security (e.g., Web Application Firewall)
❌ Cons:
Higher CPU/memory usage (parsing full payloads)
Slightly higher latency than L4
📦 Example Tools:
NGINX (HTTP reverse proxy)
Envoy Proxy (in L7 mode)
AWS ALB (Application Load Balancer)
GCP HTTP(S) Load Balancer
Istio Ingress Gateway
🧪 FAANG Use Case:
Instagram service routes:
/feed → FeedService
/story → StoryService
/media/* → Media CDN
🔹4. Visual Comparison
🔹5. When to Use What?
🔹6. FAANG-Level Interview Questions
🔸 Conceptual
How does a Layer 4 Load Balancer differ from Layer 7 in TLS termination?
Which one would you choose to route based on a cookie? Why?
Explain L4 and L7 behavior during a long-polling WebSocket connection.
What happens if the backend server responds with a 5xx? How does L4 vs L7 handle retries?
What are the limitations of a L7 LB in Kubernetes Ingress?
🔸 Design/Scenario-Based
Design an HAProxy-based solution for 1000 HTTP microservices.
You observe latency spikes in your L7 load balancer. Walk through debugging steps.
In your system, some requests are non-idempotent. Should the LB retry them?
Route traffic between old and new app versions (canary deployment): L4 or L7?
🔹7. Debugging Metrics & Tools
🔹8. Advanced Concepts
🔹9. Summary Table
🔹10. Bonus: Service Mesh View
Service meshes like Istio or Linkerd use L4 sidecars and L7 proxies for complete routing and policy control:
Want Hands-On? 🧪
You can simulate both L4 & L7 locally:
L4: TCP Proxy with HAProxy
L7: HTTP Routing with NGINX
Can Nginx be used as L4 Loadbalancer
NGINX can be used as a Layer 4 Load Balancer, but only NGINX Plus supports native L4 (TCP/UDP) load balancing fully.
Open Source NGINX (OSS) has some L4 capabilities but they are limited.
🔹 Layer 4 Support in NGINX
🔹 L4 Load Balancing in NGINX OSS
NGINX OSS uses the stream module to support basic L4 proxying for TCP/UDP.
🔸 Example: TCP Load Balancing (e.g., MySQL)
🔧 You need to enable the stream module in OSS if it's not compiled.
🔹 Limitations in Open Source NGINX
❌ No active TCP/UDP health checks
❌ No session affinity at L4
❌ No traffic splitting or weighted backends
❌ No observability metrics per TCP/UDP connection
❌ No automatic failover — just round-robin
🔹 NGINX Plus for Full L4 LB
NGINX Plus adds:
✅ Native health checks for TCP/UDP
✅ Live activity monitoring dashboard
✅ Session persistence via IP
✅ Fine-grained control on load balancing algorithms
Example with NGINX Plus:
🔹 Use Cases for NGINX L4 LB
🔹 NGINX as L4 in Kubernetes?
Ingress: NGINX Ingress Controller (official) works at L7 (HTTP).
For L4, you'd use kube-proxy, MetalLB, or a sidecar like Envoy instead.
However, NGINX can run in a pod with stream config to act as a L4 proxy in-cluster.
🔹 Summary
✅ Recommendation
Use NGINX OSS for simple TCP load balancing.
Use NGINX Plus if you need:
Active health checks
Monitoring
Advanced failover & session handling
Use Envoy or HAProxy if you want powerful open-source L4/L7 hybrid alternatives.