Unit 5 of 13 · about 4 hours · explain, implement
Request IDs, client incarnations and idempotency keys
Retries replay requests. Without a key that names the intent, a retried create becomes two resources and a restarted client replays someone else's effect.
You'll be able to
- Explain why a message ID alone cannot separate a retry from a new intent after a client restarts.
- Implement first-write-wins execution keyed on caller plus idempotency key with parameter-mismatch rejection.
- State the retention bound on duplicate-suppression state and what happens after it expires.
Covers
Read
In this order, only the parts listed
0 of 2 done
- AWS making retries safe with idempotent APIsAWS Builders' Library - Making Retries Safe with Idempotent APIs
Caller-provided client request identifier pattern, validation error on same key with different parameters, semantically equivalent retry responses, identifier retained only for a bounded interval
- MIT 6.5840 Lab 2 key/value server and lockMIT 6.5840 - RPC and Threads plus Lab 2 Key/Value Server
Versioned
PutwithErrVersionandErrNoKey; lock built onPutandGetwith per-client identity fromRandValue(8)
Focus on
- Identity has three parts: which client incarnation sent it, which request of that incarnation, and which side effect the request intends. One UUID for all three conflates them.
- The server stores the key plus the request parameters plus the result. A repeat with the same key and same parameters returns the stored result; the same key with different parameters is a validation error, never a second execution.
- The Systems unit on deadlines, retries and idempotency across RPC built the durable key state machine with atomic transitions. Here the new work is identity across client restarts, parameter-mismatch rejection and the retention bound.
- Retention is bounded by policy such as resource lifetime plus an interval. After expiry, a late duplicate looks new, so callers must handle that window.
Common mistakes · 3
- Hashing parameters to invent the key. Two identical creates may be two intents, and two different parameter sets may be one retried intent; only a caller-provided key expresses intent.
- Returning AlreadyExists on a retry and calling it idempotent. The side effect is single, but the caller cannot tell whose resource exists, so default retry loops break.
- Keeping keys forever to be safe. Unbounded state grows without limit and old keys can collide with future intents; bound the interval and document it.
Try it yourself
Closed-book, before checking anything
execute(key, proposed) store with first-write-wins semantics, parameter-mismatch rejection and a TTL, and show a retried create returning the first result while a changed-parameter repeat raises a validation error.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.A client sends create with key
a, times out, and retries with keyaand identical parameters. What must the server return?AnswerThe stored first result, semantically equivalent to the first response. No second resource is created.
2.The retry reuses key
abut changes the instance type. What must the server do?AnswerReject with a validation error for parameter mismatch. The key names one intent, so changed parameters signal a different intent, not a retry.
3.Why is a per-message UUID without a client incarnation insufficient after a restart?Answer
A client that numbers requests from 1 again after a restart collides with its old numbers, so the server suppresses a new request as a duplicate. A client that lost an unacknowledged request retries it under a fresh ID, so it runs twice. An incarnation number separates old numbering from new, and a key saved with the intent before the first send lets the retry reuse it.
4.Why must key recording and the mutation be one atomic step?Answer
A crash between the two leaves either a recorded key with no effect, which wrongly replays as done, or an effect with no key, which wrongly executes twice.
Practice
- Deduplicate Idempotent Requests
Implement
execute(key, proposedResult)first-write-wins; the judge replays keys with different proposed resultsThe judge holds keys for the test lifetime with no expiry. Add a TTL and a parameter-mismatch error in your own store and state what the judge does not establish.
- Build an Idempotent Consumer
Implement
process(messageId)returning true once then false on redeliverySame harness shape. Extend it with caller-plus-key identity and a bounded retention test outside the judge.
You're done when
Your store replays the first result on identical retries, rejects changed parameters, expires keys on schedule, and you can name the atomicity boundary.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 14 more in the section
- MIT 6.5840 - RPC and Threads plus Lab 2 Key/Value ServerMIT PDOS · required
Read the Lecture 2 notes (
notes/l-rpc.txtandnotes/kv.go) on RPC failure and at-most-once execution, then do Lab 2: a versionedPutkey/value server whose client returnsErrMaybewhen a retriedPutmay already have run, and a lock built on it. Keep the course tests, then add cancellation, size-limit and mixed-version faults in your own harness. - AWS Builders' Library - Making Retries Safe with Idempotent APIsAmazon Web Services · support
Read the caller-provided client request identifier pattern, the validation error for a repeated identifier with different parameters, the semantically equivalent response to a retry and why the identifier is retained only for a bounded interval. Compare that interval with your own duplicate-suppression state.
Also in this section
- DDIA 2e - Chapter 5 Encoding and EvolutionMartin Kleppmann and Chris Riccomini · primary
Read formats, schema evolution, service RPC, durable workflows and event-driven dataflow. Write a four-version compatibility table and connect each compatibility claim to a wire fixture.
- gRPC Guides - Deadlines, Cancellation, Status Codes, Retry and Flow ControlgRPC Authors · required
Read the deadlines, cancellation, status codes, retry (enabled by default with no default policy; no retry once response headers arrive), keepalive and flow-control guides. Turn each into a rule your own RPC layer follows, and check a claim against grpcio before you rely on it.
- Protocol Buffers and Avro - Unknown Fields and Schema ResolutionGoogle and the Apache Software Foundation · required
Read the proto3 guide on field numbers, defaults and unknown fields (kept through binary parsing and reserialization, lost through JSON) and the Avro 1.12.0 schema resolution rules (https://avro.apache.org/docs/1.12.0/specification/). Build round-trip fixtures that show each rule.
- Building Distributed Systems - The MessengerBuild Distributed Systems · support
Track 1 is free: do the RPC subtrack (sync RPC with timeout, timeout and retry loop, async callbacks, callback reaper, exponential backoff) and the deduplication task. Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.
- DSLabs - Lab 1 Exactly-Once RPCUniversity of Washington (Ellis Michael) · support
Labs are Java 21 message-passing state machines tested on an emulated network and by model-checking search tests that print a minimal violating trace; run them with the
run-tests.pyscript and its--laband--partoptions. Lab 1 builds an "exactly-once" RPC abstraction for an unreplicated key/value server on a network that drops, duplicates and reorders messages. Use it to check your request identity and duplicate-suppression design against exhaustive orderings. - HLD Handbook - Networking, API Design and BackpressureHLD Handbook · required
Read only Part 0 networking and API design, then the load balancer, service discovery, rate limiting and reliability chapters needed for the protocol design. Use the diagrams to review boundaries, not as a substitute for fault tests.
- AWS Builders' Library - Timeouts, Retries and Backoff with JitterAmazon Web Services · required
Read the whole article for timeout choice, retries at one layer, retry amplification across hops, capped exponential backoff and jitter. Derive the request multiplier for retries at every layer of a call chain before writing your retry policy.
- Building Distributed Systems - The NetworkerBuild Distributed Systems · optional
Only the first three tasks are free (TCP echo, connection pool and backlog, graceful shutdown and drain). Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.
- Beej's Guide to Network ProgrammingBrian Hall · support
Use the client, server, send, receive, select or poll, and partial-transfer sections when socket behavior is unclear. This is a protocol mechanics reference, not the distributed-systems spine.
- AlgoMaster System Design - Selected Networking and API ProblemsAlgoMaster · optional
Use the problems on idempotency and retries (Deduplicate Idempotent Requests, Build an Idempotent Consumer, Classify Retry Outcomes), rate limiting (sliding-window log and token bucket), the Least Requests load balancer and the queue visibility timeout. For every solved component, add an external fault test and explain what its simplified judge does not establish.
- Replicated Log and Stream Processor - Protocol SliceRoadmap-Final · support
Create the repository, message schema, request and node identities, deadline and retry contract, deterministic network scheduler and raw event format. Keep this same artifact through DS-9.
- RFC 9000 - QUIC Version 1IETF · reference
Read Section 2 (streams), Section 4 (flow control) and Section 9 (connection migration) for QUIC transport behavior. No implementation; compare with TCP behavior from the Systems stage.
- RFC 9114 - HTTP/3IETF · reference
Read Sections 3 (connection setup), 6 (stream mapping) and 7 (framing) for how HTTP semantics map onto QUIC streams.
- W3C Trace ContextW3C · reference
Read Section 3.2 (traceparent fields), Section 3.3 (tracestate) and Section 4 (processing model) for trace correlation without synchronized clocks.