Home / Learn Secure SQL / Idempotency Keys

Idempotency Keys

Preventing a resubmitted request from being applied twice.

The gistPreventing a resubmitted request from being applied twice.

The idea, explained simply

Idempotency checkrequest arrives witha keykey already seen?yes: return thestored prior resultno: perform theoperation and store…
Idempotency check

Definition

An idempotency key is a unique value the client sends with a request (e.g. "process this payment exactly once"), which the server stores and checks so that a retried or duplicated request does not repeat the operation.

Why you need this

Network retries, double-clicks, and automated retries are common; without an idempotency check, the same "pay $50" request arriving twice could charge a customer twice, which is a correctness and trust issue closely tied to validating input properly.

How it works

Require a client-supplied idempotency key for operations that must not repeat, store it with a UNIQUE constraint alongside the operation's result, and check for an existing key before performing the operation again.

Where you'll see this

  • Web application code review
  • Backend development
  • Database administration
A UNIQUE constraint enforcing one-time processing (expected error)SQL: edit and run

The second insert fails on purpose: the UNIQUE constraint on idem_key is exactly what stops the same payment from being recorded twice.

Test what you just read

1. An idempotency key mainly prevents...