CMD Guide
HomeSystem DesignMicroservices Patterns

Introduction to the API Gateway Pattern

Introduction to the API Gateway Pattern

Imagine an e-commerce app whose home screen shows your profile, a cart badge, recommended products, and recent orders. Behind the scenes those live in four different microservices. If the mobile app talked to each service directly, it would need to know four hostnames, hold four TLS connections, attach an auth token four times, and handle four sets of retries and failures. Multiply that by web, iOS, Android, and partner integrations, and every client becomes a fragile map of your internal topology.

The API Gateway is a single server that sits between all external clients and your fleet of internal services. Every request enters through one front door. The gateway authenticates it, decides which internal service (or services) should handle it, forwards the request, and returns the response. Clients stop caring how many services exist or where they live; they see one stable API. This is the microservices equivalent of a hotel front desk: guests talk to one concierge instead of wandering into the kitchen, laundry, and accounting departments themselves.

How it works, precisely

An API Gateway is a reverse proxy with policy logic layered on top. A request flows through a pipeline of stages, most of which are cross-cutting concerns — work that every service would otherwise duplicate:

A richer variant is request aggregation / composition: the gateway calls several services in parallel and merges the JSON into one response, so a chatty screen becomes a single round trip. Push this per-client and you get the Backend for Frontend (BFF) pattern — a dedicated gateway tuned for the mobile app, another for web.

A worked scenario, with numbers

Take the home-screen example at 5,000 QPS peak. Without a gateway, one screen load fans out to 4 direct calls, so clients generate 20,000 QPS of edge traffic and every client re-implements auth, retries, and TLS. Each direct call over mobile networks pays connection setup and its own ~100 ms round trip; done partly serially, the screen can take 300–400 ms just in networking.

Put a gateway in front. The client makes one request, GET /home. The gateway validates the JWT once (~1 ms with a cached signing key), then fans out to the four services in parallel inside the data center, where round trips are ~2–5 ms. Total added latency at the gateway is roughly 5–8 ms, and the screen returns in one ~110 ms round trip (~100 ms cellular RTT + the gateway's tax) instead of four slow ones. Edge QPS drops back to 5,000, and 90% of duplicated auth/retry code disappears from every client. Be precise about what was won: if the client had fired its four calls in parallel, the latency gap mostly evaporates — the durable wins are one TLS handshake and radio wake-up instead of four, auth validated once at the edge, and a server-side deadline the client cannot enforce. Quote those in an interview, not the milliseconds.

The cost is a new hop that must be sized for the full 5,000 QPS and made highly available. In practice you run the gateway as a horizontally scaled, stateless tier (say 6–10 instances behind an L4 load balancer) so any node can die without dropping the door.

Trade-offs, and when to use vs not

Use an API Gateway when you have multiple services behind one product, several client types (mobile/web/partner), and you're tired of duplicating auth, rate limiting, and TLS in every service. It shines the moment cross-cutting concerns and client-facing API shape start to sprawl.

Named alternatives and how they differ:

The core trade-off: a gateway adds an extra network hop, operational surface, and a place that can become a development bottleneck (every team waits on gateway config) — in exchange for a stable client contract and DRY cross-cutting concerns.

Pitfalls an interviewer probes

Worked trace: fan-out / fan-in aggregation

Imagine the mobile home screen needs profile, cart, recommendations, and recent orders. The gateway exposes one endpoint, GET /home, and fans out to four upstream services in parallel.

TimeProfileCartRecommendationsOrdersGateway action
t0GET /profile (50 ms timeout)GET /cart (50 ms)GET /recs (80 ms)GET /orders (80 ms)Dispatch all four
t0+18 msOK (12 ms)---Hold partial
t0+22 ms-OK (20 ms)--Hold partial
t0+55 ms--OK (50 ms)-Hold partial
t0+80 ms---TimeoutReturn cached recent-orders stub with X-Partial-Response: orders
t0+85 msMerged JSON deliveredScreen loads in ~85 ms instead of failing

Each upstream gets its own timeout. The slowest healthy call (recommendations at 50 ms) sets the floor; the orders timeout caps the tail. Without per-call timeouts, one slow upstream would block the whole screen.

Decision table: auth, rate limit, transform

Cross-cutting concernDo it in the gateway?Why / why not
TLS terminationYesOne place to manage certificates; internal hops can use mTLS or plaintext inside trust boundary
JWT validation / coarse authNYesReject anonymous traffic at the edge; pass trusted identity header downstream
Fine-grained authorizationNoAuthZ depends on domain state; belongs in services (defense in depth)
Rate limiting per clientYesProtects all backends from a single noisy tenant
Request/response transformationSometimesOK for versioning and field mapping; dangerous when it becomes business logic
Request aggregationSometimesGreat for chatty screens; adds coupling between gateway and upstream APIs

Failure trace: backend timeout → circuit open → fallback

StepUpstream stateGateway behaviorUser-visible result
1Orders service healthyProxy normally; record latency/error metricsFull order history
2Orders degrades; 5 % errors, p99 spikesRetry idempotent GET once with jitterOccasional slower load
3Errors exceed 50 % over 30 sCircuit breaker opens; fail fast for 10 sOrders section shows cached top 3 recent orders
4Health probe passesHalf-open probe allowed; circuit closes on successFull history returns
5Hard failure during requestPer-call timeout fires; return partial responseScreen loads with degraded orders section

The key idea is graceful degradation: the gateway does not let one sick upstream break the entire client experience.

Key takeaways

🎯 Drill Ladder — survive the follow-ups

L0 · An API Gateway is a single stateless reverse-proxy front door that centralizes north-south cross-cutting concerns so clients see one stable API.

L1 · ⑥ Cost/Simplicity — "For one client and four services, why not skip the gateway and call each service directly?"
Trap: "Add the gateway anyway — it's always the right pattern, more layers never hurt."
Bar: For a single client type, direct calls really are cheaper — no extra hop, nothing new to operate. The gateway's ROI only appears once you have multiple client types or duplicated auth/TLS/rate-limit code across N clients × M services; only then does trading a single-digit-ms hop for deleting that duplication pay off. connects-to: API Gateway vs Direct Service Exposure

L2 · ② Failure — "The gateway just died at 2am. What's the blast radius, and how did you stop it being your SPOF?"
Trap: "It's one server — put a hot standby behind a failover switch."
Bar: The gateway runs as a stateless fleet (no session affinity, no per-request instance identity) with N≥3 instances spread across multiple AZs behind an L4 load balancer with health checks; losing any instance only drops capacity, never availability. A hot-standby pair reintroduces the exact SPOF — failover lag and split-brain risk — the fleet design exists to remove. connects-to: Fault Tolerance vs High Availability

L3 · ③ Scale — "GET /orders?page=500 through the gateway times out under load; page=2 is instant. Why, and what do you change?"
Trap: "Add an index on the offset column" or "just raise the timeout."
Bar: OFFSET pagination forces the DB to scan and discard page×limit rows before returning results, so cost grows linearly with depth no matter the index, and concurrent writes shift row positions, skipping or duplicating items across pages. Switch to cursor/keyset pagination (WHERE id > last_seen_id ORDER BY id LIMIT k) — O(log n) via the index at any depth, and stable under concurrent inserts. connects-to: API Pagination Strategies — Offset vs Cursor

L4 · ④ Time/Lifecycle — "You need to rename a field the mobile app reads, but that app is 6 months behind on updates. How do you ship it without breaking anyone?"
Trap: "Just change the field and tell mobile to update ASAP" (or the opposite trap: "bump to /v2 for every field tweak").
Bar: Expand-contract: first expand — add the new field alongside the old one and deploy, so both exist; once usage telemetry shows the old field has dropped off (a fixed deprecation window, e.g. 90 days), contract — remove it. Reserve an actual version bump (/v2 route or Accept-Version header) for changes that are truly breaking — removed fields, changed semantics — never for additive ones. connects-to: Evolving APIs & Event Schemas

L5 · ⑤ Adversary/Edge — "Mobile times out waiting on POST /orders, the user taps 'place order' again. Did you just charge them twice?"
Trap: "TCP retries are safe, and the client only retries once, so it's fine."
Bar: HTTP/TCP retries are at-least-once, never exactly-once. The client must send a client-generated Idempotency-Key header on the retry, and the gateway or order-service persists a request-id → response mapping so a repeated key within a TTL window returns the cached original response instead of re-executing the write — otherwise a timeout-triggered retry double-charges the card. connects-to: Idempotency Keys — Implementing Them Safely

The floor keeps dropping: staff+ perturbation — "the gateway validated the JWT, so downstream service X blindly trusts the X-User-Id header; an internal service-to-service call forges that header and now it's a horizontal privilege escalation." The gateway's authentication is coarse and edge-only; every service must still independently authorize the request and treat identity headers as untrusted unless cryptographically signed or mTLS-bound end to end — services can never trust the network, only defense in depth.

Self-locate: died at L1 → mid-level; L4+ → staff signal.

Facing any new concept? Hit it with the six: concurrent? failing? at 100×? over time? adversarial? worth the cost? — that's the interviewer's whole playbook.


Sources: Chris Richardson, Microservices Patterns (Manning, 2018), ch. 8 — API Gateway and Backend-for-Frontend; Sam Newman, Building Microservices, 2nd ed. (O'Reilly, 2021); Netflix Zuul and Envoy/Kong gateway documentation on filter chains, rate limiting, and aggregation.

🤖 Don't fully get this? Learn it with Claude

Stuck on Introduction to the API Gateway Pattern? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Introduction to the API Gateway Pattern** (System Design) and want to truly understand it. Explain Introduction to the API Gateway Pattern from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Introduction to the API Gateway Pattern** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Introduction to the API Gateway Pattern** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Introduction to the API Gateway Pattern** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes