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.
Builds on Polling, long polling, WebSockets, and server-sent events, Functional and non-functional requirements.
- 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 atGET /reservations/42. - Named operation: call
ReserveSeat(show, seat)and laterGetReservation(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
- A read returns state without creating another reservation. This preserves the meaning of a GET.
- A repeated defined DELETE asks for the same removal effect. The first response and a later “already absent” response may differ.
- A create needs explicit retry handling. Neither POST spelling nor an RPC function name automatically remembers the earlier result.
- 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:
- The service authenticates the caller and checks the request. The service looks up
K7together with the verified account and the reservation-create operation. Another account’sK7, or a payment operation with that text, is not this request. - 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 concurrentK7attempts creating separate outcomes. A transaction commits the required records together or not at all; a losing concurrent attempt reads the committed result. - The reply is lost. The client does not know whether
A12was reserved. Timeout is not proof that the transaction rolled back. - The client retries
K7withA12again. The service looks upK7, verifies matching content and returns its saved reservation result. It does not allocate another seat. - The client accidentally uses
K7forB14. 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
Follow the numbered arrows
- Reservation client → Public APIrequest one seat reservation
- Public API → Reservation servicepass caller identity and remaining time
- Reservation service → Saved request resultsave reservation and request result together
- Reservation client → Public APIretry the same key and fields
Trace the diagram in words
- Either 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.
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.
Compare the worked solution
- Define a catalog read whose response states whether caching and validation are permitted. Reading must not create a payment.
- The payment request includes verified account, amount, currency and a stable request key; success returns payment ID and state.
- 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.
- 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.
- 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?
- REST organizes resources through an interface with consistent operation meanings.
- RPC exposes named commands with arguments and results.
- HTTP and data encodings are separate choices; neither style supplies retry safety automatically.
CARD 02How is an uncertain create retried safely?
- Retry the same key and fields.
- The service looks up the result saved with the original change.
- Return it instead of creating again; reject changed content under that key.
CARD 03What must an API example specify?
- Show request fields, success and business errors.
- Explain identity, permission and the deadline.
- Show timeout recovery and key conflicts.
- Define compatibility for older clients.
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.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.