Stop duplicate form submissions: front-end guards, idempotency keys, and database constraints
Front-end form: how to prevent duplicate submissions
When building a signup page for a community event, the easiest thing to overlook is the few seconds after clicking submit. When the network is slow, the page seems unresponsive; some people click again, others refresh. The server may have already registered successfully, but the confirmation message hasn't come back yet. As a result, the signup list may end up with an extra record, and the user doesn't know which one to trust. To handle this, first look at how the page intercepts the second action, then look at how the API responds when it receives a second request.
Channel all submissions through one entry point
Make clicks on the button, Enter key presses, and programmatic submissions all go through the form's submit event. For forms that send requests on their own, cancel the browser's default submission at the start of the handler, then check whether a submission is already in progress. After passing the check, set the submission flag immediately before the first async operation; otherwise two triggers could both pass the check. The disabled state of the UI can follow the flag, but the actual interception still happens in the submission handler. When submitting programmatically, use form.requestSubmit() so the browser runs form validation and fires the submit event as usual. Calling form.submit() directly skips both steps.
After the button is disabled, show "Submitting" in a status area near it so screen readers can announce it. Use the data captured at this submission time for the request; leave subsequent edits on the page for the next operation. If you need to disable input controls, capture the data first, because browsers typically exclude disabled fields from form data. On success, show the registration result and its number. If there's a clear field error, keep the entered content so the user can fix and resubmit. Clear the submitting flag when the request finishes, then show success, editable, or pending-confirmation UI accordingly. Place feedback near where the user just acted, so they don't have to scroll between the top and bottom of the page.
Use a single idempotency key for retries
When the browser can't wait for a response, the page can show "Not yet confirmed" and retain the data from this request. If the API supports idempotency keys, the front end generates a random identifier for one submission. On timeout and retry, send the original identifier with the original request body. Even if the first attempt already wrote data, the second attempt can find that same record. If the API explicitly rejects the request and requires field changes, use a new key for the corrected submission. If the result hasn't been confirmed yet, first query the status of the original submission, rather than treating a modified request as a plain retry.
The server needs to bind the idempotency key to the current user and the API operation, store a digest of the key request fields, and keep enough information to restore the result after completion, such as the registration number. If the same key arrives with the same content, return the original result. If the content differs, return a conflict with a clear explanation. The API must also define how long the key is retained, at least covering the allowed retry window. If the page supports re-checking after a refresh, it should be able to recover the existing submission identifier. After the retention period expires, the front end can guide the user to check the registration list.
Let the database catch simultaneous requests
If the implementation only checks whether a key exists before inserting, two concurrent requests can both see an empty result. Put the user ID, API operation, and idempotency key together as a unique constraint, and let the database decide who gets write access. When the registration data and idempotency record live in the same database, put the key occupation, registration creation, and result storage in a single transaction. The side that wins the write completes the transaction; the other side reads the committed result after handling the unique-key conflict. If the first transaction rolls back, the duplicate request can retry per the API contract. For operations that need to be processed slowly outside a transaction, the API can return a "processing" status, and the page can poll for progress instead of holding the connection open.
The idempotency key solves replays of the same operation. Some events also require each account to register only once; in that case, add another unique constraint on the registration table, using the event ID and account ID. With this rule, even if the user registers in two tabs with two different idempotency keys, only one valid registration can exist. Both the list page and the submission result page show the same record number, so the user can confirm they are seeing the same registration.
Deliberately slow down the API during joint debugging
First add a few seconds of response delay to the API, click the button repeatedly, then submit with Enter. Observe whether the page always shows a clear indicator, and confirm from the network panel that only one request was sent. Next, let the first request get its response dropped after the database commit, wait for the "not yet confirmed" notice, then click retry. Verify that the two requests used the same key and that the returned registration number matches the one in the database. Finally, submit from two tabs at the same time and check the actual number of records added to the database. If refresh recovery is supported, refresh the page while the submission result is still unconfirmed and check whether the page can find the pending submission.
I compare what I see in the browser with what the server has recorded. The former shows when a user should wait and when retry is safe; the latter shows whether two requests turned into two registrations. When both sides match, even with fluctuating network speed, users know exactly how far their last operation got.
Top 2 from juejin.cn, machine-translated. The original thread is authoritative.
Retrying with the same key and same content after a timeout is the most critical part.
This three-layer duplicate-prevention approach is solid. Relying only on graying out the button really can't hold up; once concurrency hits, you have to depend on idempotency keys plus unique constraints. Reusing the same key for retries is a thoughtful detail.