API Gateway Patterns: The Edge Layer
The API Gateway acts as the single entry point for all client requests, offloading cross-cutting concerns from individual microservices.
1. Routing and Path Mapping
The Gateway decouples the public API surface from the internal service topology.
* **Dynamic Routing:** Use service discovery (e.g., Consul or Kubernetes DNS) to route `/v1/users/*` to the current healthy instances of `user-service`.
* **Header-Based Routing:** Route requests to specific versions or regions based on headers (e.g., `X-Region: us-east-1`), enabling A/B testing and localized traffic management.
2. API Composition (Request Aggregation)
To prevent "chatter" between the client and multiple services, the Gateway can perform aggregation.
* **Concrete Example:** A "Mobile Dashboard" request requires data from `UserService`, `OrderService`, and `NotificationService`. Instead of the client making three calls, the Gateway executes them in parallel, joins the JSON results, and returns a single payload.
* **Implementation:** Use asynchronous I/O (e.g., Node.js, Go routines, or Project Loom) to prevent the Gateway from blocking while waiting for downstream responses.
3. Authentication and Security Offloading
Centralizing security at the edge prevents inconsistent implementations across services.
* **JWT Validation:** The Gateway validates the token signature and expiration. It then strips the `Authorization` header and replaces it with an internal `X-User-ID` header for downstream services.
* **Rate Limiting:** Implement Distributed Rate Limiting using a sidecar or a central store like **Redis**.
* *Algorithm:* Use **Leaky Bucket** or **Sliding Window Log** to prevent brute-force and DoS attempts before they hit the application logic.
4. Resilience: Circuit Breaking and Retries
The Gateway must protect the system from cascading failures.
* **Circuit Breaker:** If `service-b` latency exceeds 500ms for 5% of requests, the Gateway "opens" the circuit for that route, returning a `503 Service Unavailable` immediately without hitting the backend.
* **Retries with Backoff:** Implement retries *only* for idempotent methods (GET/PUT) with exponential backoff and jitter to avoid the "Thundering Herd" problem when a service recovers.
---
**See Also:**
- [Api Security Patterns](ApiSecurityPatterns) — Deep dive into auth mechanisms.
- [Cloud Networking](CloudNetworking) — VPC and Subnet integration.
- [Backwards Compatibility Strategies](BackwardsCompatibilityStrategies) — Versioning at the gateway level.