Skip to the page

Microservices and APIs · mid

REST API design, gRPC and API versions

Design resources and status codes clearly, make POST safe to retry with an idempotency key, use cursor pagination, pick gRPC inside and REST outside, and change APIs by adding fields first.

The notes

What you must know

  • Orders API: POST /orders returns 201 with Location, GET /orders/{id}, GET /orders with status, from, cursor and limit, and cancel as POST /orders/{id}/cancel (an action with rules) or a validated PATCH.
  • Idempotency-Key header on POST, stored with the result, so a retry returns the same order. The same key with a different body is rejected (422 in the IETF draft).
  • Cursor pagination with an opaque cursor over (created_at, id) and a capped limit, not offset, for large changing lists.
  • Errors have one consistent JSON shape, for example RFC 9457 problem details (type, title, status, detail), plus an error code and request id. No stack traces.
  • Codes: 400 or 422 invalid input, 401 not signed in, 404 missing and also for someone else order (so existence does not leak), 409 state conflict such as already cancelled, 429 rate limit. Every query is filtered by the caller user id.
  • gRPC: HTTP/2, protobuf, strict contracts with generated code, streaming and deadlines passed down the chain. REST with JSON works everywhere and GET can be cached. Use gRPC between internal services; REST or GraphQL for mobile and partners.
  • Protobuf: never reuse or renumber field numbers; add new optional fields; mark removed numbers reserved.
  • Changing an API old app versions still call: add a new field (price with value and currency) and keep amount. If the shape must change, make /v2. Know callers from logs by app version, keep a minimum app version, send deprecation and sunset headers, and remove only when traffic is near zero. Check breaking changes in CI and use consumer-driven contract tests such as Pact.

The red pen

Common traps: mistake → fix

  • Mistake: GET /cancelOrder?id= is fine

    Fix: GET must not change state.

  • Mistake: Offset pagination works for huge tables

    Fix: it is slow and unstable.

  • Mistake: 200 with an error in the body is fine

    Fix: use real status codes.

  • Mistake: Change the type of a field in place

    Fix: old clients break; add a new field.

Easy to hard

Questions interviewers ask, easy to hard

  1. Design a REST API for orders.
  2. How do you make POST /orders safe to retry?
  3. gRPC or REST between services?
  4. How do you change an API that old apps still call?
  5. Why can a gRPC service overload one pod?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview