Skip to Content
Meubles Morinière

Engineering choicesTechnical case studies13 min read

A request that may or may not have gone out

A refund sent to Stripe that gets no answer, an email Resend said nothing about, a sale sent twice from the store. Here’s how the ERP treats the unknown outcome as a state of its own, with keys taken from the fact being announced and states that don’t move backwards.

The problem

A call to an outside service has three outcomes. It succeeded, it failed, or you don’t know. When the timeout expires, the connection drops or the server answers with a generic error, the request may have gone out. The simplest code files this third outcome under one of the other two. In a store, both choices are expensive.

The refund audits of September 2026 found two examples.

On September 1, a refund whose Stripe response had been lost stayed pending. That was right, because the request may have succeeded. If we had closed the line, we’d have made refundable an amount that may already have left. But nothing closed it afterwards. The order’s refundable ceiling stayed reduced. The webhook had no Stripe identifier to find the line. And the morning check only flagged it on the fourteenth day, the day by which the customer must have been refunded after a withdrawal (article L221-24 of the French Consumer Code, 68416dd042).

On September 4, the reconciliation that queries Stripe wrote its result under a condition that was too broad (status: { notIn: [SUCCEEDED] }). But three lines earlier, it had just made a round trip to Stripe, and that takes seconds. If a webhook closed the line as failed during that call, an older read overwrote it. The write didn’t look at its count of updated rows either, and the screen announced “status applied” when nothing had been written (d1a24c301e).

The same problem shows up elsewhere, in other shapes. In the store, a double click or a network that recovers sends the same sale twice. Stripe delivers its events at least once and without a guaranteed order, and a payment can arrive while the workshop is cancelling the order. Resend may not answer within the time the code allows it (RESEND_TIMEOUT_MS) even though the email went out. And some emails are mandatory, like the acknowledgement of a withdrawal.

The constraints

  • A duplicate can’t be erased. The database refuses to modify or delete a payment, a credit note or an issued invoice (ADR 0008). A sale recorded twice is corrected by a new entry, which stays visible in the payment chain.
  • A transaction doesn’t span Stripe or Resend. The database write and the outside call are two steps. If a failure happens between them, one is done and the other isn’t (Data).
  • The project has no durable queue, no long-running process and nobody on call (Debt, risks and evolutions). There’s only the morning check, which runs once a day.
  • Stripe and Resend accept an idempotency key. When a request reuses the same key, Stripe returns the response of the first one. Resend refuses a key it has already served, and the code recognises that refusal (keyAlreadyUsed).
  • Stripe redelivers an unacknowledged event for three days (STRIPE_EVENT_RETRY_SECONDS), and it doesn’t guarantee the order.

The alternatives

  • Treat every error as a failure and let the person try again. It’s the most common reflex, and that’s how you refund twice when the first request had gone through. The September 1 commit rules it out for that reason: closing the line “would free a ceiling that may already be used up” (68416dd042).
  • Treat every error as a success. The screen stays calm, and the customer waits for an invoice or a refund that will never come.
  • Hand the outgoing requests to a durable queue and a process that retries them. The queue guarantees that a request will be retried. But at every retry, the process hits the same question: did the previous attempt go out? So it needs the same idempotency keys, plus a queue to host and monitor. The project doesn’t have one, and the debt page says when that choice would become the wrong one.
  • In the store, keep the sale’s key in a unique column of the database. It would be durable, and the guard wouldn’t depend on any other service. The code keeps it in Redis (see the limits).

Only the first option is ruled out in writing. The others are the ones a reader would ask about, and I found no record showing they were compared.

The decision

The unknown outcome is a state of its own. The code doesn’t mix it up with success or with failure. It leaves the line in an intermediate state, tells the person, and offers a way out that asks the other side. Three rules come with that choice.

  1. A request carries a key taken from the fact it announces. A refund carries its local identifier. The withdrawal acknowledgement carries the order and the date of the request. An in-store sale carries an identifier drawn once per ticket. So a retry lands on the same key, wherever it comes from.
  2. A write that applies a state coming from outside names the states it’s allowed to leave, and it reads its count of updated rows. A stale read then loses the race, when it used to win it.
  3. A terminal state isn’t rewritten. When the outside world contradicts it, an alert goes out and a person decides.

The implementation

One key per fact

In the store, the browser draws a key on the first submission of a ticket and keeps it until the ticket is recorded (crypto.randomUUID(), in apps/erp/modules/payments/components/off-site/off-site-basket.tsx for a sale and apps/erp/modules/orders/components/store-payments-section.tsx for a payment). So a second click or a retried submission carries the same key. The server reserves it in Redis for a limited time (SUBMISSION_TTL_SECONDS), with a SET … NX that only succeeds for the first arrival (apps/erp/shared/server/idempotency.ts#claimSubmission). There are three possible answers.

  • The key is free, and the sale is recorded.
  • The key is reserved but has no result, and the submission is refused as already in progress.
  • The key holds the identifier of a recorded sale. The server reads that sale back and returns the same answer as the first time (record-off-site-basket.ts).

If recording fails, the key is released and the person can try again. A sale also writes its key on the order, under a uniqueness constraint (Order.submissionKey): when the Redis reservation has expired or is missing, the database finds the sale instead of writing a second one (off-site-sale.service.ts#BasketAlreadyRecorded).

Refunds go to Stripe with their local identifier as the key (stripe-refund.service.ts#sendReservedRefund). Emails follow the same logic. The acknowledgement of a withdrawal is keyed on the order and the date of the request (packages/orders/src/withdrawal.ts#createWithdrawalNotices), and a notice to the customer is keyed on the order and its last update. Since the key comes from the fact being announced, two paths that announce the same fact meet at the provider, for instance the retry of a process and a new click.

Three outcomes, and a follow-up for each

sendReservedRefund sorts Stripe’s errors. A certain refusal (isPermanentStripeFailure) closes the line as failed, and the amount becomes refundable again. Everything else (a timeout, a network failure or a server error) leaves the line pending, sends the alert “Stripe response LOST — the refund may have gone out” and tells the person that the outcome is uncertain. One last case has its own branch: Stripe accepted, but the identifier couldn’t be saved in the database. In that case, the line stays pending, and the webhook will find it with the local identifier passed in the metadata.

For emails, the list is written the other way round. isCertainlyUnsent (packages/infra/src/email.ts) names the Resend error codes that prove nothing went out: an invalid key, a failed validation, a quota reached, a send the code never attempted. Anything that isn’t on the list, a timeout included, counts as possibly sent. So if Resend added an error code tomorrow, it would fall on the cautious side.

The acknowledgement of a withdrawal shows the whole gesture. It goes out first, and the order is only marked as acknowledged (withdrawalAcknowledgedAt) afterwards, by a conditional write. Then there are four cases. If Resend has already seen the key, the email went out and the order is marked. If another request is sending under the same key, that request will mark it. If the failure is certain, nothing is marked and the error is raised. In every other case, nothing is marked either, and an alert goes out. The order stays “never acknowledged”: the morning check and the order page’s “Renvoyer l’accusé” button resend it under the same key, which Resend doesn’t serve twice within 24 hours. The reverse order, marking before sending, ruled out any duplicate. But a process killed between the two left an order marked as acknowledged with no email sent, and nothing read it again. A duplicate is still possible if the retry comes more than 24 hours after a first send, whereas an acknowledgement the law requires can no longer go missing without anyone knowing.

Leaving the pending state by asking Stripe

A pending line with no Stripe identifier can’t get out through the webhook. The September 1 commit says so: the webhook “only fires if Stripe speaks, and the whole problem is that Stripe may never have received the request” (68416dd042). So the reconciliation queries Stripe (stripe-refund.service.ts#reconcileStripeRefund). It looks for the refund by its Stripe identifier. If there isn’t one, it looks for it by the local identifier among the payment’s refunds. For a line with no Stripe identifier, it refuses to look before a delay (LOST_REFUND_RESPONSE_MAX_AGE_MS). Otherwise, it could conclude that a request doesn’t exist while it’s still on its way.

Stripe can answer in three ways.

  • It knows the refund. The line takes its identifier and status, through the same function as the webhook (refund-settlement.ts#applyStripeRefund).
  • It doesn’t know it. The line closes as failed with the note “never received by Stripe”, the amount becomes refundable again, and you can try again with no risk.
  • It doesn’t answer. The reconciliation concludes nothing, and the line stays where it was.

The outcomes form a typed union (StripeRefundReconciliation), and the action turns it into a sentence for the person. What nobody resumed, the morning check picks up: a refund pending for too long (PENDING_REFUND_MAX_AGE_MS) shows up in the morning email (A morning check that says what it could not read).

A state never moves backwards

The webhook doesn’t trust the content of the event. It reads the refund again at Stripe (webhook-refund.service.ts), like the reconciliation does. Two reads started one after the other can come back in the opposite order. That’s what caused the overwrite of September 4.

So the write names the states it’s allowed to start from. An “in progress” state (pending, action required) only applies to a line still in progress. An arrival state (succeeded, failed, canceled) applies to a line in progress or succeeded, because a succeeded refund can still fail at Stripe. A failed or canceled line no longer changes. That rule is written once, in a function the write puts in its where (settleRefund).

refundSourceStatusesapps/erp/shared/lib/refund-status.ts · lines 32–37
export function refundSourceStatuses(target: RefundStatus): RefundStatus[] {
	if (target === RefundStatus.PENDING || target === RefundStatus.REQUIRES_ACTION) {
		return IN_FLIGHT_REFUND_STATUSES;
	}
	return NON_TERMINAL_REFUND_STATUSES;
}

A stale read then finds a line that has already moved on. Its write changes nothing, and the code reads that zero. The webhook answers normally, and the reconciliation announces that it lost the race (RECONCILE_RACED). Before September 4, it announced “status applied”. The test replays both cases on a real database: it holds back the first Stripe read until the second one has finished.

A terminal state that Stripe contradicts doesn’t move, but it’s reported. If Stripe brings back to life a refund the database considers failed, an alert warns that the money may have gone out (applyStripeRefund). Disputes follow the same rule: a dispute won or lost doesn’t go back on a late event (dispute-webhook.db.test.ts).

Across the ERP, the transition test checks that a state column being written is in the where of a conditional write (Tests that read the code in place of the reviewer). The list of starting states is checked by each domain’s own tests.

Recording an event is not enough to process it once

The ERP records each Stripe event by its identifier before processing it (WebhookEvent), and marks it processed at the end. A duplicate already marked gets an immediate answer. A duplicate recorded but not marked is processed again, because the first pass may have stopped halfway. An alert that couldn’t be sent also makes the endpoint answer with an error, so that Stripe redelivers (stripe-webhook.service.ts).

So the table doesn’t guarantee that an event is processed only once. It avoids redoing an event that’s already processed. What prevents a double effect is the write in each handler: an order only becomes paid if it was pending, and stock only goes down if there’s enough. A redelivery finds the state already advanced and does nothing (Data). And for an event Stripe failed to deliver, even after three days of attempts, the morning check asks every day for the list of missed deliveries and replays those events through the same path (Stripe).

The same reasoning settles a rarer crossing: the workshop cancels an order at the moment its payment arrives. The cancellation takes the order’s lock first, while the customer has just paid. The money is at Stripe, and the ERP can’t refuse it. The webhook, which was waiting for the lock, finds the order canceled. It records the payment without decrementing stock, issues the invoice and raises an alert so that someone refunds. The database describes what happened, and what comes next is up to a person.

Forcing the race in the test

If you hope to reproduce a race with pauses, you get a test that passes by luck. So the concurrency tests force the order of arrival. A transaction in the test first takes the lock on the target row. The test starts the first gesture and waits until Postgres counts one session blocked on a lock (pg_stat_activity). It starts the second, waits for a second blocked session, then releases the lock. If the crossing doesn’t happen, the test fails and says so. It doesn’t pass without having checked anything.

untilLockWaiterspackages/testing/src/db-fixtures.ts · lines 161–171
export async function untilLockWaiters(expected: number): Promise<void> {
	const deadline = Date.now() + 10_000;
	while ((await lockWaiters()) < expected) {
		if (Date.now() > deadline) {
			throw new Error(
				`jamais ${expected} sessions en attente de verrou : le croisement n'a pas eu lieu`,
			);
		}
		await new Promise((resolve) => setTimeout(resolve, 25));
	}
}

On November 16, 2026, fourteen test files in the ERP and the storefront use it (counted with rg -l untilLockWaiters, helper files excluded). A round trip to Stripe takes no lock in the database. To reproduce it, the Stripe double holds the first read behind a promise, and the test only releases it once the second has finished (refund-stale-read.db.test.ts).

The results

  • A sale sent twice from the store is recorded once, and the second submission gets the answer of the first.
  • A refund left without an answer waits, then the reconciliation files it according to what Stripe knows. The two crossed reads of September 4 end on the most recent state, on a real database (refund-stale-read.db.test.ts).
  • A retry doesn’t send a mandatory email twice, and an uncertain send arrives as an alert with what to check.
  • A Stripe redelivery counts nothing twice, and a payment that crosses a cancellation is recorded, invoiced and reported (cancellation-versus-payment.db.test.ts).

The limits

  • When Redis is missing or doesn’t answer, the in-store guard lets submissions through, and the log says so. The reason is written down: blocking the till because a cache went down would be worse (Debt, risks and evolutions). But a doubled payment can only be corrected by a new entry. A key stored in the database with the sale, under a unique constraint, would close that gap without depending on a second service. I found no written record explaining the choice of Redis.
  • The key reservation expires. A payment retried after its lifetime (SUBMISSION_TTL_SECONDS) is no longer recognised, whereas a sale still is, by its key in the database. And if the result couldn’t be noted in Redis, new attempts stay refused as “in progress” until it expires (settleSubmission writes it to the log).
  • The uncertain outcome of an email falls to a person. Resend sends no events back to the applications (Stripe and Resend), so the ERP can’t check on its own that an email went out. Everything rests on the alert and on someone reading it.
  • The partition of states is written once for refunds, but each model with a state has its own. The transition test checks that the state is in the where. It can’t tell whether the list of starting states is right. That’s the job of each domain’s tests.

On this page