What is an API Gateway and Why Use One?
The Problem Without a Gateway
Without a gateway, clients must know the address of every service. A mobile app rendering a product page needs to call the product service, inventory service, pricing service, and reviews service separately. This creates 4 round trips over the network, each adding 50-200ms latency on mobile networks. Clients also need to handle service discovery, authentication with each service, and protocol differences.
What the Gateway Provides
The gateway presents a unified API to clients. Instead of 4 calls, the client makes 1 call to the gateway, which fans out to backend services over the fast internal network (1-2ms latency). The gateway handles authentication once, translates protocols (REST to gRPC), and aggregates responses. Clients are decoupled from internal service topology changes.
Core Gateway Functions
Routing: Direct requests to correct backend service based on path, headers, or content. Composition: Aggregate multiple service responses into one. Protocol translation: Convert between REST, gRPC, WebSocket, GraphQL. Cross cutting concerns: Authentication, rate limiting, logging, metrics collection applied uniformly.
Gateway vs Service Mesh
Gateways handle north south traffic (external clients to internal services). Service meshes handle east west traffic (service to service). A gateway sits at the edge; a mesh sits between services. Most architectures use both: gateway for external API, mesh for internal communication.