Objective & prerequisites
Make one local state transition repeatable without claiming global exactly-once delivery. Prerequisites: Python functions, JSON, basic SQL, Git and the existing software/testing workshops. Extract the full kit and open a terminal in cloud-operations-kit. Python 3.10+ with its standard SQLite module is sufficient for the executed labs; no cloud account, credentials, package installation or network is needed. All data and prices are invented teaching fixtures. The runnable drills use new temporary folders and databases, never the website or hosting. These free companions retain existing course progress/access. Payments and public registration remain disabled.
python3 lab.py
python3 -m unittest discover -s tests -vUnderstand the concept
Queues may redeliver work after a timeout or lost acknowledgement. Design a stable business/event identifier and an idempotent effect rather than assuming one delivery. The local ledger stores the event ID and its amount in the same SQLite transaction as updating the total. An exact repeated ID returns duplicate; a conflicting amount for that ID raises an error. A simulated pre-commit failure rolls back both marker and total, so a retry can apply the event once. A commit followed by lost acknowledgement leads to a later duplicate, which must not apply again. This transaction covers one local database only. External emails, APIs or payments cannot be made atomic merely by adding a database marker; they require their own outbox/idempotency/reconciliation design.
Worked example
A applies 30000 cents. B’s attempted 40000 update fails before commit, leaving the total at 30000. Replaying the four supplied deliveries then applies B, recognizes two A deliveries as duplicates and rejects negative C. Final state contains two unique applied IDs and 70000 cents. The replay’s counters are applied=1, duplicate=2, rejected=1 because A was already committed before replay. bounded_retry permits at most three total attempts and retries only the simulated TimeoutError; validation failures are not retried. It does not implement durable queue scheduling or backoff.
from lab import queue_drill
r=queue_drill()
assert r["after_failure_cents"]==30000
assert r["unique_applied"]==2 and r["total_cents"]==70000
assert r["rerun"]=={"applied":1,"duplicate":2,"rejected":1}Try it yourself
Trace the failure boundary and counters before running. Write a test for A repeated with a different amount, and one for three transient timeouts. Explain the difference between before-commit failure and after-commit/before-ack redelivery. Draft a rejected-message review process without automatically deleting evidence. Predict the result first, keep the original fixture, and record the exact command and observed outcome from your practice copy.
Worked solution
Conflicting A raises ValueError and leaves the committed total unchanged. Three TimeoutErrors cause exactly three attempts then a terminal error. An authorization or validation error should not be treated as a transient fault. In a real broker, acknowledgement policy, visibility timeout, deduplication retention and dead-letter handling need provider-specific tests. This lab is a single-process SQLite analogue, not a tested distributed queue or exactly-once guarantee.
Knowledge check
Can a local deduplication table guarantee that an external payment or email happens exactly once?
Check and explain
No. It does not atomically control that external side effect. Define and verify the whole delivery/effect boundary.
Evidence, mistakes & handover
State whether an activity was executed locally, reviewed as a design, or left unexecuted. A successful local drill does not verify a cloud provider, container, hosted pipeline or production recovery. Keep an owner, acceptance criteria, known failures and rollback/cleanup evidence with the work. The download includes source fixtures, runnable drills/tests, separate exercises and solutions, a runbook, a resource plan, an optional container/CI review pack and a capstone rubric. Credit the teaching baseline and your own contribution; no certification, commercial service or employment guarantee follows.
Authoritative reference
— primary/official reference for the mechanism. Lesson explanations and fictional cases are original. Provider details can differ; this lesson does not configure or provision an account. No production access, payment, public registration or live deployment is activated.
Try the activity before reading its reference. Keep your sources, commands and assumptions with your work. Companion activities are unsaved self-practice and do not award a certificate or change lesson progress.