idempotency-demo · sequence diagram

Scenario A · First request

A new Idempotency-Key reserves a row, charges once and stores the response. Answer: 201 Created.

own transaction own transaction Clientcurl / test PaymentControllerPOST /payments PaymentFacadecreatePayment() IdempotencyReserverREQUIRES_NEW H2idempotency_record PaymentServicesimulated provider POST /payments · Idempotency-Key: pago-001 createPayment(key, request) requestHash = SHA-256(body) findByKey(pago-001) Optional.empty() Reserve the key reserve(key, hash) INSERT status=PROCESSING (saveAndFlush) COMMIT row: PROCESSING Charge exactly once process(request) executions++ · sleep 2 s PaymentResponse(paymentId, APPROVED) Store the response complete(key, responseJson) UPDATE status=COMPLETED, response_body COMMIT row: COMPLETED 201 Created 201 Created · paymentId Result: 1 real charge; the response is stored for future retries
Solid arrows: calls. Dashed: replies. Red: errors. Amber: conflicts. Dashed blue boxes: database transactions (each IdempotencyReserver method opens its own); amber boxes: alternative branches.

What to look for