What happens when Dodo Payments sends the same webhook twice
Webhooks get retried and arrive out of order. Four replay scenarios run against Launchfast's real handler and Postgres, and why each one ends in the right state.
Short answer: nothing bad. Launchfast stores each payment once no matter how many times Dodo delivers it, and a subscription never moves backwards when an old event shows up late. Both rules are enforced by Postgres inside a single statement, so they hold even when two copies of an event arrive at the same moment.
This post walks through the replay scenarios we ran against the kit's real webhook handler and a real Postgres database, and shows which line of code makes each one come out right.
Why the same webhook arrives more than once
When a customer pays, Dodo sends your server a webhook: "payment pay_1 succeeded for customer cus_1". Your server has to answer with a 2xx status. If it doesn't, because the server was slow, a deploy was rolling out or the database had a moment, Dodo can't tell whether you processed the event, so it sends it again later.
That is the right behaviour for a payment provider, and it means three things your handler has to expect:
- Duplicates: the same event delivered two or more times.
- Concurrent duplicates: two copies arriving at once, handled by two server instances in parallel.
- Out-of-order events: a retried older event arriving after a newer one.
A handler that doesn't plan for these will double-count revenue, or reactivate a subscription the customer already cancelled.
The scenarios and what we got
We ran each scenario through handleWebhookEvent from the kit, unchanged, against Postgres with the kit's own migrations applied, then read the tables back.
| Scenario | What a naive handler does | Launchfast result |
|---|---|---|
Same payment.succeeded delivered 3 times | 3 payment rows, revenue counted 3 times | 1 row, counted once |
Same payment.succeeded delivered 3 times at the same moment | Can race and store 2 or 3 rows | 1 row |
subscription.active, then subscription.cancelled, then the old active retried | Subscription active again: free access | Stays cancelled |
subscription.cancelled arrives before the older active | Subscription ends up active | Stays cancelled |
Duplicate payments: one row per Dodo payment ID
Every successful charge goes into a payment table, keyed by Dodo's own payment ID, and that column is unique:
// src/db/schema.ts
providerPaymentId: text("provider_payment_id").notNull().unique(),
The webhook handler inserts and tells Postgres to do nothing if that ID is already there:
// src/lib/billing/dodo.ts
await db
.insert(payment)
.values({ userId, provider: "dodo", providerPaymentId: charge.payment_id, amount: charge.total_amount /* … */ })
.onConflictDoNothing();
The second and third deliveries hit the unique constraint and are skipped. There is no "check if it exists, then insert" step in application code, so two copies handled in parallel can't both slip through: Postgres decides, and only one insert wins.
This is also why the admin revenue dashboard can't double-count: it sums the same table.
Out-of-order subscription events: newer wins
A subscription row holds the latest known state, plus the timestamp of the event that set it:
// src/lib/billing/dodo.ts
const eventTime = new Date(event.timestamp);
await db
.insert(subscription)
.values({ userId, providerSubscriptionId: sub.subscription_id, status: sub.status, providerUpdatedAt: eventTime /* … */ })
.onConflictDoUpdate({
target: subscription.providerSubscriptionId,
set: { status: sub.status, providerUpdatedAt: eventTime /* … */ },
setWhere: lt(subscription.providerUpdatedAt, eventTime),
});
The setWhere is the important line. The update only happens when the stored event is older than the incoming one. So:
- A late
activeafter acancelled: the stored event (cancelled) is newer, the condition is false, the row doesn't change. The customer stays cancelled. - An exact duplicate: same timestamp, so "older than" is false. Nothing changes.
- A genuinely newer event (a renewal, a cancellation): the condition is true, and the row updates.
As with payments, the comparison runs inside the same SQL statement as the write, so it is safe when deliveries overlap.
Why access stays correct
Pages never ask Dodo who has paid. They call getBillingStatus(userId), which reads only these tables:
- A paid one-time row in
paymentgrants access. - Otherwise the subscription row must be
active.
Because the tables can't hold a duplicate payment or a stale subscription state, the access check built on them can't either. And page loads keep working even when the payment provider is having an outage.
Checklist for your own handler
- Key every stored payment on the provider's payment ID, with a unique constraint.
- Insert with "on conflict do nothing" instead of checking first in code.
- Store the event timestamp on each subscription and only apply newer events.
- Do the comparison in the database, in the same statement as the write.
- Return
500on real errors so the provider retries, and let the rules above make the retry harmless.
For the full handler, including signature verification and linking payments to users, read how to handle Dodo Payments webhooks safely. Launchfast ships all of it, already wired to the admin revenue dashboard. See what's inside.