The point kernel makes one movement correct. It cannot know that the movement it is asked to make is the same one it made ten seconds ago. A payment confirmation retried by a browser, a daily job run a second time, and an admin's double click all look like new requests. This page is about how the system recognises them.
The problem
Retries are normal here, and most of them are outside my control.
- A browser returning from the card provider’s page calls the confirmation route. If the response is slow, the user reloads, and the route is called again.
- The provider sends a webhook for the same payment the browser is confirming.
- A daily job pays rewards. If it is triggered again by an operator, or two jobs cover the same application for different reasons, the same reward is requested twice.
- An admin presses a button twice.
A check of the form “has this been paid? if not, pay” does not hold under concurrency, for the same reason a read-then-write balance does not. Two requests can both read “not paid” before either writes.
A key names the thing, not the request
Every operation that can be retried passes the kernel a key. The key is built from the identity of the thing being paid for, so any request for that thing produces the same key, whoever sends it and whenever.

| Operation | The key is built from | Why that identity |
|---|---|---|
| Card top-up credit | The payment order id | The order is the unit the provider confirms. A retry, a webhook and an admin’s manual retry all refer to it |
| Card cancellation: revoking the points, and restoring them if the provider refuses | The order id and an attempt number | A payment can be cancelled, restored after a provider failure, and cancelled again. Each attempt is its own event |
| Bank-deposit top-up | The deposit request id | One request is matched to one incoming transfer |
| Cash-out debit, and refund on rejection | The request id, with a distinct prefix for the refund | The debit and its reversal are two events on one request |
| Shop purchase, and its refund | The purchase id | One charge and at most one refund per purchase |
| Campaign reward | The campaign id and the tester’s id | A tester is paid once per campaign, whichever job gets there first |
| Campaign refund issued through the refund route | The campaign id | The refund is written once per campaign |
| Application deposit: hold, refund, forfeit | The application id with one prefix per event | Three different events on the same application must not collide |
| Game rewards | The game token or session id | One result per play |
| Admin adjustment | A key sent by the admin tool in a request header | A double click sends the same header twice |
The campaign reward shows the idea best. Two different daily jobs can decide that the same tester should be paid: one pays completed applications after the review deadline, another closes the campaign a week later and pays anyone still unpaid. Neither job needs to know about the other. Both build the key from the campaign and the tester, and the second one to arrive gets the first one’s result.
Two layers
The kernel uses the key twice.
The lookup. Before opening a transaction, the kernel searches the ledger for a row with the key. A hit returns that row’s result with a flag saying it was a repeat. This is the path almost every retry takes, and it costs one indexed read.
The index. The lookup alone has the same race as any read-then-write: two first attempts can both find nothing. The ledger therefore has a unique index on the key. This is its shape.
// index on the point ledger (shape)
{
key: { idempotencyKey: 1 },
unique: true,
partialFilterExpression: { idempotencyKey: { $type: "string" } }
}
When two first attempts race, both pass the lookup and both enter a transaction. One ledger insert succeeds. The other violates the index, its transaction rolls back, including the balance update that preceded the insert, and the kernel answers it as a repeat. The caller cannot tell the difference between losing the race and arriving a minute late, which is the point.
The index is partial because not every movement has a key. A plain unique index would treat every row without a key as the same missing value and reject the second one. The filter restricts uniqueness to rows where the key is a string.
A second partial unique index covers referral bonuses, on the combination of reason, user and reference. That rule is narrower than a key: one bonus per referred account, regardless of how the request was phrased.
Creating the index
Partial indexes cannot be declared in the Prisma schema, so a script in the repository creates them. It does three things that a one-line command would not:
- It scans the ledger for keys that already appear more than once, because a unique index cannot be built over duplicates.
- It has a dry-run mode that prints what it would do.
- Its header says to run it before deploying code that depends on the index. Code that expects a duplicate-key error and never receives one would silently accept the second insert.
Keys and claims
A key protects a point movement. Some operations also change the state of the thing itself, and there the same idea appears as a conditional update on that state.
- A campaign reward first claims the application by setting its paid-at timestamp, with a condition that it is not set. Only the request that wins the claim goes on to the kernel.
- A campaign refund claims a processed flag on the campaign, pays with the campaign’s key, and releases the claim if the payment fails.
- A cash-out moves between admin states by conditional update on the current state. Rejecting one triggers the keyed refund, and the status is restored if the refund fails.
- A card order is claimed by moving its status before the provider is called. Payments follows that path.
Claim and key do different jobs. The claim keeps two requests from doing the surrounding work twice, such as calling a provider. The key keeps the points from moving twice even if the claim logic has a gap.
Failure modes
| What happens | What the system does | Covered by |
|---|---|---|
| A keyed movement is requested again later | The lookup finds the row and returns its result | A kernel test: an existing row with the same key yields a repeat |
| Two first attempts race | One insert wins. The other hits the index, rolls back and is answered as a repeat | A test on an in-memory replica set creates the index, fires five concurrent calls with one key and asserts the balance moved once |
| The insert hits the index inside a caller’s transaction | The error is rethrown and the caller’s whole transaction aborts | By construction: the kernel answers as a repeat only when it opened the transaction itself |
| A card confirmation is retried after success | The route answers that the order is already approved | Tests on the confirmation route for ownership and repeats |
| A reward is requested by two jobs | The paid-at claim stops the second. If both got through, the key would | Tests on the reward service against the kernel |
| A cash-out is rejected twice | The second conditional update finds a different state and returns a conflict | A state test on the admin cash-out route |
Limits
- A key is only as good as the identity it is built from. The cancellation keys carry an attempt number precisely because “this order” was not specific enough once a cancellation could be undone and repeated.
- Keys cover the operations in the table, not every movement. A movement without a key still cannot overdraw, because of the balance condition, but the database has no way to recognise a repeat of it.
- The rule lives in the database, and the database has to have it. The index script is a deployment step, with a dry run, and not something the schema tool applies on its own.
