LearnastraTHE DESIGN COURSE

Make the right trade-off / CHAPTER 35

REST versus remote procedure calls

Compare resource APIs with named commands, then specify fields, results and safe retries for either style.

Intermediate1h of deliberate study10 questions

Builds on Polling, long polling, WebSockets, and server-sent events, Functional and non-functional requirements.

By the end, you can
  • Distinguish architectural style from wire format.
  • Explain resource-oriented and operation-oriented contracts.
  • Preserve deadlines and safe retry behavior.

01Style, protocol, and encoding are separate

A customer wants to reserve seat A12 for a show. The API can present that action in two styles:

  • Resource-oriented: create a reservation through POST /reservations, then read its saved state at GET /reservations/42.
  • Named operation: call ReserveSeat(show, seat) and later GetReservation(42). This is remote procedure call, or RPC: a client invokes a named operation elsewhere.

REST is broader than resource-shaped URLs. It requires a consistent interface, separate client/server roles, stateless requests, explicit caching rules and support for intermediary layers. Reading a reservation must not secretly create another seat claim.

Keep style, transport and data encoding separate. HTTP describes requests and responses. JSON writes values as text; Protocol Buffers is a binary encoding. RPC can use HTTP and JSON too. gRPC is one RPC framework, not all RPC. Either style still needs permissions, compatibility, deadlines and safe retry behavior.

02Design effects before endpoint names

Specify the seat operation before choosing names:

Required behavior Resource interface example Named-operation example
Create a reservation POST /reservations carries show, seat and request key. ReserveSeat(show, seat, requestKey) carries the same intent.
Read a saved outcome GET /reservations/42 reads reservation 42. GetReservation(42) reads it.
Cancel A documented update or deletion requests the cancellation. CancelReservation(42) requests it.

Then define effects

  1. A read returns state without creating another reservation. This preserves the meaning of a GET.
  2. A repeated defined DELETE asks for the same removal effect. The first response and a later “already absent” response may differ.
  3. A create needs explicit retry handling. Neither POST spelling nor an RPC function name automatically remembers the earlier result.
  4. State required fields and errors. Distinguish invalid input, a known unavailable seat and an unknown result after timeout.

An operation is idempotent when repeating it has the same intended effect as applying it once. Responses need not be identical and the server may still log attempts. A create or charge commonly needs a request key plus a saved outcome to obtain that behavior.

For ordinary public reads, HTTP caching and version validators can help. Internal operation-heavy services may benefit from generated request types or two-way streams. These tools do not replace the effect rules.

03Worked example: reservation timeout

The customer supplies request key K7 for one attempt to reserve A12. Follow a lost response:

  1. The service authenticates the caller and checks the request. The service looks up K7 together with the verified account and the reservation-create operation. Another account’s K7, or a payment operation with that text, is not this request.
  2. The database saves the reservation and K7's outcome together. It also retains enough original request content to detect a changed seat under the same key. A unique account/operation/request-key constraint prevents two concurrent K7 attempts creating separate outcomes. A transaction commits the required records together or not at all; a losing concurrent attempt reads the committed result.
  3. The reply is lost. The client does not know whether A12 was reserved. Timeout is not proof that the transaction rolled back.
  4. The client retries K7 with A12 again. The service looks up K7, verifies matching content and returns its saved reservation result. It does not allocate another seat.
  5. The client accidentally uses K7 for B14. Reject that mismatch. A different requested seat is a new intent, not a matching retry.

This sequence works for either interface style.

Also preserve a caller deadline. If 120 ms has elapsed out of 300 ms, a downstream call has at most 180 ms left, including the return path. Cancellation can stop more work, but cannot undo a reservation already committed.

A binary representation may reduce bytes without much response-time improvement when storage, queue waiting or distance dominates. Compare equivalent complete operations rather than choosing a winner from format alone.

REST versus remote procedure calls — decision flow

REST versus remote procedure calls — decision flowEither a resource operation or named command expresses the seat-reservation request. Save the reservation, original request fields and key-to-result record together. A matching retry returns that result; changed request fields under the same key are rejected. Numbered arrows correspond to the connection key below. All connections are directional.ReservationclientReuses one requestkey on retryPublic APIResource request ornamed commandReservationserviceCheck permission andsaved request keySaved requestresultOriginal fields andreservation outcome1234REST versus remote procedure calls — decision flowEither a resource operation or named command expresses the seat-reservation request. Save the reservation, original request fields and key-to-result record together. A matching retry returns that result; changed request fields under the same key are rejected. Numbered arrows correspond to the connection key below. All connections are directional.ReservationclientReuses one requestkey on retryPublic APIResource request ornamed commandReservationserviceCheck permission andsaved request keySaved requestresultOriginal fields andreservation outcome1234REST versus remote procedure calls — decision flowEither a resource operation or named command expresses the seat-reservation request. Save the reservation, original request fields and key-to-result record together. A matching retry returns that result; changed request fields under the same key are rejected. Numbered arrows correspond to the connection key below. All connections are directional.ReservationclientReuses one requestkey on retryPublic APIResource request ornamed commandReservationserviceCheck permission andsaved request keySaved requestresultOriginal fields andreservation outcome1234

Follow the numbered arrows

  1. Reservation client → Public APIrequest one seat reservation
  2. Public API → Reservation servicepass caller identity and remaining time
  3. Reservation service → Saved request resultsave reservation and request result together
  4. Reservation client → Public APIretry the same key and fields
Either API style can reserve a seat. Save the reservation and request result together so a matching retry retrieves the original outcome.
Trace the diagram in words
  1. Either a resource operation or named command expresses the seat-reservation request.
  2. Save the reservation, original request fields and key-to-result record together.
  3. A matching retry returns that result; changed request fields under the same key are rejected.

04Explain a contract a client can depend on

Make the choice reviewable:

  • Resource collections and predictable read behavior can suit broad public APIs.
  • Explicit commands and generated request types can suit operation-heavy internal workflows.
  • Old clients still exist. Define behavior for omitted new fields and unknown enum values. Code generation does not update deployed mobile applications.
  • Show one request and result. Name its success identity, business rejection, uncertain timeout, request key and deadline.
  • Document read caching and command repetition separately. State which account and operation share a retry key, rather than assuming one global key space.

Neither style is inherently faster, safer or more scalable. Explain what a caller does after uncertainty, and show the saved records that make that behavior possible.

DRAW. CALCULATE. EXPLAIN.

Specify an uncertain payment

20 minutes · guided practice

Define a catalog read and a payment create using resource or named-operation style. The payment commits but its response is lost. The caller then retries and accidentally changes the amount. State required fields, outcomes and conflict handling.

Your self-check
Compare the worked solution
  1. Define a catalog read whose response states whether caching and validation are permitted. Reading must not create a payment.
  2. The payment request includes verified account, amount, currency and a stable request key; success returns payment ID and state.
  3. Save a payment-intent record containing those fields, the request key and a stable provider-operation ID before contacting the payment provider. Use a provider that recognizes that ID on retries or can report its outcome; then save the returned payment state. A local database transaction cannot include an unrelated provider’s charge.
  4. After a timeout, retry the same key and fields to recover the saved payment ID. Query that payment’s state and let the service resolve any uncertain provider operation using its saved provider-operation ID. Do not create a fresh charge merely because the response was lost.
  5. Reusing the key with a changed amount is a conflict. Preserve the caller deadline without claiming that cancellation erases a committed payment.

The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.

RETRIEVE BEFORE YOU REVEAL

Keep the important ideas.

Say the answer aloud, then turn the card over.

CARD 01What differs between REST and RPC?
  1. REST organizes resources through an interface with consistent operation meanings.
  2. RPC exposes named commands with arguments and results.
  3. HTTP and data encodings are separate choices; neither style supplies retry safety automatically.
CARD 02How is an uncertain create retried safely?
  1. Retry the same key and fields.
  2. The service looks up the result saved with the original change.
  3. Return it instead of creating again; reject changed content under that key.
CARD 03What must an API example specify?
  1. Show request fields, success and business errors.
  2. Explain identity, permission and the deadline.
  3. Show timeout recovery and key conflicts.
  4. Define compatibility for older clients.
Practise this topic in the revision deck ↗

CHECK THE REASONING

Can you explain the choice?

Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.

1 / 10The API sends JSON over HTTP. What does that establish?
2 / 10Which pair can represent the same reservation intent?
3 / 10K7 reserved A12, but the reply was lost. What does timeout mean?
4 / 10A matching retry uses K7 for A12. What should prevent a second reservation?
5 / 10The caller reuses K7 for B14. What should the contract do?
6 / 10A downstream call starts after 120 ms of a 300 ms deadline. What budget applies?
7 / 10What does idempotency constrain?
8 / 10Why can generated RPC types still need evolution rules?
9 / 10Which use case most directly benefits from HTTP validators?
10 / 10A binary API saves payload bytes but latency barely changes. Why is that plausible?
0 of 10 answered

CLOSE THE LOOP

Explain it without the page.

Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.

Go to the source