Deduplicate an event in the same transaction as its effect
At-least-once delivery means a consumer must expect duplicate events. Recording a processed identifier separately from the business effect introduces two failure windows: a lost effect or a repeated effect. A uniqueness constraint and one transaction close those local windows.
An identifier alone is not permission to ignore a changed payload. This contract rejects a replay whose amount differs from the original event. In production, scope identifiers to a tenant and producer, define retention, and decide how poison messages are handled. The exercise demonstrates durable local deduplication, not an end-to-end exactly-once guarantee.
import sqlite3
with sqlite3.connect(":memory:") as db:
db.execute("create table inbox(id text primary key)")
db.execute("insert into inbox values (?)",("a",))
print(db.execute("select id from inbox").fetchall())Review the design
This serial SQLite exercise proves the local invariant. A concurrent database deployment additionally needs an isolation and uniqueness-conflict policy; a read-then-write sequence by itself is not a cross-process lock.
Your task
Implement apply_event(db,key,amount). Tables inbox(id primary key,amount) and totals(value) with one row exist. First delivery inserts the inbox row and increments total atomically, returning True. Identical replay returns False; changed amount raises ValueError.
The explanation opens here once every check passes.