Skip to main content

Retrying Reservation Requests Without Duplicates

How an idempotency key helps an API recover when a reservation succeeds but its response gets lost

Updated 3 min readMarkdown
Retrying Reservation Requests Without Duplicates

A traveler reserves two spots on a guided excursion. The server commits the reservation just before the phone loses its connection. The traveler taps Try again when the connection comes back. To the phone, the first attempt looks like a failure. To the server, the second request must refer to the same booking intent. This article uses a fictional reservation API. Tripmap local experiences

Give the booking intent an identity

Generate an idempotency key once when the traveler confirms the reservation. Send it with the initial request and reuse it when retrying the same intent. A new, deliberate reservation gets a new key. This lets the server distinguish two requests that share a payload because one is a retry from two bookings for the same excursion.

POST /reservations HTTP/1.1
Content-Type: application/json
Idempotency-Key: 347a8420-223c-45f4-92b8-f7bd40b8d17b

{"experienceId":"walk-17","partySize":2}

The header name is part of the contract for this example. Clients should retry a non-idempotent request only when the API documents how the retry is safe or the client can determine the first attempt did not apply. HTTP Semantics, section 9.2.2

Make the server enforce the promise

Scope the key to the authenticated caller and the operation. Store a fingerprint of the accepted payload and the reservation reference. Enforce uniqueness on the scoped key at the database level. With a single database, commit the reservation and the replayable result together in one transaction. A second request arriving at the same time then has one owner to wait for or one in-progress operation to report.

What the server sees One possible API response
New key and valid payload Create the reservation and return its reference.
Completed key and matching payload Return the existing reservation reference.
In-progress key and matching payload Report that the operation is pending and give the client a way to check.
Known key and different payload Reject the conflicting reuse.

Publish how long keys remain valid. If a retry arrives after the retention window, give the traveler a way to check for an existing reservation before they submit again.

Reconcile requests that reach another provider

A local transaction cannot cover a remote provider call. When a provider accepts its own idempotency key, pass a stable operation identifier and retain the mapping. If a call times out, look up the operation through a provider reference where the provider supports it. An unresolved outcome belongs in a pending or reconciliation state until the system can establish whether the reservation exists.

Test the failure after the write

Drop the response immediately after the database commits, then send the same request again. Assert that both attempts refer to one reservation. Repeat with two simultaneous requests and with a different payload under the same key. Finally, simulate a provider timeout and check that uncertainty stays visible to the traveler while the system resolves it.

1 view

About the author

  • Tripmap is a Vietnam-based travel technology platform on a mission to connect people with the real world through AI, geospatial intelligence, and digital innovation. We build intelligent travel discovery and booking solutions, combining AI agents, mapping technology, and tourism data to make exploring the world easier, smarter, and more accessible. Here, we share our engineering journey, technical insights, and experiences in building the future of travel technology. Built in Vietnam, for the world. 馃寪 https://tripmap.vn

No comments yet. Be the first to comment.