Theory
Load Balancer vs. Reverse Proxy vs. API Gateway
📋 Overview
While these three components often overlap in functionality (and are sometimes provided by the same software), they serve distinct architectural purposes. A Load Balancer focuses on Efficiency, a Reverse Proxy on Security/Abstraction, and an API Gateway on Management/Governance of microservices.
🏗️ Core Principles & Characteristics
- Load Balancer: Distributes incoming traffic across a group of backend servers to prevent overload. (e.g., Round Robin, Least Connections).
- Reverse Proxy: Acts as an intermediary for servers. Hides the server's identity, handles SSL, and caches content.
- API Gateway: A specialized reverse proxy that handles "cross-cutting concerns" for microservices: Auth, Rate Limiting, Request Transformation, and Service Discovery.
⚖️ Trade-offs: Pros & Cons
- Load Balancer:
- Pros: Essential for horizontal scaling.
- Cons: Simple; doesn't know about business logic or users.
- Reverse Proxy:
- Pros: Great for security and static content caching.
- Cons: Management overhead for multiple backend routes.
- API Gateway:
- Pros: Simplifies client code; provides a single entry point for complex microservices.
- Cons: Can become a Single Point of Failure and a performance bottleneck if not scaled correctly.
🌍 Real-World Implementation
- Nginx: Can act as both a Reverse Proxy and a basic Load Balancer.
- HAProxy: The gold standard for high-performance L4/L7 Load Balancing.
- Kong / Amazon API Gateway: Used for managing API keys, rate limits, and routing in large-scale microservice environments.
💡 Interview "Gotchas" & Tips
- The Hierarchy: A Load Balancer is a type of Reverse Proxy. An API Gateway is a type of Reverse Proxy.
- Protocol Translation: API Gateways often convert
HTTP/JSONfrom the client intogRPCorAMQP(RabbitMQ) for internal service communication. - Edge Functions: Modern API Gateways can run "Serverless" logic at the edge (e.g., checking a JWT before the request hits any backend).
📐 Suggested Architecture Primitives
- ALB/NLB: Cloud-native load balancing.
- Ingress Controller: The K8s implementation of these patterns.
- Circuit Breaker: Often implemented within the API Gateway or Load Balancer.
- BFF (Backend-for-Frontend): A specific pattern where an API Gateway is built for a specific UI (Mobile vs. Web).
Canvas