API Design & Gateways
Design clean, idempotent API contracts fronted by a unified gateway for auth, routing, and throttling.
In plain words
An API is the menu of actions other programs can ask your service to do. An API gateway is the front desk that checks who you are and forwards your request to the right service.
Think of it like…
An airport security & passport checkpoint (API Gateway) inspecting tickets once before passengers walk to specific airline gates (microservices).
Where you’ve seen it
A mobile app calls GET /orders/123. The gateway checks the login token, applies rate limits, and forwards the call to the orders service.
Words you’ll hear
- REST
- An API style using URLs and HTTP verbs like GET and POST.
- gRPC
- A fast binary API style, popular between internal services.
- Idempotency
- Repeating the same request has the same effect as doing it once.
- Pagination
- Returning large lists in pages instead of all at once.
Visual diagram
Theory & deep dive
Clean API habits every backend needs
Three design details separate fragile APIs from rock-solid production APIs: Cursor pagination (never OFFSET 100000 on growing tables), Idempotency keys on mutations (POST /payments with Idempotency-Key header so mobile retries never double-charge), and explicit API versioning (/v1/).
How it works, step by step
Step 1 of 41. Centralize cross-cutting concerns at the API Gateway
Terminate TLS, verify JWT/API keys, apply rate limits, log request IDs, and route /v1/orders to the right internal service.
- Responsibilities
- Auth, RL, Routing
- Avoid
- Heavy orchestration
When to use it & trade-offs
What it is
API design covers resource modeling, pagination, versioning, and idempotency across REST, GraphQL, or gRPC, while an API Gateway centralizes cross-cutting edge concerns like auth, rate limiting, and routing.
Why it matters: Poor pagination (OFFSET) collapses at scale, non-idempotent POSTs double-charge users on retries, and duplicating auth/throttling in 30 microservices creates security drift.
When to reach for it
- API Gateway for centralized JWT auth, TLS, CORS, request correlation IDs, and rate limiting
- Cursor-based pagination on every infinite-scroll feed, timeline, or search listing
- Idempotency keys on every payment, order creation, or async job trigger endpoint
When to avoid or be careful
- Putting complex multi-service business joins inside the API Gateway
- Using
OFFSETpagination on live feeds where items insert/delete continuously
REST vs. GraphQL vs. gRPC
Strengths: Simple, cacheable via HTTP semantics, universal browser/curl support.
Watch out: Can suffer over-fetching or N+1 round trips for complex nested screens.
Strengths: Clients fetch exact fields in one request; strongly typed schema.
Watch out: Harder HTTP caching, N+1 resolver traps (need DataLoader), query complexity attacks.
Strengths: Compact binary Protobuf, HTTP/2 multiplexing, strict contracts.
Watch out: Not natively browser-callable without gRPC-Web/REST transcoding.
Interview signals & numbers to know
When this topic comes up
- infinite scroll
- mobile retries
- public developer API
- API gateway
- GraphQL vs REST
Benchmarks & rules of thumb
- Gateway hop overhead
- ~1–3 ms
- Default page limit
- 20–100 items
- Idempotency key retention
- 24–72 hours
- GraphQL depth limit
- Max 5–10 levels
Common mistakes & follow-ups
Common interview mistakes
- Turning the API Gateway into an unmaintainable smart orchestration layer with DB connections
- Returning
200 OKon async jobs that actually take 30 seconds - return202 Acceptedwith a status/poll URL or webhook - Allowing unbounded
?limit=1000000queries that OOM your service
Likely interviewer follow-ups
- Why does cursor pagination prevent duplicate items on a live feed compared to OFFSET pagination?
- How do you implement an Idempotency-Key store safely under concurrent duplicate retries?
- What is the Backend-for-Frontend (BFF) pattern and when is it useful?
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply API Design & Gateways inside real High-Level Design prompts or review Q&A flashcards.