API Gateway
IntermediateAn API Gateway is a single entry point for all client requests. It handles routing, authentication, rate limiting, request aggregation, and protocol translation, shielding internal microservices from direct client access.
Overview
In a microservices architecture, clients would need to know the addresses of dozens of services and handle cross-cutting concerns (auth, rate limiting) per service. An API Gateway acts as a reverse proxy — all client requests go through it. The gateway routes requests to the appropriate microservice, handles authentication/authorisation (JWT validation), rate limiting, request/response transformation, load balancing, caching, and API composition (aggregating responses from multiple services into one). Popular gateways include Kong, NGINX, AWS API Gateway, Spring Cloud Gateway, and Envoy/Istio ingress. The Backend for Frontend (BFF) pattern uses separate gateways for different clients (mobile vs web), each tailored to the client's needs.
Gateway Responsibilities
The API gateway handles routing, authentication, rate limiting, response aggregation, protocol translation, and SSL termination — keeping microservices focused on business logic.
// API Gateway architecture
//
// Mobile App Web App 3rd-party
// │ │ │
// └─────┬─────┴──────────┘
// ▼
// ┌──────────────────┐
// │ API Gateway │ ← routing, auth, rate limit, aggregation
// │ (Kong / NGINX) │
// └──┬──┬──┬──┬──────┘
// │ │ │ │
// ▼ ▼ ▼ ▼
// User Order Payment Search
// Svc Svc Svc Svc
// Gateway responsibilities:
// 1. Routing: /api/users/** → User Service
// 2. Auth: Validate JWT, attach user context
// 3. Rate limit: 100 req/min per API key
// 4. Aggregation: /api/dashboard → calls 3 services, merges response
// 5. Transformation: REST → gRPC (for internal services)
// 6. SSL termination: HTTPS at gateway, HTTP internally
// 7. Logging: Centralised access logs, request tracingSpring Cloud Gateway Example
Spring Cloud Gateway provides a declarative way to define routes, filters, and predicates. It integrates with Spring Security, Resilience4j, and service discovery.
// Spring Cloud Gateway — route configuration
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://USER-SERVICE # service discovery
predicates:
- Path=/api/v1/users/**
filters:
- StripPrefix=0
- name: CircuitBreaker
args:
name: userServiceCB
fallbackUri: forward:/fallback/users
- id: order-service
uri: lb://ORDER-SERVICE
predicates:
- Path=/api/v1/orders/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
key-resolver: "#{@userKeyResolver}"
// JWT authentication filter
@Component
public class JwtAuthFilter implements GlobalFilter {
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String token = exchange.getRequest().getHeaders().getFirst("Authorization");
if (token == null || !jwtUtil.isValid(token)) {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
}Key Points to Remember
- 1API Gateway is a single entry point — routes, authenticates, rate-limits, and aggregates.
- 2Decouples clients from internal service topology — clients call one URL, gateway routes internally.
- 3Backend for Frontend (BFF): separate gateways tailored for mobile, web, and third-party clients.
- 4Popular options: Kong, NGINX, AWS API Gateway, Spring Cloud Gateway, Envoy.
- 5Avoid making the gateway a bottleneck — keep it thin, push business logic to services.
Interview Questions
Sign in to ask AriaWhat is an API Gateway and why is it needed?
What cross-cutting concerns does an API gateway handle?
What is the Backend for Frontend (BFF) pattern?
How do you prevent the API gateway from becoming a bottleneck?
Design an API gateway for a system with 50 microservices serving mobile and web clients.
Ask Aria about API Gateway
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.