Last Updated: January 8, 2026
In the previous chapter, we learned how services find each other through service discovery.
But consider what happens when external clients, like mobile apps or web browsers, need to interact with your microservices. Should a mobile app call a dozen different services to load a single screen? Should every service implement its own authentication, rate limiting, and SSL termination? The answer to both questions is no.
The API Gateway pattern provides a single entry point for all external clients. Instead of exposing your microservices directly, you place a gateway in front of them that handles routing, security, and other cross-cutting concerns.
The gateway simplifies client interactions and centralizes functionality that would otherwise be duplicated across services.
In this chapter, you will learn:
The API Gateway is one of the most common patterns in microservices architectures. Understanding it is essential for designing scalable, secure, and maintainable systems.
Imagine a mobile app for an e-commerce platform. The home screen shows product recommendations, the user's cart, recent orders, and promotional banners. Without an API Gateway, the app would need to call multiple services directly.
This approach creates several problems:
The client makes multiple round trips over the network. Mobile networks are slow and unreliable. Each request adds latency, increases battery drain, and risks failure.
Clients are coupled to the internal service structure. If you split the Order Service into Order and Shipping services, every client must be updated.
Every service must implement authentication, authorization, rate limiting, logging, and SSL. This duplicates code and creates inconsistency.
| Concern | Without Gateway |
|---|---|
| Authentication | Each service validates JWT tokens separately |
| Rate Limiting | Each service tracks its own rate limits |
| SSL Termination | Each service manages certificates |
| Logging | Different formats, different destinations |
| CORS | Configured in each service |
Internal services might use gRPC, AMQP, or other protocols. External clients typically expect REST or GraphQL over HTTPS.
An API Gateway is a server that acts as the single entry point for all client requests. It routes requests to appropriate backend services and handles cross-cutting concerns.
Now the client makes a single request to the gateway, which handles:
The gateway routes incoming requests to the appropriate backend service based on the path, headers, or other criteria.
Routing rules can be based on:
| Routing Criteria | Example |
|---|---|
| Path | /api/v1/products → Product Service |
| HTTP Method | GET /orders → Query Service, POST /orders → Command Service |
| Headers | X-Version: 2 → Product Service v2 |
| Query Parameters | ?beta=true → Beta Service |
| Client Type | Mobile User-Agent → Mobile-optimized endpoints |
The gateway can aggregate multiple backend calls into a single response. This is sometimes called the API Composition pattern.
Benefits of aggregation:
The gateway handles authentication so individual services do not have to.
| Pattern | Description |
|---|---|
| Token Validation | Gateway validates JWT or OAuth tokens |
| Token Exchange | Gateway exchanges external token for internal token |
| Header Propagation | Gateway adds user context headers for downstream services |
| mTLS | Gateway establishes mutual TLS with services |
The gateway protects backend services from being overwhelmed.
| Strategy | Description | Use Case |
|---|---|---|
| Fixed Window | N requests per minute | Simple rate limiting |
| Sliding Window | N requests in last 60 seconds | Smoother distribution |
| Token Bucket | Refill tokens at fixed rate | Allow bursts |
| Leaky Bucket | Process at constant rate | Smooth traffic |
| Per-User | Limits per user/API key | Fair usage |
| Per-Endpoint | Different limits per API | Protect expensive endpoints |
The gateway can translate between external and internal protocols.
This allows:
The gateway can modify responses before sending them to clients.
Transformations include:
One gateway handles all traffic. Simple but can become a bottleneck.
Different gateways for different client types. More complex but better suited for diverse requirements.
This approach is called the Backend for Frontend (BFF) pattern, which we will cover in the next chapter.
Open-source gateway built on Nginx. Extensible through plugins.
Key features:
Fully managed service from AWS.
Key features:
High-performance web server often used as an API Gateway.
Key features:
Modern proxy designed for microservices.
Key features:
| Feature | Kong | AWS API Gateway | NGINX | Envoy |
|---|---|---|---|---|
| Deployment | Self-hosted or Cloud | Managed | Self-hosted | Self-hosted |
| Extensibility | Plugins (Lua) | Limited | Lua, modules | Filters (C++) |
| Protocol Support | HTTP, gRPC, WS | HTTP, WS | HTTP, TCP | HTTP, gRPC, TCP |
| Observability | Good | Basic | Basic | Excellent |
| Learning Curve | Moderate | Low | Moderate | High |
| Cost | Open source + Enterprise | Pay per request | Open source | Open source |
The gateway is on the critical path for all requests. It must be highly available.
Strategies for high availability:
The gateway should focus on routing and cross-cutting concerns, not business logic.
Every request goes through the gateway, adding latency.
Minimize overhead by:
The gateway can help manage API versions.
Versioning strategies:
| Scenario | Reason |
|---|---|
| External clients (mobile, web) | Reduce client complexity, optimize payloads |
| Multiple microservices | Centralize cross-cutting concerns |
| Third-party API consumers | API keys, rate limiting, usage tracking |
| Protocol translation needed | External REST to internal gRPC |
| Response aggregation needed | Multiple services for single screen |
| Scenario | Alternative |
|---|---|
| Service-to-service calls | Direct communication or service mesh |
| Very simple architecture | Single service may not need a gateway |
| Extreme latency sensitivity | Direct calls save gateway overhead |
| Already using service mesh | Mesh provides similar features |
The API Gateway pattern provides a unified entry point for clients accessing microservices: