What is API Gateway and What Problems Does It Solve
An API Gateway is a reverse-proxy architectural component that acts as the single public entry point (Edge Service) through which all external clients (web browsers, mobile appl...
🟢 Junior Level
An API Gateway is a reverse-proxy architectural component that acts as the single public entry point (Edge Service) through which all external clients (web browsers, mobile applications, and third-party partners) communicate with an internal microservices cluster.
Public Internet (Untrusted): Edge Perimeter: Private Network (Trusted):
[Mobile App] ───┐ ┌──► [User Service]
├──HTTPS (Public IP)──► ┌─────────────────────────┐ │
[Web Browser] ───┼─────────────────────► │ API GATEWAY │────┼──► [Order Service]
│ │ (Spring Cloud / Envoy) │ │
[Third-Party] ───┘ └─────────────────────────┘ └──► [Payment Service]
The Real-World Analogy
Think of an API Gateway as an International Airport Terminal:
- Without a Gateway: International travelers enter a country through random city streets, forcing every house, coffee shop, and office building to check passports, inspect luggage, and stamp visas (chaotic, insecure, and redundant).
- With an API Gateway: All incoming travelers arrive at a central terminal where customs, passport control (Authentication), and security screening (Rate Limiting & DDoS inspection) occur once at the perimeter before travelers board domestic connecting flights.
Core Problems Solved
| Problem Without Gateway | How API Gateway Solves It |
|---|---|
| Exposed Internal Topology | Clients only see a single public domain (api.domain.com); internal pod IPs, ports, and microservice boundaries remain hidden. |
| Chatty Mobile Networks | Aggregates multiple internal calls into a single cohesive response, minimizing round-trips over cellular connections. |
| Duplicated Security Logic | Validates JWTs, handles OAuth2/OIDC, and manages SSL/TLS termination in one central perimeter service. |
| CORS Complexities | Handles Cross-Origin Resource Sharing (CORS) headers centrally rather than configuring dozens of microservices. |
| Traffic Overload & DDoS | Enforces IP-based or user-based Rate Limiting at the edge before traffic reaches internal databases. |
🟡 Middle Level
1. Spring Cloud Gateway Architecture
Modern Spring Cloud Gateway is built on a non-blocking reactive stack (Project Reactor + Netty + Spring WebFlux), replacing the obsolete servlet-blocking Netflix Zuul 1.
Its architecture is centered around three core concepts:
- Route: The fundamental routing definition containing an ID, target URI, predicates, and filters.
- Predicate: A Java
Predicate<ServerWebExchange>that evaluates whether an incoming request matches route criteria (e.g., path pattern, HTTP method, headers, query parameters). - Filter: Intercepts and modifies the request (Pre-filter) before forwarding to downstream microservices, or transforms the response (Post-filter).
@Configuration
public class GatewayRoutesConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder,
JwtAuthenticationFilter authFilter,
RedisRateLimiter redisRateLimiter) {
return builder.routes()
.route("order-service-route", r -> r
.path("/api/v1/orders/**")
.and().method(HttpMethod.GET, HttpMethod.POST)
.filters(f -> f
.filter(authFilter) // Custom JWT validation filter
.requestRateLimiter(c -> c
.setRateLimiter(redisRateLimiter)
.setKeyResolver(exchange -> ReactiveSecurityContextHolder.getContext()
.map(ctx -> ctx.getAuthentication().getName())))
.circuitBreaker(c -> c
.setName("orderServiceBreaker")
.setFallbackUri("forward:/fallback/orders"))
.stripPrefix(1) // Rewrites /api/v1/orders/123 -> /v1/orders/123
)
.uri("lb://order-service") // Dynamic resolution via Service Discovery
)
.build();
}
}
2. The 7-Step Edge Processing Pipeline
[Incoming External HTTP Request]
│
▼
[1. TLS Termination] ──► Decrypts HTTPS at the perimeter; uses lightweight HTTP/gRPC internally
│
▼
[2. WAF & Anti-DDoS] ──► Inspects payloads for SQLi, XSS, and malicious bot user-agents
│
▼
[3. Rate Limiting] ──► Enforces Redis Token Bucket limits (returns HTTP 429 if exceeded)
│
▼
[4. Authentication] ──► Validates JWT signatures and verifies token expiration
│
▼
[5. Header Propagation] ──► Injects X-Correlation-ID, X-User-Id: 42, and X-User-Roles
│
▼
[6. Protocol Mediation] ──► Translates external JSON/REST into internal gRPC/Protobuf
│
▼
[7. Reverse Routing] ──► Forwards sanitized request to healthy pod via Service Discovery
🔴 Senior Level
1. Token Relay vs Phantom Token Architecture
How should an API Gateway propagate user identity to downstream microservices?
Phantom Token Pattern:
[Mobile Client] ──(Opaque Token: "d41d8cd98f00b204")──► [API Gateway]
│ 1. Validates in Redis / Keycloak
▼ 2. Mints / Retrieves signed JWT
[Internal Microservice]
(Receives JWT / X-User-Id headers)
- Token Relay Pattern: The client sends an encoded JWT. The gateway validates the signature and forwards the identical JWT in the
Authorization: Bearerheader downstream.- Downside: Leaks sensitive token claims into client logs; internal microservices waste CPU verifying identical cryptographic signatures redundantly.
- Phantom Token Pattern (Enterprise Best Practice):
- External clients receive a cryptographically meaningless, opaque reference token (e.g., a random UUID string).
- The API Gateway checks the opaque token against an in-memory Redis session cache or OAuth2 Authorization Server (Keycloak / Okta).
- The Gateway swaps the opaque token for a freshly minted or cached internal JWT token, or strips the token entirely and injects trusted internal headers (
X-User-Id: 104,X-User-Roles: ADMIN). - Downstream services trust the internal network perimeter and process requests without expensive cryptographic verification.
2. The “Smart Gateway” (God Gateway) Anti-Pattern
The most prevalent architectural error when operating an API Gateway is allowing it to evolve into a monolithic repository of business logic:
- The Anti-Pattern: Adding database connections, business validation rules, complex JSON data transformations, and orchestrating multi-step business transactions directly inside the gateway.
- The Fallout: Cross-team deployment contention (every microservice team needs to modify the gateway); severe memory leaks from deserializing large payloads; high latency caused by single-thread Event Loop blocking.
- The Architectural Principle: “Smart Endpoints, Dumb Pipes.” The API Gateway must remain a lightweight reverse proxy handling only routing and edge concerns. When specialized client aggregation is required, deploy the Backend for Frontend (BFF) pattern.
3. Comparing Edge Technologies
| Technology | Underlying Engine | Best Fit & Operational Characteristics |
|---|---|---|
| Spring Cloud Gateway | Java Netty / Project Reactor | Standard choice for pure Spring Boot teams; programmatic Java route filters and native Spring Security integration. |
| Kong API Gateway | NGINX / OpenResty (C/Lua/Go) | Ultra-high throughput ($> 50\text{k RPS}$); extensive plugin ecosystem (Lua/Go) for enterprise security. |
| Envoy / Kubernetes Gateway API | C++ High Performance | Modern cloud-native standard; dynamic xDS configuration, seamless integration with Kubernetes Ingress and Istio Service Mesh. |
| AWS API Gateway / Apigee | Managed Cloud SaaS | Zero-maintenance serverless deployments; built-in API monetization, developer portals, and usage plans. |
4 Tricky Questions
1. Why is Spring Cloud Gateway built on a non-blocking reactive stack (Netty + WebFlux) rather than a servlet container (Tomcat / Zuul 1)?
Answer: Classic servlet containers (such as Tomcat or Netflix Zuul 1) utilize the Thread-per-Request concurrency model. In an API Gateway, nearly all execution time is spent on I/O (waiting for downstream microservices to respond over the network). Under heavy loads, if downstream services experience latency, hundreds of Tomcat worker threads become blocked simultaneously. The thread pool (typically 200–500 threads) exhausts rapidly, causing connection drops and cascading failure. Spring Cloud Gateway uses a non-blocking Event Loop architecture (Netty). A small fixed pool of worker threads (typically matching the number of CPU cores) handles thousands of concurrent socket connections asynchronously without thread blocking or OS context switching overhead.
2. What is API Composition (Aggregation) at the Gateway, and why does it become a catastrophic bottleneck at scale?
Answer: API Composition occurs when the gateway receives a single client request and fans out multiple concurrent calls to downstream microservices (e.g., fetching user profile, shopping cart, and recommendations simultaneously), merging the results into one unified JSON response. At high scale:
- Tail Latency Amplification: The gateway’s response time is bottlenecked by the slowest service in the fan-out chain ($p99$ tail latency compounds).
- JVM Heap & GC Thrashing: Deserializing multiple JSON payloads, merging large object graphs in memory, and re-serializing into a response creates massive heap churn, triggering frequent Stop-the-World garbage collection pauses. Remedy: Shift aggregation logic to dedicated Backend for Frontend (BFF) microservices or use asynchronous GraphQL Federation.
3. How do you implement distributed Rate Limiting across 10 horizontal API Gateway instances?
Answer: In a clustered gateway deployment, local in-memory counters cannot be used because incoming requests are distributed across multiple instances by a load balancer; a client could easily bypass limits by hitting different gateway nodes. Distributed rate limiting is implemented using a centralized Redis Token Bucket algorithm:
- All 10 gateway instances communicate with a shared Redis cluster.
- The rate-limiting logic is executed atomically via a Redis Lua script (built into Spring Cloud Gateway’s
RedisRateLimiter), ensuring thread-safe token consumption in a single Redis round-trip without distributed locking. - If tokens are exhausted, the gateway immediately returns
HTTP 429 Too Many Requestswith aRetry-Afterheader without forwarding the call downstream.
4. What is the risk of verifying asymmetric cryptographic JWT signatures (RS256) on every request at 100,000 RPS?
Answer: Verifying asymmetric cryptographic signatures (such as RSA-2048 with SHA-256) involves computationally expensive mathematical operations (modular exponentiation). At 100,000 requests per second, performing full signature verification on every request will completely saturate the gateway’s CPU cores, driving latency spikes and requiring massive horizontal over-provisioning. Architectural Mitigations:
- Local Token Caching: Cache the verification result in a local in-memory Caffeine cache keyed by a SHA-256 hash of the token, bounded by the token’s
exp(expiration) timestamp. - Elliptic Curve Cryptography: Adopt ES256 (ECDSA) or EdDSA (Ed25519) algorithm keys, which require significantly fewer CPU cycles for signature verification than RSA keys while maintaining equivalent cryptographic strength.
🎯 Interview Cheat Sheet
Must Know
- Definition: The centralized reverse-proxy gateway providing a single public entry point to internal microservices.
- Core Edge Functions: Routing, TLS Termination, Authentication (JWT/OAuth2), Rate Limiting, CORS handling, and Distributed Tracing (
Correlation-ID). - Modern Concurrency Model: Built on non-blocking reactive event loops (Netty / WebFlux / Envoy), not blocking thread-per-request servlets.
- Architectural Rule: “Smart Endpoints, Dumb Pipes” — never place business logic, SQL queries, or heavy transaction orchestration inside an API Gateway.
- Security Pattern: The Phantom Token Pattern uses opaque tokens externally and swaps them for rich JWTs internally at the edge.
Gateway Edge Protection Checklist
- Redis Token Bucket Rate Limiting to prevent brute-force and DDoS attacks.
- Circuit Breakers on every route to fail fast during backend outages.
- Strict HTTP Timeouts (Connect Timeout: 1s, Read Timeout: 3s) to prevent resource hangs.
- Header Sanitization to strip incoming spoofed internal headers (e.g.,
X-User-Id).
Common Follow-up Questions
- “How do you prevent the API Gateway from becoming a Single Point of Failure?” $\to$ Deploy multiple stateless gateway replicas behind an L4 Cloud Load Balancer (AWS NLB) with horizontal pod autoscaling.
- “What is the difference between an API Gateway and a Kubernetes Ingress Controller?” $\to$ Ingress handles basic cluster-level L7 routing and TLS; an API Gateway adds advanced application-layer policies (API keys, rate limiting, authentication, request transformation).
- “What is the BFF pattern?” $\to$ Creating separate lightweight gateways for different client categories (e.g., Mobile BFF vs Web Desktop BFF) to optimize payload sizes and client experiences.
Red Flags (DO NOT Say)
- ❌ “The API Gateway connects to the database to validate if a user account is active.” (Gateways must remain stateless and delegate database access to domain services).
- ❌ “Spring Cloud Gateway uses Tomcat servlets to process requests.” (It runs on Project Reactor and Netty’s reactive event loop).
- ❌ “We don’t need rate limiting because our microservices scale automatically.” (Without rate limiting at the gateway, sudden traffic spikes will overwhelm databases before autoscaling completes).
Related Topics
- What is Service Discovery and Why is It Needed — Dynamic endpoint routing
- What is the Difference Between Client-Side and Server-Side Discovery — Edge vs internal balancing
- How to Implement Authentication and Authorization in Microservices — Token architectures
- What is Circuit Breaker Pattern — Gateway circuit breaking