API Gateway
Single entry point for all your microservices. Authentication, routing, rate limiting in one place.
1The Hotel Concierge Analogy
2What is an API Gateway?
Architecture Overview
3Key Responsibilities
Verify JWT tokens, API keys, OAuth. Reject unauthorized requests before they reach services.
Check JWT → Valid? Route to service : Return 401Prevent abuse. Limit requests per user/IP. Protect backend from traffic spikes.
100 requests/min per user. Exceeds? Return 429.Route /users/* to User Service, /orders/* to Order Service based on path, headers, etc.
/api/v2/users → user-service:3000/usersCentral place to log all requests, measure latency, track errors across all services.
Log: request_id, user, endpoint, latency, statusAccept REST from clients, convert to gRPC for internal services. Or GraphQL to REST.
Client: REST → Gateway → gRPC to servicesCombine responses from multiple services into one. Reduce client round trips.
/dashboard = user info + recent orders + notificationsHandle HTTPS at gateway. Backend services can use simple HTTP internally.
Client ←HTTPS→ Gateway ←HTTP→ ServicesCache frequent responses. Reduce load on backend services.
GET /products/popular → cached for 5 mins4Request Flow
Step-by-Step Request Flow
5Popular API Gateways
| Gateway | Type | Best For | Notes |
|---|---|---|---|
| Kong | Open Source | General purpose, plugins | Built on Nginx, Lua plugins |
| AWS API Gateway | Managed | AWS ecosystem | Lambda integration, pay-per-request |
| Nginx | Open Source | High performance | Simple routing, need custom auth |
| Envoy | Open Source | Service mesh, Kubernetes | Used by Istio, advanced features |
| Traefik | Open Source | Kubernetes, Docker | Auto-discovery, Let's Encrypt |
6Pros & Cons
Advantages
- ✓ Single point for cross-cutting concerns
- ✓ Simplifies client code (one endpoint)
- ✓ Hide internal service structure
- ✓ Central place for monitoring/logging
- ✓ Easy to add new services
Disadvantages
- ✗ Single point of failure (need HA)
- ✗ Added latency (extra hop)
- ✗ Can become bottleneck
- ✗ Additional complexity to manage
- ✗ Risk of becoming "god service"
Keep your API Gateway thin. It should route and apply policies, NOT contain business logic. If you're writing custom code in your gateway for specific endpoints, that logic probably belongs in a service.