Idempotence retries and rollback
A timeout does not mean failure
A mock ticket API may commit a note before its response is lost. Sending the same request as a new operation can duplicate the note. Idempotence means repeating the same logical operation has the same intended effect as performing it once. Bind a unique operation key to a canonical payload and scope; reuse it for the same intent, reject key reuse with a different payload. Store the result durably in a real design; this lab uses only a local dictionary.
Retry only within a budget
Classify errors before retrying: malformed input and denied access need correction, not repeated calls. A transient failure or rate limit may permit bounded retries if the operation is safe to repeat and the total deadline allows it. Respect server retry guidance; use capped exponential backoff and jitter to spread load. For this dry run, the waiting schedule is deterministic (2, 4 seconds) with at most three attempts including the first; no sleeping or API calls occur.
Work through uncertain outcomes
If operation K1 commits a classroom note and times out, the next request with K1 and the same payload reads the stored result. It must not append a second note. If K1 carries a changed payload, return conflict. A request without an idempotency key is invalid in this model. Real systems need atomic storage, concurrent request handling, key expiration and reconciliation; a dictionary alone does not establish distributed exactly-once execution.
Rollback and compensating actions
Rollback restores a previous state where safe; compensation is a separate corrective operation for effects that cannot be undone directly. Never erase audit history to make a run look successful. Before proposing compensation, verify the current version still matches the operation result, the rollback is authorized, and evidence is preserved. If another actor has changed the record, stop and request reconciliation. Some effects, such as disclosed information, cannot be reversed.
Practice: replay the mock requests
Replay requests.json in order into a local dictionary of operation keys and payloads. Treat the first commit with lost response as committed but uncertain to the caller. Classify retries and conflicts and count actual notes. For retry-cases.json calculate attempts and planned waits, stopping on permanent error or budget exhaustion. Review rollback proposals separately; no external actions occur.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Idempotence retries and rollback — knowledge check
Evaluate workflow safety, retry behavior and metric interpretation.
What can a timeout after commit mean?
Same key and different payload should yield what?
How should a permanent authorization error be handled?
What reduces synchronized retry bursts?
What should occur if record version changed before rollback?