What is the API Gateway pattern?

Topic: microservices, Infrastructure. Level: Intermediate.

Answer

An API Gateway is the single entry point between clients and the microservices behind it. It handles cross-cutting concerns so services don't each reimplement them: routing/composition, authentication/authorization, rate limiting, TLS termination, request/response transformation, caching, and observability. It shields clients from internal topology and lets services evolve independently. Risk: it can become a bottleneck or a "god" component, so keep business logic out of it. Use a token-bucket limiter keyed by client identity (API key/user/IP). Tokens refill at the allowed rate up to a burst cap; a request that finds no token gets HTTP 429. Redis backs the counter so the limit holds across all gateway instances. ```yaml # Spring Cloud Gateway — route + auth + rate limit at the edge spring.cloud.gateway.routes: - id: orders uri: lb://order-service predicates: [ Path=/api/orders/** ] filters: - StripPrefix=1 - name: RequestRateLimiter args: { redis-rate-limiter.replenishRate: 100, redis-rate-limiter.burstCapacity: 200 } ``` ```java // Key the limiter by API key so each client gets its own bucket @Bean KeyResolver apiKeyResolver() { return exchange -> Mono.just( exchange.getRequest().getHeaders().getFirst("X-API-Key")); } // replenishRate=10, burstCapacity=20 -> ~10 req/s, bursts to 20; excess -> 429 ```