Idempotency: what happens when an operation is performed twice
Designing retries without duplicating orders: operation identity, uniqueness and transaction boundaries.
Imagine an API receives a POST request to create an order of ten units. The database commits the order, but a network failure prevents the HTTP response from reaching the client. The client sees a timeout. From its perspective, the operation could have succeeded or not run. Repeating the request as an independent creation could register twenty units across two orders.
Identify the business effect
The business rule in this example is that one purchase intention creates one order. A deliberate second purchase of ten units can create another. The design must identify the intention rather than simply compare quantities or request contents.
RFC 9110, section 9.2.2, defines HTTP method idempotency in terms of the intended effect of repeated identical requests. POST has no general idempotency guarantee in that specification; a client can retry when it knows the operation has that semantic. In this design, the API contract would assign a stable key to each intention, which the client retains across retries.
Why concurrency matters
Suppose two requests have the same key K. Request A queries the database and finds no K. Before A inserts, B performs the same query and also finds no K. Both would attempt to create an order. The race arises because checking and writing are separate.
For this scenario, I propose an operation table with non-null tenant_id, operation_type and operation_key columns, and a UNIQUE constraint across all three. PostgreSQL 18 documents that the uniqueness constraint prevents repetition of the combination; NOT NULL avoids the null-value exception. The scope comes from the authenticated and authorised identity. A canonical representation of the relevant request data is also stored to detect incompatible reuse of the key.
One possible transactional strategy
If the order and its idempotency record live in the same database, group the changes in a transaction. The reason is to prevent committing an order without leaving a recoverable result for K.
- After authenticating and authorising the request, start a READ COMMITTED transaction.
- Try to reserve the key with INSERT ... ON CONFLICT (tenant_id, operation_type, operation_key) DO NOTHING RETURNING operation_key.
- If the insert returns a row, create the order, store its identifier and result with the reserved key, and commit. If creation fails, roll back both the order and reservation.
- If no row is returned, query the existing operation in a new SQL statement. PostgreSQL documents that DO NOTHING can skip an insert because of a conflict not visible to that statement's snapshot. In READ COMMITTED the next query obtains a new snapshot. If the intention matches and a committed result exists, return it. For incompatible reuse, return the API's defined conflict response. If the record cannot be found, handle the situation as a recoverable failure under an explicit policy.
This design assumes idempotency records are not removed during the permitted retry period. Retention should reflect how long the client can repeat the operation. Define the handling of timeouts, database errors and previously recorded responses.
The transaction boundary
If creating the order also means confirming an operation in another service, the PostgreSQL transaction protects local changes only. A failure between confirmations can leave divergent states. For that additional requirement, I would examine persistent communication, an idempotent consumer and reconciliation; each addresses a different part of the problem.
How I would verify the design
- Repeat a request after committing the order and simulate a lost response: check that only one order exists and its identifier is unchanged.
- Run two concurrent requests with the same key: check that one order is created.
- Force a failure before commit: check that the reservation and order roll back together.
- Reuse the key with different content: check the API's defined rejection. From a different tenant, verify that no other tenant's order is returned.
This is an illustrative scenario. The cited guarantees come from the HTTP specification and PostgreSQL 18 documentation. The strategy is a design proposal and these acceptance tests remain to be run against an implementation.