Both sit in front of backend services and route traffic, but they work at different levels and solve different problems. Load balancer - Distributes incoming traffic across multiple instances of the same service to improve availability and capacity. - Layer 4 (transport) load balancers route TCP or UDP connections based on IP and port (Azure Load Balancer, AWS Network Load Balancer). - Layer 7 (application) load balancers understand HTTP and can route by host, path or headers, terminate TLS and handle sticky sessions (Azure Application Gateway, AWS Application Load Balancer, NGINX, HAProxy). - Typical features: health checks (remove unhealthy instances), algorithms (round robin, least connections, IP hash), TLS termination, connection draining. - Focus: spreading load and ensuring the service stays available. API gateway - A single entry point for clients to a set of APIs, often microservices, that applies API management features. - Typical features: - Request routing to different services (/orders to the Orders service, /payments to the Payments service). - Authentication and authorization (validate JWTs, API keys, OAuth scopes). - Rate limiting, quotas and throttling per client or subscription. - Request and response transformation, protocol translation, API versioning. - Response caching, request aggregation (combining several backend calls), and Backend-for-Frontend patterns. - Analytics, logging, monitoring and developer portals with API documentation and keys. - Examples: Azure API Management, AWS API Gateway, Kong, Apigee, Ocelot or YARP for .NET, Spring Cloud Gateway. - Focus: managing and securing APIs and their consumers. Key differences - Purpose: a load balancer distributes traffic for availability and scale; an API gateway manages, secures and routes API calls. - Awareness: load balancers know little about the API itself; gateways understand API concepts (consumers, keys, scopes, versions). - Routing targets: a load balancer typically spreads requests across identical instances; a gateway routes to different services based on the request. - Features: security policies, rate limiting, transformation and developer portals are gateway features, not core load-balancer features. How they are used together A common architecture: Client, then a global load balancer or CDN (Azure Front Door), then the API gateway (API Management, often with multiple instances behind a load balancer), then internal load balancing across service instances (Kubernetes Services or an ingress controller), then the microservices. The gateway itself must be load-balanced for high availability. Overlap Modern Layer 7 load balancers and ingress controllers can do some gateway tasks (path routing, TLS, basic rate limiting), and gateways often include load balancing. Choose based on the primary need: traffic distribution or API management. Interview summary: "A load balancer spreads traffic across instances for availability; an API gateway is the managed front door for APIs, handling routing between services, authentication, rate limiting, transformations and analytics. In production you usually use both."
Ravindra Wadile · · 0 likes