microservices

What is API Gateway and why is it needed in Microservices?

What is an API Gateway?

An API Gateway is a single entry point for all client requests in a microservices architecture. It sits between the client and the backend services, acting as a reverse proxy that routes, composes, and manages API traffic.

Client → API Gateway → ┌─ User Service
                        ├─ Order Service
                        ├─ Payment Service
                        └─ Notification Service

Without an API Gateway — The Problem

Mobile App → User Service    (port 8081)
Mobile App → Order Service   (port 8082)
Mobile App → Payment Service (port 8083)
  • Client must know every service's address.
  • Cross-cutting concerns (auth, rate-limiting) duplicated in every service.
  • Tight coupling between client and internal architecture.

With an API Gateway — The Solution

Mobile App → Gateway (port 443) → routes to appropriate service

Key Responsibilities

ResponsibilityDescription
Request RoutingRoutes /api/users/** → User Service, /api/orders/** → Order Service
Authentication & AuthorizationValidates JWT/OAuth tokens at the edge — services don't need to
Rate LimitingThrottles excessive requests per client/IP
Load BalancingDistributes requests across service instances
Request/Response TransformationAggregates responses from multiple services into one
Circuit BreakingPrevents cascading failures when a downstream service is down
SSL TerminationHandles HTTPS — internal services can use HTTP
Logging & MonitoringCentralized request logging, tracing, and metrics
API VersioningRoutes /v1/users and /v2/users to different service versions

Popular Implementations

GatewayEcosystem
Spring Cloud GatewayJava / Spring Boot — reactive, non-blocking
Netflix ZuulJava (legacy — replaced by Spring Cloud Gateway)
KongLanguage-agnostic, built on Nginx
AWS API GatewayManaged service on AWS
Nginx / EnvoyHigh-performance reverse proxies

Spring Cloud Gateway Example

spring: cloud: gateway: routes: - id: user-service uri: lb://USER-SERVICE predicates: - Path=/api/users/** filters: - StripPrefix=1 - name: CircuitBreaker args: name: userServiceCB fallbackUri: forward:/fallback/users

Gateway Patterns

  1. Backend for Frontend (BFF): Separate gateways for web, mobile, and IoT clients — each tailored to the client's needs.
  2. API Composition: Gateway calls multiple services and merges responses into a single payload for the client.
  3. Edge Authentication: Token validation at the gateway; services receive trusted headers like X-User-Id.

When NOT to Use?

  • Monolithic apps: No benefit — adds unnecessary complexity.
  • Simple 2-3 service setups: Might be overkill; direct service-to-service calls may suffice.
What is API Gateway and why is it needed in Microservices? | DevExCode