Deutsch
Security wikiPayments · Integrations

What is idempotency?

Also known as: idempotent, idempotency key, deduplication, exactly-once processing

An idempotent operation has the same effect whether it runs once or many times. Why webhooks and payments need it, and a tested Postgres pattern to skip repeats.

By Tim GeithnerPublished 2 min read

Short answer

An operation is idempotent if doing it several times has the same effect as doing it once. Webhooks and payment calls must be idempotent because they get retried: Stripe may deliver the same event more than once. Record each event ID under a unique key before you act, skip IDs you have already seen, and send idempotency keys on retried API calls.

Ask an AI about this page:ChatGPTClaude

On this page

An operation is idempotent if running it several times has the same effect as running it once. MDN puts it for HTTP as the effect of one request being the same as that of several identical requests. Reading a record or setting a value is naturally idempotent; adding 100 credits or sending an e-mail is not, unless you make it so.

Idempotency in an AI-built app

The place this bites is the webhook handler. Stripe documents that an endpoint might receive the same event more than once and that it retries live-mode events for up to three days if your endpoint does not answer in time. A generated handler often looks like “on invoice.paid, add 100 credits”. Deliver that event twice and the customer gets 200 credits; a slow handler that times out can be called again while the first run is still working.

The fix is to remember which events you have processed and to make the “have I seen this?” check atomic, so two parallel deliveries can’t both pass it. A table with the event ID as primary key does exactly that:

SafeA table that remembers processed events
create table public.processed_events (
  event_id text primary key,
  processed_at timestamptz not null default now()
);

alter table public.processed_events enable row level security;
revoke all on table public.processed_events from anon, authenticated;

In the handler, after the signature is verified, claim the event before doing anything else:

Claim an event; no row back means: already done
insert into public.processed_events (event_id)
values ('evt_123')
on conflict (event_id) do nothing
returning event_id;

The first delivery gets evt_123 back and does the work. Every later delivery gets no row and answers 200 without doing anything. Unlike a separate “select first” check, the primary key lets only one insert of the same ID succeed, even if two deliveries arrive at the same moment. Only server code with the secret key should write to this table; RLS without policies and the revoke keep browser keys out.

For calls your app makes, such as creating a Stripe payment, the same idea runs the other way: send an Idempotency-Key header, and Stripe returns the saved result on a retry instead of creating a second object.

How to check your app

  1. Open each webhook handler and look for the event ID (event.id). If it is never stored or compared, repeats are processed again.
  2. Look for read-then-write checks (“select, and if nothing found, insert”). Two parallel deliveries can both pass them; use a unique key instead.
  3. Check whether the side effect (credits, plan change, e-mail) happens only after the claim succeeded.
  4. For outgoing payment calls, check that retries reuse the same idempotency key.

How to fix or prevent it

Claim each event under a unique key, keep side effects after the claim, and prefer setting state (“plan = pro”) over adding to it (“credits + 100”) where you can. If the work can fail halfway, do the claim and the change in one database transaction, for example inside one Postgres function, so a crash does not leave an event marked done that never took effect. The full fix is on Non-idempotent Stripe webhook, and the payment flow around it in Stripe in vibe-coded apps.

Frequently asked questions

Why would Stripe send the same event twice?

Stripe’s docs say webhook endpoints might occasionally receive the same event more than once. It also retries for up to three days when your endpoint does not answer with 2xx in time, so a slow handler can see an event again.

What is an idempotency key?

A unique value you send with an API request, for example in Stripe’s Idempotency-Key header. If the request is retried with the same key, the server returns the saved result instead of doing the work again. Stripe suggests V4 UUIDs.

Is GET idempotent?

Yes. MDN lists GET, HEAD, PUT, DELETE, OPTIONS and TRACE as idempotent methods. POST and PATCH are not guaranteed to be, which is why creating payments or charges needs idempotency keys.

Sources

  1. 1.Idempotent (glossary) — MDN Web Docs
  2. 2.Idempotent requests — Stripe API Reference
  3. 3.Receive Stripe events in your webhook endpoint — Stripe Docs
  4. 4.INSERT (ON CONFLICT) — PostgreSQL Documentation

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt