System design concepts · Talking Over the Network

API Design for System Design Interviews: REST vs gRPC vs GraphQL

An API is a promise. Once clients depend on it, changing it is expensive, so design it as if you'll support it for five years.

What you'll be able to explain
  • Design clean REST resources and use HTTP semantics correctly
  • Choose between REST, gRPC and GraphQL
  • Implement cursor pagination
  • Make writes safe to retry with idempotency keys
  • Version and evolve APIs without breaking clients

REST done right

REST models the world as resources (nouns) manipulated with a small set of methods (verbs).

GET    /v1/restaurants?city=pune&cuisine=thali    list (filter via query)
GET    /v1/restaurants/42                         read one
POST   /v1/orders                                 create (server assigns id)
PATCH  /v1/orders/9f2c                            partial update
PUT    /v1/users/7/address                        replace whole resource
DELETE /v1/cart/items/5                           remove
POST   /v1/orders/9f2c/cancel                     action that isn't plain CRUD
Method Safe (no side effects) Idempotent (repeat = same result)
GET, HEAD Yes Yes
PUT, DELETE No Yes
PATCH No Not necessarily
POST No No — needs an idempotency key

Status codes worth memorising

Code Meaning Use when
200 / 201 / 204 OK / Created / No content Success variants
400 Bad request Validation failed
401 / 403 Unauthenticated / Forbidden No valid identity / identity lacks permission
404 / 409 Not found / Conflict Missing resource / version or state conflict
422 Unprocessable Well-formed but semantically invalid
429 Too many requests Rate limited; include Retry-After
500 / 502 / 503 / 504 Server errors Bug / bad upstream / overloaded / upstream timeout

Return errors in one consistent shape: {"error": {"code": "SEAT_TAKEN", "message": "...", "request_id": "..."}}. Machine-readable codes let clients react; request IDs let support find logs.

REST vs gRPC vs GraphQL

REST + JSON gRPC + Protobuf GraphQL
Best for Public APIs, web Service-to-service, mobile on poor networks Varied clients needing different shapes
Payload Text, human-readable Binary, compact, fast JSON, client picks fields
Contract OpenAPI (optional) .proto (mandatory, typed) Schema (typed)
Streaming SSE / WebSocket Built-in bidirectional Subscriptions
Caching HTTP caching works great Custom Harder (single POST endpoint)
Pitfall Over/under-fetching Not browser-native N+1 resolvers, expensive queries
Say it in the interview

"Externally I'd expose REST because it's cacheable and universally supported. Internally between services I'd use gRPC for typed contracts and lower latency. If we had many client types with different data needs, a GraphQL layer could sit at the edge."

Pagination: offset vs cursor

Offset (?page=500&size=20) is simple but the database must scan and skip 10,000 rows, and if new items arrive, pages shift and users see duplicates.

Cursor (?after=eyJpZCI6OTg3fQ&limit=20) encodes the position of the last item seen (e.g. created_at, id). The query becomes WHERE (created_at, id) < (:t, :id) ORDER BY created_at DESC, id DESC LIMIT 20: fast at any depth and stable while data changes.

CLIENTClientSERVICEFeed APIDATABASEDB index(created_at, id)GET ?after=cursorrange scan, LIMIT 20
Cursor pagination: each response carries the cursor for the next page.
Tip

Make cursors opaque (base64 of a small JSON). Clients can't depend on their structure, so you can change the implementation later.

Idempotency: making retries safe

Networks fail after the server has done the work. The client times out, retries, and the customer is charged twice. The fix is an idempotency key: a unique ID the client generates per logical operation.

AppPayments APIIdem storeLedger1POST /charges Idempotency-Key: k-81f2insert k-81f (status=processing)3charge ₹4994ok5k-81f → result6response lost (timeout)7retry, same key k-81f8k-81f exists → result9201 (same charge, not a new one)
Idempotent POST: the retry returns the stored result instead of charging again.

Implementation details that matter:

  • Insert the key with a unique constraint before doing work, so two concurrent retries can't both proceed.
  • Store the response with the key; return it verbatim on retries.
  • Scope keys per user/merchant and expire them after 24–72 hours.