The Money Path Is a Replay¶
Learning objectives
- Why every payment event must be treated as something that will arrive more than once
- How to layer idempotency so that no single mechanism is a single point of failure
- Why an append-only ledger with a uniqueness key beats "checking first"
- What "exhausted" should mean to a customer — and what it must never mean
Payment providers redeliver, and that is the design¶
A webhook that tells your platform "this customer paid" is not delivered once. Providers retry on timeouts, on slow responses, on their own internal hiccups — redelivery is a feature of reliable delivery, not a bug in it. So the first design question of any money path is not "how do I process a payment?" but "what happens when I process this payment for the third time?"
The naive answer — check whether you have seen the event before — has a flaw: the check and the action are two steps, and the world can change between them. Two replicas processing the same redelivery can both pass the check and both act. Anywhere the check lives in memory, or in a cache, or in anything that can be wiped, the guarantee is only as durable as that component's worst day.
Three locks, independently owned¶
The pattern that holds is layering independent mechanisms, each of which would be sufficient on a good day, so that a replay must defeat all of them on a bad one:
Layer 1 — deduplicated delivery. The webhook endpoint claims each event's unique identifier before doing anything. A replay finds the claim and is acknowledged without a second handoff. This layer is fast and cheap — and it lives in a cache, so it is allowed to fail.
Layer 2 — pinned work identity. The durable job that processes the event derives its identity from the event. Starting the same job twice is a collision the workflow engine rejects, not a second execution. This layer survives a cache wipe — but it ends when the job's history is eventually archived.
Layer 3 — a uniqueness constraint at the database. The ledger entry that grants value carries a key built from the payment's own identifiers, and the database enforces uniqueness. Insert the same grant twice and the second insert fails as a duplicate — reported as a normal outcome, not an error. This layer survives everything the other two cannot, because it holds where the money actually lands.
The discipline that makes the layers real: each one gets a test that fails when it stops holding, and the failure of any one layer must be visible in that test — not discovered on a customer's statement.
A ledger you can edit is not a ledger¶
The value-granting record is append-only: there is an insert path and no update or delete path, structurally. Corrections are new entries with their own direction (a refund is a new row, not an erasure), so the history of what happened is always reconstructible from the rows alone. This is centuries-old bookkeeping practice, and it earns its keep in software the first time someone asks "why does this balance say what it says?" — the answer is a list of rows, each carrying its own provenance, not the current value of a mutable counter.
One subtlety worth stealing: when a replayed grant hits the uniqueness constraint, the rest of the processing still runs. If the first delivery crashed after writing the ledger but before updating the customer's spending ceiling, the replay is what heals the gap. Idempotent does not mean "do nothing the second time" — it means "converge on the same correct state no matter how many times you run."
Grant exactly what was bought, or nothing¶
When money arrives, the amount is matched exactly against a published catalog of what can be bought. A match grants the catalog's value. No match — a discounted amount, a partial capture, a tampered request — grants nothing, loudly, and summons a human. The alternative — deriving the granted value from whatever amount arrived — turns every upstream anomaly into invented credit. Code should never guess about money; it should recognize it or refuse it.
"Exhausted" is a state, not an outage¶
The last lesson is a product lesson. When a customer's included usage and credits run out, the platform refuses new AI work — and nothing else. Their documents stay readable. Their existing answers stay available. Work that was interrupted mid-run is parked, not failed, and the moment a payment lands it resumes on its own, without anyone filing a ticket.
The refusal itself carries a distinct, machine-readable reason that the interface can turn into a self-service action ("buy credits"), kept strictly separate from the reason that means "the platform is misconfigured, contact an administrator." Collapsing those two states is how every out-of-budget customer ends up in a support queue for a problem they could have solved themselves in thirty seconds.
The takeaway
Design the money path for its replay, because the replay is coming. Layer independent idempotency mechanisms and test that each can actually fail; make the value-granting record append-only with the guarantee held by the database; grant exactly what the catalog says or nothing at all; and make running out of budget a self-service pause — never a lockout, and never an incident.