Interview's MateSystem Design
Interview's Mate

System Design

Architecture & trade-offs

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
System Design topics
Lesson 19/ 31
Distributed Systems & Architecture

API Design & Gateways

Design clean, idempotent API contracts fronted by a unified gateway for auth, routing, and throttling.

BeginnerGateway · Cursor · Idempotency3 quiz questions0/2 subtopics done
Practice in AI HLD
In plain wordsDiagramTheorySubtopics (2)Step by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice
Deep-dive subtopics (2) - 0/2 completed
REST vs. GraphQL vs. gRPC

Pick the right API paradigm for public APIs, mobile BFFs, and internal microservices.

Beginner
Cursor Pagination & Idempotent APIs

Why OFFSET slows down on large tables and how idempotency keys make retries safe.

Intermediate

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

Loading interactive diagram
Loading interactive exercise

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/).

Key takeaway: Put Auth, Rate Limiting, and Routing in the API Gateway; put Idempotency and Cursor Pagination in every core endpoint.

How it works, step by step

Step 1 of 4
Edge Gateway

1. 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.

Keep business logic out of the gateway to avoid a monolith bottleneck
Key numbers at this step
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 OFFSET pagination on live feeds where items insert/delete continuously

REST vs. GraphQL vs. gRPC

REST (HTTP + JSON)
Best for: Public APIs, resource CRUD, CDN-cacheable GET endpoints

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.

GraphQL
Best for: BFF (Backend-for-Frontend) aggregating many entities for web/mobile UIs

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.

gRPC
Best for: Internal service-to-service RPCs and streaming

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 OK on async jobs that actually take 30 seconds - return 202 Accepted with a status/poll URL or webhook
  • Allowing unbounded ?limit=1000000 queries 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?
Interview tip: Explicitly mention Idempotency-Key headers on write endpoints and cursor pagination on feeds - both score huge practical points.

Knowledge check

Loading quiz

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.

Open AI HLD Practice →SD Interview Q&A
Previous topic
Distributed Locks & Unique IDs
Next topic
Microservices & Service Mesh