API Gateway & Reverse Proxy

12 min read

Reading Progress0%
System Design Index
Start Here
Tier 1 -- Building Blocks
Tier 2 -- Core Systems
URL Shortener — System Design InterviewFree
Pastebin — System Design InterviewFree
News Feed System — System Design InterviewFree
Chat System (WhatsApp/Messenger) — System Design InterviewFree
Tier 3 -- Location & Real-Time
Tier 4 -- Infrastructure & Data
Tier 5 -- Finance & Commerce
Tier 6 -- Advanced & Collaborative
System Design Index
Start Here
Tier 1 -- Building Blocks
Tier 2 -- Core Systems
URL Shortener — System Design InterviewFree
Pastebin — System Design InterviewFree
News Feed System — System Design InterviewFree
Chat System (WhatsApp/Messenger) — System Design InterviewFree
Tier 3 -- Location & Real-Time
Tier 4 -- Infrastructure & Data
Tier 5 -- Finance & Commerce
Tier 6 -- Advanced & Collaborative

API Gateway & Reverse Proxy

1. What Is It?

A is a server that sits between clients and backend servers, forwarding requests on behalf of clients. It provides SSL termination, caching, compression, and load balancing — the backend servers see only the proxy's IP, not the client's. A is infrastructure-level: it routes HTTP traffic.

An is a superset: it does everything a does, plus adds authentication/authorization, rate limiting, request transformation, analytics, versioning, and protocol translation. It's the single entry point for all external API clients. An is application-aware — it understands API contracts, not just HTTP traffic.

The relationship: every API gateway is a reverse proxy, but not every reverse proxy is an API gateway. The distinction matters in interviews because they solve different problem classes: reverse proxy for scaling and SSL offload; API gateway for API lifecycle management, security, and microservices coordination.


QUICK CHECK

Your team is building a microservices platform and needs a single entry point that handles JWT authentication, enforces per-client rate limits, and translates between REST and gRPC. Which type of infrastructure component best fits this requirement, and why?

Choose one answer

2. How It Works

Reverse Proxy Core Functions

A receives requests from clients and forwards them to one or more backend servers:

Client → Reverse Proxy → Backend Server(s)
                         ↑ client only sees the proxy's IP

SSL Termination: The proxy decrypts HTTPS from clients, forwards plain HTTP to backends. Centralizes certificate management; offloads TLS CPU from application servers.

Caching: Cache responses from backends. On cache hit, serve from proxy memory/disk — backend is not called. Reduces backend load and improves response time for repeated requests.

Compression: Apply gzip/brotli compression at the proxy layer. Application code doesn't need to handle it.

Static file serving: Serve images, JS, CSS directly from the proxy's disk, bypassing the application server entirely.

Buffering: Buffer slow client connections so backends can respond quickly and move on, instead of being held waiting for slow clients to receive data.

API Gateway Capabilities (Superset of Reverse Proxy)

Client (web/mobile) → [API Gateway] → Microservice A
                                    → Microservice B
                                    → Microservice C (aggregated response)
CapabilityWhat It Does
AuthenticationValidates JWT (JSON Web Token — a signed token proving the client's identity) / OAuth tokens before forwarding to backends
AuthorizationCoarse-grained access control (this API key can access /api/v1/users but not /api/admin)
Rate LimitingPer-client/per-endpoint request throttling (see Rate Limiter building block)
Request TransformationTranslate REST → gRPC (a high-performance binary RPC protocol often used between microservices), rename headers, add/remove fields, inject request IDs
Response TransformationFilter fields the client doesn't need, translate response formats
Request AggregationCall 3 microservices, merge responses into one — client makes 1 request, gateway fans out
Routing/v1/* → legacy service; /v2/* → new service; canary deploys
Load BalancingDistribute requests across backend instances
Circuit BreakingStop forwarding to failing backends
Analytics & LoggingCentralized access logs, latency tracking, per-route metrics
API VersioningRoute /v1/ to old backends, /v2/ to new backends simultaneously

Auth Offloading Pattern

Without API Gateway:
  Every microservice must:
  1. Accept JWT token
  2. Validate signature
  3. Check expiry
  4. Parse claims
  5. Authorize the specific operation
  → Auth logic duplicated across all services

With API Gateway:
  Gateway:
  1. Validates JWT signature + expiry
  2. Extracts claims (user_id, roles)
  3. Injects claims as headers: X-User-Id, X-User-Role

  Microservice:
  1. Trusts headers from gateway (network boundary = trust boundary)
  2. Only does fine-grained resource-level auth (can this user_id access this specific resource?)
  → Auth logic centralized; services focus on business logic

WHY: Centralizing auth at the gateway is simpler to maintain (one place to update auth logic) but requires trusting the internal network. Fine-grained authorization (can user X access resource Y?) must still live in each service — the gateway can't know the business rules of every microservice.

Request Aggregation / Fan-Out

This pattern reduces mobile client chattiness — on a 150ms mobile connection, making 3 serial requests adds 450ms; 1 aggregated request adds 150ms + max(backend ).

Mermaid: Full API Gateway Architecture

BFF (Backend for Frontend) Pattern

Instead of one general-purpose gateway for all clients, create a separate gateway per client type:

WHY BFF: A single becomes a "lowest common denominator" API that tries to satisfy all clients. Mobile clients need smaller payloads; web clients need richer data; partner APIs need specific contracts. BFF lets each team own their gateway layer and evolve independently.

API Gateway vs Service Mesh

DimensionAPI Gateway (Kong, AWS API GW)Service Mesh (Istio, Linkerd)
Traffic directionNorth-South (external clients → internal services)East-West (service A → service B internally)
DeploymentCentralized (a few gateway instances)Decentralized (sidecar proxy per pod)
Primary concernsAuth, rate limiting, API keys, versioning, analyticsmTLS, service discovery, observability, retries, circuit breaking
GranularityPer-API, per-routePer-service-to-service call
StackKong: Nginx/OpenResty; AWS: managedIstio: Envoy sidecar
Used together?Yes — complementary, not competing

QUICK CHECK

Your company has 15 microservices, and each one currently implements JWT validation, signature checking, and expiry verification independently. A new team member proposes centralizing JWT validation at the API gateway and having the gateway inject extracted claims (like user ID and role) as headers that each microservice can trust. What is the key trade-off this approach introduces?

Choose one answer

3. Variants & Comparisons

API Gateway Products

ProductTypeBuilt OnBest For
KongOpen-source + enterpriseNginx + OpenResty (Lua)Self-hosted, plugin ecosystem, open-source
AWS API GatewayManaged SaaSAWS proprietaryAWS-native, serverless, Lambda integration
NginxReverse proxy + LB (not full API GW)C + Lua (OpenResty)High-performance proxying, static serving
HAProxyReverse proxy + LBCUltra-high-throughput TCP/HTTP load balancing
TraefikCloud-native reverse proxyGoDocker/Kubernetes auto-discovery, Let's Encrypt
EnvoyL7 proxyC++Service mesh sidecar, gRPC, observability
ApigeeEnterprise API managementGoogle Cloud managedEnterprise API lifecycle, monetization
AWS ALBL7 load balancerAWS managedAWS-native L7 routing (subset of API GW features)

REST API Gateway vs GraphQL Gateway

DimensionREST API GatewayGraphQL Gateway (Apollo Federation)
Endpoint modelMany endpoints (per resource)Single /graphql endpoint
Client flexibilityFixed response shapes per endpointClient specifies exactly what fields it needs
Backend aggregationExplicit fan-out in gateway logicSchema composition (subgraphs federated)
CachingStraightforward (URL-based)Complex (queries are POST bodies — not URL-cacheable)
Learning curveLowHigher
Best forPublic APIs, external integrationsInternal apps with diverse client requirements

QUICK CHECK

Your team is building an internal dashboard app where the mobile client, web client, and desktop client each need very different subsets of data from the same backend services. You want clients to fetch only the fields they need in a single request. Which API gateway approach is best suited to this requirement, and what is a notable trade-off you accept?

Choose one answer

4. When to Use It (and When NOT To)

Use an API Gateway When:

  • Microservices architecture: Multiple backend services need a unified entry point. Auth, rate limiting, and logging shouldn't be duplicated in every service.
  • Centralized auth: Validate JWT/OAuth tokens once at the gateway; inject user identity as trusted headers downstream. Avoids auth logic in every service.
  • Different API versions in production: Route /v1/ and /v2/ to different backends simultaneously during migration. Zero-downtime versioning.
  • Protocol translation: External clients use REST/HTTP; internal services use gRPC. The gateway translates.
  • Canary deployments: Route 5% of traffic to new service version, 95% to old. Gradual rollout with automatic rollback on error rate spike.
  • Rate limiting by API key: API products with free/paid tiers. Gateway enforces limits without touching service code.

Use a Reverse Proxy Only When:

  • Single backend service (monolith): A full is overkill. Nginx as a for SSL termination, caching, and load balancing is sufficient.
  • Static site + API backend: Nginx serves *.js, *.css files directly; proxies /api/* to the application server. No need for a full .
  • Simple SSL termination: You just need HTTPS → HTTP conversion. Nginx or an ALB handles this.

Do NOT Use an API Gateway When:

  • Internal service-to-service calls: An API gateway is for external (north-south) traffic. Service A calling Service B internally should use direct HTTP/gRPC + (Consul, Kubernetes DNS), not go through an external API gateway.
  • You haven't outgrown a monolith yet: API gateways introduce complexity (another service to deploy, manage, and tune). For a monolithic app with one backend, a simple Nginx is simpler and more reliable.
  • -critical paths where gateway overhead matters: An API gateway adds 5–20ms per request (auth validation, plugin execution, logging). For sub-10ms P99 requirements, measure and consider gateway bypass for hot paths.

QUICK CHECK

Your team runs a monolithic e-commerce application behind Nginx, which handles SSL termination and load balancing across two app servers. A senior engineer suggests replacing Nginx with a full API gateway. Which situation would best justify that switch?

Choose one answer

5. Real-World Usage

Netflix — Zuul (API Gateway)

Netflix built and open-sourced Zuul as their . By 2013, Zuul was handling 2 billion daily API requests from external Netflix clients (iOS, Android, web, Smart TVs, gaming consoles) to 500+ internal microservices. Zuul's key features: dynamic filters (written in Groovy, deployed without restart), request routing, authentication, and deep integration with Hystrix (). Netflix eventually moved to Zuul 2 (async, non-blocking) and later adopted Zuul alongside other gateway implementations.

Kong (Open-Source)

Kong powers API gateways for thousands of enterprises. Built on Nginx + OpenResty (Lua), it provides a plugin architecture where each cross-cutting concern (auth, rate limiting, logging, transformation) is an independent plugin. Kong can handle hundreds of thousands of requests/second on commodity hardware. The plugin ecosystem (100+ official + community plugins) makes it the most flexible self-hosted .

AWS API Gateway + Lambda (Serverless Pattern)

The AWS API Gateway + Lambda pattern is the dominant serverless API architecture: API Gateway handles routing, auth (via Cognito Authorizer or Lambda Authorizer), throttling, and CORS; Lambda functions implement the business logic. No servers to manage. HTTP APIs (newer, lighter) are up to 71% cheaper than REST APIs for simple proxy scenarios.


QUICK CHECK

A team is building a serverless API on AWS where different endpoints need authentication, rate limiting, and CORS handling, while the actual business logic runs in isolated functions with no servers to manage. Which architectural pattern best fits this requirement?

Choose one answer

6. Interview Cheat Sheet

5 Sentences to Show Deep Understanding

  1. "An is a plus API management: it adds authentication, rate limiting, request transformation, aggregation, and analytics to the raw proxying capabilities of Nginx/HAProxy — the key insight is centralizing cross-cutting concerns that would otherwise be duplicated in every microservice."

  2. "Auth offloading at the is powerful but incomplete: the gateway handles authentication (is this request from a valid user?) and coarse-grained authorization (is this API key allowed to call /payments?), but fine-grained authorization (can this user access this specific resource?) must stay in the individual service — the gateway can't know every service's business rules."

  3. "BFF (Backend for Frontend) solves the 'lowest common denominator API' problem: instead of one general-purpose gateway that returns too much data to mobile clients and too little to web clients, you create separate gateways per client type, each evolving independently."

  4. "API gateways handle north-south traffic (external → internal); service meshes handle east-west traffic (service A → service B internally). They are complementary: the gateway secures the perimeter, the service mesh secures and observes internal communication — you often need both at scale."

  5. "Request aggregation at the gateway reduces mobile client chattiness: instead of a mobile app making 3 serial API calls over a 150ms- connection (450ms total), the gateway fans out to 3 services in parallel and returns one merged response (150ms + max backend )."

Common Follow-Up Questions

Q: What are the drawbacks of an API gateway? A: (1) : If the gateway goes down, all external traffic is blocked. Mitigate with multiple instances behind a and health checks. (2) Latency overhead: Each plugin (auth, rate limit, logging) adds overhead — 5–20ms typically. (3) Bottleneck at scale: The gateway must handle all external traffic; it needs and caching. (4) Tight coupling: Services may start to depend on gateway-injected headers or behaviors, creating implicit coupling.

Q: How do you handle authentication in a microservices system? A: Three-layer approach: (1) API Gateway validates the JWT signature and expiry, extracts claims (user_id, roles), and injects them as trusted headers (X-User-Id, X-Roles). (2) Services trust these headers within the internal network (the gateway is the trust boundary). (3) Each service still does fine-grained authorization — "can user 123 read order 456?" — based on its own data and business rules. NEVER trust user-supplied identity headers from outside the gateway.

Q: What is the difference between an API key and a JWT? A: An API key is an opaque secret — the gateway must look it up in a database to validate it and find associated permissions (database call on every request). A JWT is self-contained — it carries signed claims, and the gateway validates the signature locally using a public key (no database call). JWTs are better for high-scale APIs; API keys are simpler for server-to-server partner integrations where the key doesn't change often.

Q: How would you do a zero-downtime version migration at the API gateway? A: Route by path prefix (deploy v2 alongside v1): /v1/* → old service, /v2/* → new service. Both run simultaneously. Clients opt into v2 when ready. When v1 traffic drops to zero (or a deadline passes), decommission the v1 route. For canary: use weighted routing — route 5% of /v2/* traffic to new backend, 95% to stable backend. Monitor error rates; ramp to 100% if healthy.

Connections to Other Building Blocks

  • Load Balancing: The API gateway distributes requests across backend instances (it's an L7 plus more). The gateway sits behind a L4 load balancer for .
  • : Rate limiting is one of the most common API gateway plugins. The gateway is the enforcement point (Redis-backed, per-API-key counters).
  • : API gateways (Kong, Envoy) have plugins that stop forwarding to failing backends and return cached responses or error messages.
  • Caching: API gateways cache responses at the gateway layer (e.g., Kong's Response Caching plugin), reducing load on backends for repeated identical requests.
  • Service Mesh (Envoy/Istio): API gateway handles external north-south traffic; service mesh handles internal east-west traffic. They complement each other.
  • : handles static asset delivery at the edge; API gateway handles dynamic API traffic. Both sit in front of backends, but at different layers of the stack.
Glossary History

Click dotted jargon to save explanations here.

Glossary History

Click dotted jargon to save explanations here.