System design
Idempotency Explained: Prevent Duplicate Forms, Payments and Jobs
Networks fail in ambiguous ways. A server may complete an action even when the browser never receives the response. Idempotency lets the client retry the same logical operation without creating a second result.
Identify the business operation
A double-click on Submit and a retry after a timeout may represent the same enquiry. Generate an operation ID when the user starts that submission, and keep it while the outcome is uncertain. A new ID on every retry defeats the protection.
Do not reuse the ID for a different request. If the customer changes the amount, recipient or product, treat that as a new proposal with a new identifier and fresh confirmation where needed.
Persist the result atomically
Store the operation ID with its owner, request fingerprint and outcome. Use a uniqueness constraint so simultaneous requests cannot both create the same logical record. An in-memory flag alone disappears on restart and may not cover multiple workers.
When an existing ID is presented, compare the important parameters. Return the recorded result for a matching request and reject conflicting parameters. Do not silently apply a different amount under an old payment identifier.
Handle external systems carefully
Pass an idempotency key to a provider when the relevant API supports it. Save the provider reference locally. If the response is lost, reconcile using that reference or retry the identical operation according to the provider's documented behavior.
Webhook delivery can also repeat. Track event identity and business state so the same confirmed payment does not grant access twice. Event deduplication and order-level safeguards solve related but different failure cases.
Test uncertainty rather than only success
Simulate a completed operation followed by a dropped response. Retry it and verify that only one record, charge or entitlement exists. Also test concurrent requests, changed parameters and a process restart.
Apply this pattern to the business logic behind a CloudWira app or its external backend. A disabled button improves the interface, but it cannot guarantee that the server will never receive the same request twice.
Distinguish a repeated request from a new intention
Imagine a customer requesting a repair quotation on a slow mobile connection. The first request reaches the server, but the response never reaches the browser. The customer taps submit again. If both requests represent the same intention, the application should return the original result rather than create two quotations. A disabled button helps the interface, but it cannot protect against network retries, two tabs or a restarted client.
Generate an operation identifier when the customer begins the submission and reuse it while that operation is unresolved. A genuinely new quotation should receive a new identifier. Define its scope: account plus operation type plus identifier is often more meaningful than a globally accepted string. Store a fingerprint of relevant input so the same identifier cannot quietly be reused with a different amount, recipient or project.
Persist the decision together with the result
The server needs a durable record of the operation's state and outcome. Where the work is entirely inside one database, use an appropriate transaction and uniqueness constraint so two concurrent requests cannot both create the record. Returning “already received” is helpful only if the application can identify the original saved result and show its reference to the customer.
An external payment introduces a second system. Reuse the provider's idempotency mechanism, preserve consistent request parameters and store the provider reference. If the connection fails, query or retry using the same operation identity according to the provider's documented behavior. Do not generate a different payment request merely because the browser did not receive confirmation. Uncertainty about delivery is different from evidence that the action failed.
Handle events that arrive more than once or out of order
Payment and job callbacks may be repeated. Store event identifiers and make fulfillment itself resistant to duplication. A second paid notification should not add the same storage allowance again. An old success event should not reactivate an order already refunded. Check the authoritative object and allowed state transition rather than assuming that arrival order always reflects business order.
For a background job, distinguish accepted, running, completed and uncertain. If a worker stops after sending a message but before recording completion, automatically running the whole job again may duplicate the message. Design reconciliation around the external action reference. Where the destination provides no reliable deduplication, make the uncertain state visible to an operator instead of claiming exactly-once execution that the system cannot guarantee.
Test the awkward moments, not only a double click
Submit identical requests concurrently, drop the response after success and restart the app before the client retries. Reuse the identifier with changed input and verify rejection. Deliver the same callback twice, then deliver a late callback after refund or cancellation. Inspect record counts, entitlements and audit references. These cases test the business invariant rather than just whether a button becomes disabled.
Document how long operation records are retained and when a customer can safely start again. In a CloudWira app, confirm whether the managed submission path provides the behavior you need; a custom external payment or message service needs its own deduplication design. The practical goal is a single understandable customer outcome despite repeated delivery, with a clear reconciliation path when the external result remains uncertain.
Can an idempotency key guarantee exactly-once delivery?
It can support deduplication within a defined system and retention window, but end-to-end guarantees depend on every external boundary. A database uniqueness rule may prevent two local records while a separate message provider still receives two sends. Document what is protected, how long the operation identity is retained and how uncertain external results are reconciled.
Test the actual business invariant: one entitlement, one saved request or no repeated charge. Keep event deduplication separate from fulfillment protection, because two distinct events can refer to the same operation. If you cannot prove the external outcome, preserve an uncertain state and ask for operator review. An accurate status is more useful than a confident success or failure label that causes the customer to repeat an action unnecessarily.