josesaid / idempotency-demo

Idempotency Keys ✿ charge at most once

A Spring Boot payments endpoint that charges at most once per Idempotency-Key, even with client retries, a reused key with another body, or several requests racing with the same key.

Stack

LanguageJava 21
FrameworkSpring Boot 3.3.5 (Web, Data JPA)
DatabaseH2 in memory, table idempotency_record with UNIQUE(idem_key)
Messaging / Cloud / DockerNot used
BuildMaven 3.9+ · port 8080

Endpoints

MethodPathDescription
POST/paymentsCreates a payment; requires the Idempotency-Key header
GET/payments/executionsHow many real charges happened

Run it

git clone https://github.com/josesaid/idempotency-demo.git
cd idempotency-demo
mvn spring-boot:run      # or: mvn test

Scenarios

ScenarioAnswerDiagrams
A · First request201EN · ES
B · Retry with the same key200 stored responseEN · ES
C · Same key, different body422EN · ES
D · Concurrent requests201 409EN · ES
E · Provider failure500 → 201EN · ES

Key ideas