I clicked twice: how to prevent duplicate operations in an app

A double click can produce two requests. A dropped connection can cause a client to retry after the server has already completed an operation. Preventing duplicates means designing for that second attempt too.

Original editorial diagram 01 / QUESTION 02 / EVIDENCE 03 / DECISION
↗ Original conceptual diagram Not a product screenshot
In this guide ⌄

Disabling a button improves the interface. A guarantee about the operation, however, requires a check in the system managing the data.

What idempotency means

According to RFC 9110, section 9.2.2 , a method is idempotent when repeated identical requests have the same intended effect as a single request. The specification includes PUT, DELETE and safe methods. It does not require identical response codes or identical logs on every attempt.

POST does not automatically provide this property. An application can nevertheless define a deduplication protocol: recognise the same intention, retain its outcome and return it when a matching retry arrives.

If the client has not received a response, it does not yet know whether the server performed the operation.

The lost-response problem

Imagine creating a booking. The server records it, but the connection drops before the phone receives confirmation. The phone repeats the request and the server creates another booking.

No distracted user is required: each component may have behaved reasonably. What is needed is a stable operation identifier, reused by attempts representing the same intention.

Case Proposed behaviour Required check
Same key, same data Return the recorded outcome Compare the content
Same key, different data Report a conflict Request fingerprint
New key Evaluate a new operation Permissions and validation

This is an application contract proposed for the example. A real service must also document the key's scope, retention period and treatment of operations still in progress.

Why the check must reach the database

Two processes may simultaneously ask whether a key exists. If both read “absent” before either writes, a check performed only in application code can let both proceed.

A unique constraint with the correct scope, such as the account-and-key pair, lets the database reject the duplicate. The PostgreSQL constraints documentation explains these rules and their role in data integrity. Even in a SQLite lab, the constraint must be enforced rather than merely described in a comment.

Recording the key, performing the local effect and recording the result must be coordinated. If a key is recorded before the operation without managing states, an interruption can leave a request marked as seen but never completed.

A local lab with two attempts

DEMONSTRATION LAB

Demonstration booking without external services

The lab creates SQLite tables for requests and bookings. A transaction records account, key, content fingerprint and result together with the booking.

When the same key is repeated with the same data, the test checks that the returned identifier matches and that only one booking exists. Reusing the key with different content must produce a conflict.

The lab tests local behaviour and includes two concurrent connections. It does not simulate payments, email delivery or distributed transactions.

A call to an external service introduces another boundary. The local database may commit while the remote outcome remains uncertain, or the reverse may happen. That case requires a protocol with the provider and a reconciliation strategy; a local transaction cannot make an operation spanning two systems atomic.

Designing for retries

  1. 1

    Define what identifies a single user intention.

  2. 2

    Associate the key with the authenticated account and operation type.

  3. 3

    Compare the data too: a key must not silently authorise different content.

  4. 4

    Prevent concurrent races with rules enforced by the database.

  5. 5

    Retain and return the outcome defined by the application contract.

  6. 6

    Test interruptions, concurrency, key expiry and external systems.

Do not use a session token as a universal operation key: a session contains many different intentions. Key retention also needs a design. Once its record has been deleted, a late retry may no longer be recognised.

A pre-release checklist

CHECKLIST

Check your project

0/6

This is a useful topic to address in a Row Tech project before the first duplicate occurs: a confirmation screen describes an outcome the backend needs to be able to demonstrate.

Download the lab

Download the demonstration lab (ZIP) . The test uses local SQLite, two concurrent connections and content checking. It performs no payments or sends.

Sources and scope

Antonio Ferone

Written by

Antonio Ferone

Personal projects, technical explanations and learning experiences.

Author profile ↗

THE NEXT STEP

Apply the method to your project

For a professional project, discuss sources, constraints and responsibilities with the Row Tech team.

Talk to Row Tech ↗

Frequently asked questions

The questions that matter

Is disabling the button enough?

No. The server may receive repeated requests because of automatic retries or interrupted connections too.

Does idempotent always mean the same response?

No. The property concerns the intended effect, not equality of every response detail.

Can an application make POST idempotent?

It can define an explicit deduplication contract. Do not assume one from the HTTP method alone.

Does a UUID key solve everything?

No. It must be reused correctly, with checks on content, scope, concurrency and retention.

Does a transaction protect a remote payment too?

Not automatically. An external service has its own state and requires coordination.

Is the lab production-ready?

No. It is a bounded local example for testing certain properties and discussing what is still missing.

Continue reading

Projects and technical notes

Explore projects ↗

Explore related content

Find articles, projects and resources on the same themes.