API Gateway in System Designing
advance · System Designing
An API Gateway is an architectural pattern that places a dedicated routing server in front of your internal microservices ecosystem. Instead of external clients (mobile apps, single-page web apps, third-party developers) calling dozens of individual microservices directly, they make all their requests to this single entry point. The API Gateway acts as the traffic cop, security guard, and translator at the perimeter of your infrastructure. 1. Core Architectural Responsibilities By decoupling the client-facing APIs from your internal microservice network, the gateway centralizes several cross-cutting concerns: Request Routing (Reverse Proxying): Maps public-facing endpoints to internal service locations. For example, it intercepts a public request to api.example.com/v1/orders and proxies it down to the internal order-service running inside a private virtual cloud. Authentication and Authorization: Instead of implementing JWT validation, OAuth checks, or API key verification inside every single microservice, the gateway handles security at the front door. If a request lacks a valid token, it is rejected instantly before entering your network. Rate Limiting and Throttling: Protects your downstream services from being overwhelmed by enforcing strict traffic limits per user, per IP, or per API token. Centralized Logging and Metrics: Captures unified data on request volume, latency, and HTTP error rates across the entire application ecosystem, making it the primary tool for real-time traffic observability. SSL/TLS Termination: Decrypts incoming public HTTPS traffic at the gateway layer, allowing internal communication between microservices to utilize faster, lightweight HTTP or gRPC protocols. 2. Advanced Pattern: Request Aggregation (BFF) In a complex system, rendering a single frontend dashboard might require data from multiple independent microservices (e.g., user profile data, active notifications, and shopping cart items). Without a gateway, a mobile device would have to execute three separate network requests over slow mobile data networks, increasing battery drain and latency. With an API Gateway using Request Aggregation : The client sends a single request to api.example.com/dashboard . The gateway intercepts the request and fans it out internally to the user-service , notification-service , and cart-service simultaneously over a fast, ultra-low-latency internal network. The gateway aggregates the individual JSON responses into a single combined payload and shoots it back to the client in one clean round-trip. 3. Industry Standard Tools Choosing an API Gateway depends on whether you prefer open-source flexibility, raw performance, or fully managed cloud infrastructure: Tool Type Core Strengths AWS API Gateway Fully Managed (Serverless) Integrates natively with cloud infrastructure like AWS Lambda and Cognito. Scales automatically with zero server maintenance. Kong Open-Source / Enterprise Built on top of NGINX. Known for raw sub-millisecond execution speeds and a massive ecosystem of plugins (for auth, rate-limiting, and transformations). Apigee (Google Cloud) Enterprise API Management Highly geared toward enterprise API governance, monetization strategies, advanced analytics, and developer portals. 4. System Design Trade-offs While an API Gateway simplifies client-side development, it introduces distinct infrastructure challenges: Single Point of Failure (SPOF): If the API Gateway layer goes down, your entire application is completely inaccessible to the outside world. To mitigate this, gateways must be deployed behind high-availability load balancers across multiple availability zones. Increased Latency: Every single request must pass through an extra network hop and a layer of middleware processing before reaching its destination. The gateway code must be highly optimized (often written in low-level languages like Go, Rust, or C-based NGINX configurations) to keep routing overhead minimal.