Highlights

Contents

Part I
01

Resource modeling and HTTP methods

We give each request a stable meaning. An endpoint that leaks implementation details becomes hard to evolve and hard to recover. We will model resources, commands, methods, and validation before drawing service boxes.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Resource modeling andHTTP methods2Pagination, filtering andsorting3Versioning andidempotency keys4Auth, validation and APIgateway5Monoliths andmicroservices6Sagas and distributedtransactions7Case study: one API endto end

#Section 1Begin with the user’s action

A viewer opens a match, and a scorer corrects its latest score. Both touch the same match but ask for different effects. The viewer needs a representation; the scorer requests a state transition. An API design starts by saying what each actor can do and what a successful response means, before choosing paths or field names.

A resource model identifies the concepts the application exposes and the relationships among them. Heron has matches, events, clips, and scorer assignments. A match’s public identity should remain meaningful if its database owner changes. A client should not need to know the storage shard or internal function that supplies it.

ONE RESOURCE, /matches/m7, SEEN BY A VIEWER AND BY A SCORERONE RESOURCE, /matches/m7, SEEN BY A VIEWER AND BY A SCORER/matches/m7stable identitynot a table nameviewerGET: score JSONread permissionscorerPOST correctionwrite permissionstorage can change underneath
Figure 10.1Illustrative match resource and actor-specific operations. Public identity and permission are distinct decisions.

The invariant behind the action

An invariant is a rule that must remain true across accepted changes. Heron requires that only an assigned scorer can correct a match, that event sequence never moves backward, and that repeating one command does not create another correction. Those rules determine the write boundary more directly than a preference for REST or microservices.

A response called ‘accepted’ must say whether the effect is already committed or merely durably queued. A response called ‘complete’ must say which state and derived copies are ready. Naming those boundaries prevents clients from inferring a stronger guarantee than the implementation provides.

#Section 2Model relationships without exposing storage accidents

A clip belongs to a match, but media bytes live in object storage. A score event refers to a scorer, but a user’s display name can change. The API should describe those relationships according to product meaning rather than copying every foreign key and column name into the public response.

A representation is the data returned to describe a resource. It can contain stable identifiers, selected embedded values, and links to related resources. Embedded values need a freshness contract: a historical scorer-name snapshot and the user’s current display name answer different questions. The representation should make that distinction clear.

MATCH, EVENT, CLIP AND ASSIGNMENT ARE LINKED, BUT EACH RULE HAS ONE OWNERMATCH, EVENT, CLIP AND ASSIGNMENT ARE LINKED, BUT EACH RULE HAS ONE OWNERmatch m7event e9owner: score serviceclip c4owner: media servicescorer assignmentowner: identity servicestoring two things together does not decide who owns the rule between them
Figure 10.2Illustrative related resources. Storage location is separate from ownership of a business rule.

A tiny example

A match response can contain match_id, current score, observed version, and identifiers for published clips. A clip response can contain its processing status and a suitable delivery reference. An unfinished clip should not appear as playable merely because its upload bytes exist. The API exposes state transitions that clients can interpret, not just records that happen to be present.

BYTES UPLOADED, JOB ACCEPTED AND PLAYABLE ARE THREE DIFFERENT CLIP STATESBYTES UPLOADED, JOB ACCEPTED AND PLAYABLE ARE THREE DIFFERENT CLIP STATESuploadedbytes presentnot publishedprocessingjob acceptedvariants pendingreadychecked variantsplayable link
Figure 10.3Illustrative clip lifecycle. Uploaded bytes, accepted processing, and a published representation prove different states.

When related values change independently, avoid returning a falsely atomic-looking document unless you can supply that observation contract. A combined page can explicitly contain a current authoritative score and eventually refreshed clip suggestions. One JSON response does not prove every field was read at one globally consistent instant.

#Section 3Method semantics affect retries and intermediaries

A browser can prefetch an ordinary link without asking the user whether it should change the score. HTTP’s safe method semantics mean the client requests information rather than a state-changing action. GET should therefore retrieve a representation, while a correction uses an explicit write or command contract. Incidental logging does not turn a retrieval into a requested score mutation.

Idempotent semantics mean repeating the intended operation has the same intended effect. Repeated DELETE can leave the resource absent while returning a different status on a later attempt. Repeated PUT can request the same replacement state. POST can be made duplicate-safe by an application command key, but the method itself does not supply that promise.

GET READS, PUT REPLACES, POST SUBMITS, DELETE REMOVES; ONLY POST NEEDS A KEY TO RETRYGET READS, PUT REPLACES, POST SUBMITS, DELETE REMOVES; ONLY POST NEEDS A KEY TO RETRYGETread the representationsafePUTreplace named stateidempotent: twice = once= 3= 3POSTsubmit a commandneeds an operation key+1+1?DELETEremove named statesecond reply may be 404204404
Figure 10.4HTTP method meaning and application effect recovery. The method name does not make an unrelated provider transactional.

State replacement needs concurrency rules

Suppose two scorers read the same version and each submits a replacement. Without a version precondition, the later accepted replacement can erase a correction the client never observed. Define whether the operation means ‘replace unconditionally,’ ‘replace the version I read,’ or ‘apply this explicit delta to current state.’ Those are different contracts even when each request uses the same method.

The HTTP semantics specification defines the method vocabulary. Heron’s permission, version checks, and command identity enforce its business meaning. Keep those layers separate when explaining what a client may safely retry.

#Section 4Validation has more than one boundary

A request can be valid JSON and still ask for an impossible score. Request validation checks the shape, types, ranges, and semantics of input before expensive or irreversible work. Bound the body size and parsing effort first, then validate fields, then enforce permissions and state-dependent invariants at the appropriate authority.

Shape checks can reject a missing match identifier or unsupported command kind. A semantic check can reject a negative total where the sport’s rules forbid it. A state-dependent check can reject an event sequence older than the current committed state. Performing the last check outside the write transaction creates a race if another command changes the state before commit.

CHEAP CHECKS FIRST; THE FINAL INVARIANT CHECK SITS WITH THE EFFECTCHEAP CHECKS FIRST; THE FINAL INVARIANT CHECK SITS WITH THE EFFECTbounded parse≤ 16 KBfield typesruns: int 0–6permission + ruleassigned scorer?state changeseq > currentmost bad requests stop on the left, before any expensive worksyntactically valid is not authorized
Figure 10.5Illustrative validation layers. The final invariant check belongs with the protected effect, not only at the gateway.

Errors are part of the contract

Return a stable machine-readable error kind, enough safe detail to correct the request, and a trace identifier for diagnosis. Do not leak internal credentials or stack traces. Distinguish malformed input, unauthorized access, conflict, overload, and unknown completion so the client can choose correction, login, retry, or recovery appropriately.

The problem-details specification provides a standard shape for HTTP API error information. A shape alone does not decide whether an error is retryable. That decision comes from the operation’s state and failure meaning.