All academies
Roadmap
Level 08Communication 30 min

REST Communication

Designing clean REST APIs between services: resources, status codes, versioning, idempotency, and pagination.

REST in one paragraph

REST models your API as resources (nouns) manipulated with HTTP verbs. It rides on ubiquitous HTTP infrastructure — load balancers, caches, gateways all understand it — which is why it remains the default for service-to-service and public APIs.

Resource-oriented design
GET    /orders            # list (filter: ?status=PLACED&page=2)
POST   /orders            # create
GET    /orders/42         # read one
PATCH  /orders/42         # partial update
DELETE /orders/42         # delete
POST   /orders/42/cancel  # action that isn't CRUD → sub-resource verb

Status codes that matter

Using precise status codes lets clients and infrastructure react correctly:

  • 200 OK / 201 Created / 204 No Content — success variants
  • 400 Bad Request — malformed input; don't retry unchanged
  • 401 vs 403 — unauthenticated vs unauthorized
  • 404 Not Found — resource absent (also used to hide existence)
  • 409 Conflict — version/state conflict, e.g. optimistic locking
  • 429 Too Many Requests — rate limited; honor Retry-After
  • 503 Service Unavailable — transient; safe to retry with backoff

Idempotency: the retry safety net

Networks fail after the server processed a request but before the client got the response. Clients retry — so your API must tolerate duplicates.

GET, PUT and DELETE are naturally idempotent. POST is not, so payment-style endpoints accept an Idempotency-Key header: the server stores the first response per key and replays it for duplicates.

Idempotent payment creation
POST /payments
Idempotency-Key: 7c9e6679-7425-40de-963d-476d0462fb51

{ "orderId": "42", "amount": 1999 }

# First call  → 201 Created, charge executed
# Retry       → 201 Created (replayed), NO double charge

Versioning and evolution

Between microservices, breaking changes are deployment coupling in disguise. Prefer additive, backward-compatible evolution: add optional fields, never repurpose or remove existing ones (the tolerant-reader principle).

When you must break, version explicitly (URI /v2/orders or an Accept header) and run both versions until consumers migrate.

Key takeaways

  • Model APIs as noun resources with HTTP verbs; use sub-resources for non-CRUD actions.
  • Precise status codes let clients and infra distinguish retryable from permanent failures.
  • Idempotency keys make POST safe to retry — mandatory for payments.
  • Evolve APIs additively; version explicitly only when breaking is unavoidable.

Knowledge check

Q1Multiple choice

A client retries a POST /payments after a timeout. What prevents double charging?

Q2Debugging

A downstream service returns 429. What should the calling service do?

Q3Multiple choice

Which change to an API response is backward compatible?

Answer all questions to submit