Application Security

Design Idempotent Web Forms and Submission Endpoints

Prevent duplicate actions from repeated clicks, browser retries and network timeouts using explicit request identity.

Users can submit a form twice by clicking repeatedly, refreshing a page or retrying after a timeout. Network intermediaries and browsers can also repeat requests. For actions that create records, charge accounts or send important messages, the application should define how duplicate submissions are handled.

Identify the operation

Create a unique submission token for the intended operation and associate it with the authenticated user or session. Store the token with the result under a database uniqueness constraint. A repeated request with the same token should return the original result rather than performing the action again.

Make the database enforce uniqueness

Application-level checks alone can race when two requests arrive at nearly the same time. Use a unique index or another atomic database mechanism to ensure only one operation claims the token.

Distinguish retries from new intent

A new form submission should receive a new operation token. Do not reuse a token indefinitely across unrelated actions. Expire or clean up token records according to a documented retention policy.

Protect external side effects

If the operation triggers an external API, use that provider's idempotency mechanism when available. Otherwise, persist the operation state and use a recoverable delivery process. A local token cannot guarantee exactly-once behavior in an external system.

Checklist

  • Assign a unique key to each intended operation.
  • Enforce uniqueness in the database.
  • Return the original result for safe retries.
  • Use provider idempotency keys for external actions.
  • Test simultaneous duplicate requests.
Share this page Help other developers discover this resource.