Difficulty: Intermediate
What does an API gateway do in a microservices architecture, and what are the risks of using one?
Imagine a hotel reception desk. Guests don't wander into the kitchen, laundry and accounts department separately. They talk to the front desk, which checks who they are, routes requests to the right department and keeps a record. An API gateway is that front desk for your backend: a single entry point that sits between clients and many services.
Core responsibilities. Routing: map public paths such as /orders/* and /payments/* to internal services, hiding internal topology and letting services move without clients noticing. Authentication and authorization at the edge: validate JWTs or API keys, check scopes, and pass verified identity downstream in headers so individual services don't each reimplement it. Rate limiting and quotas per client, plan or endpoint. TLS termination. Request and response transformation, such as protocol translation between REST outside and gRPC inside. Aggregation and composition, where one client call fans out to several services and merges results, reducing chatty mobile traffic. Caching of common responses. Observability: consistent access logs, metrics, request id and trace context injection (W3C traceparent). Resilience features: timeouts, retries, circuit breakers, load balancing and canary or blue-green routing. Security controls like WAF integration, IP allow or deny lists, request size limits and schema validation against the OpenAPI spec. Versioning and deprecation routing, e.g. sending /v1 to legacy services.
Products and patterns. Managed: AWS API Gateway, Azure API Management, Google Apigee, Cloudflare. Self-hosted: Kong, NGINX, Envoy, Traefik, Spring Cloud Gateway. Related concept, Backend for Frontend: a separate gateway per client type (web, mobile, partners), each tailored to that client's needs, so the mobile BFF returns smaller, aggregated payloads. Contrast with a service mesh such as Istio or Linkerd, which manages east-west service-to-service traffic, while the gateway handles north-south traffic from outside.
Trade-offs and risks. Single point of failure and bottleneck: run multiple instances across zones and keep the data path stateless and fast. Added latency of one extra hop, usually small compared with the benefits. Becoming a monolith in disguise: if business logic creeps into the gateway, teams must coordinate deploys through it. Keep it thin, with policy rather than domain logic. Configuration sprawl and coupling: a central team owning every route becomes a bottleneck, so use self-service config in Git with automated deployment. Security concentration: if the gateway is compromised, everything is exposed, so services should still authenticate internal calls (zero trust) and not blindly trust headers, using mTLS or signed tokens.
Common interview follow-up: where should rate limiting and authentication live, gateway or service? Coarse-grained, client-level concerns like authenticating the token, throttling and blocking abuse belong at the gateway. Fine-grained, resource-level authorization such as 'can this user view order 42' needs domain data and must live in the service.
Another follow-up: gateway versus load balancer. A load balancer distributes traffic across instances of one service, typically at layer 4 or 7 with little API awareness. A gateway is application-aware, with routing by path, auth, transformation and per-client policies. In practice gateways often sit behind or include load balancing.
For small systems, a plain reverse proxy might be enough. Add a full gateway when you have multiple services, external consumers and cross-cutting policy needs.
services:
- name: orders-svc
url: http://orders.internal:8080
routes:
- name: orders-route
paths: [/v1/orders]
methods: [GET, POST]
plugins:
- name: jwt
- name: rate-limiting
config: { minute: 300, policy: redis, redis_host: redis.internal }
- name: correlation-id
config: { header_name: X-Request-Id, generator: uuid }
Routing plus authentication, rate limiting and request id injection, all at the edge in declarative config.
API gateway, microservices, cross-cutting concerns, BFF