A customer books a slot, and your app shows two bookings. An order appears twice. A sign-up creates two rows. Someone gets two confirmation emails, or two invoices. Apps built with Lovable, Bolt, Cursor and Replit, usually on Supabase or Postgres, often have this problem. The code works when you test it once, slowly. It breaks when real people click fast, networks are slow and other services send the same message twice. Vendor facts below come from each vendor's own docs, checked in November 2026.
Short answer: Duplicates come from the same action arriving more than once. Fix it in three layers. First, lock the button while the request runs. Second, let the database refuse a second copy with a unique constraint. Third, for anything that creates something or charges money, send an idempotency key so a repeat is recognised and ignored. The screen fix is nice to have. The database fix is the real guarantee. Clean up old duplicates before you add the constraint, and take a backup first.
1. Why the same thing gets saved twice
Most duplicates have one of these causes. Often it is more than one.
Double clicks and double taps
A user clicks "Pay" or "Book now" twice. Phones make this worse, because a tap with no visible reaction invites a second tap. If the button stays active while the first request is running, both requests are sent. Both succeed. You now have two rows.
Slow network and a user who retries
The request is slow, so the page looks frozen. The user reloads, or goes back and submits again. The first request may have worked. The user only never saw the answer. Now there are two.
The browser or your code retries
Some code retries a failed request on its own. A generated data layer, a network library or a mobile app may send the call again after a timeout. If the first call actually reached the server, the retry creates a second record. The key point: a timeout does not mean "it failed". It means "no answer arrived".
React effects running twice in development
If your app does a write inside a React effect, you may see two rows while building. The React docs for StrictMode say it "enables the following development-only behaviors", and one of them is that components will "re-run Effects an extra time". The same page states: "All of these checks are development-only and do not impact the production build." So this is a development effect, not the cause of duplicates on your live site. But it is a warning. A write inside an effect is fragile, and it is better to move it to a button click or to make it safe to repeat.
Webhooks delivered more than once
Services like Stripe call your app when something happens. Stripe's webhook docs say: "Webhook endpoints might occasionally receive the same event more than once." Your code must expect repeats. If each event creates an order or sends an email, a repeat creates a second order or a second email.
Scheduled jobs that overlap
A job runs every minute. One run takes longer than a minute. The next run starts while the first is still working, and both process the same items. Or a job is retried after a failure, and part of its work was already done. This is how people get two reminder emails.
2. Fix layer 1: the screen
Make the button stop accepting clicks as soon as it is pressed. Set a "saving" state, turn the button off, and show progress, for example "Saving..." with a spinner. Turn it back on when the request ends, also when it fails, so the user can try again.
MDN's page on the disabled attribute says that for a button it "prevents the user from interacting with the button." That is what you want during a save.
Also do this:
- Keep the user on the page until the answer comes. Do not navigate away while a write is running.
- After success, show a clear "Done" message or move to a confirmation page. A silent success invites a second click.
- Ignore a second submit in the code too, not only by greying the button. Pressing Enter in a form can submit again.
This layer is not a guarantee. It only protects one browser tab. It does nothing for a retry from a second device, a webhook or a job. That is why you need layer 2.
3. Fix layer 2: the database says no
The database is the only place that sees every request. Ask it to refuse a second copy.
Unique constraints
The PostgreSQL docs on constraints say: "Unique constraints ensure that the data contained in a column, or a group of columns, is unique among all the rows in the table." When a second row would break that rule, the database refuses it with an error, and no duplicate is stored. This holds no matter which screen, device or job sent the request.
The docs also note that "By default, two null values are not considered equal in this comparison." So if a column in your rule can be empty, two rows with an empty value will both be allowed. Make the important columns required.
Choosing the natural key
A unique constraint needs a rule for "what counts as the same thing". This is the natural key. Pick the columns that identify the real-world item, not the automatic row id (every row gets its own id, so the id never repeats). Examples:
- A booking slot: user id plus date plus time slot. Or room id plus start time, if a room can only be booked once.
- A sign-up: the email address, in lower case.
- A webhook event: the event id that the other service sends.
- An order from a checkout: the checkout session id from your payment provider.
- A daily report for a user: user id plus date.
Ask yourself: "If two rows had these same values, would it be a mistake?" If yes, those columns belong in the constraint. Be careful not to choose too strictly. A user may legitimately place two orders, so "user id" alone would be wrong for orders.
A constraint in SQL looks like this (here for a booking slot): alter table bookings add constraint bookings_user_slot_unique unique (user_id, slot_start);
INSERT ... ON CONFLICT
With a constraint in place, your code should handle the "already exists" case calmly instead of crashing. Postgres has a command for it. The INSERT docs say: "ON CONFLICT DO UPDATE guarantees an atomic INSERT or UPDATE outcome; provided there is no independent error, one of those two outcomes is guaranteed, even under high concurrency." The shape is: insert into bookings (user_id, slot_start) values ($1, $2) on conflict (user_id, slot_start) do nothing;
"Do nothing" quietly skips the repeat. "Do update" changes the existing row instead. For a booking or an order, "do nothing" is usually the right choice.
Upsert in Supabase
If your app uses the Supabase JavaScript library, the same idea is called upsert. The upsert reference says it will "Perform an UPSERT on the table or view." Depending on the columns passed to onConflict, it lets you insert a row if none exists, or "perform an alternative action depending on ignoreDuplicates." In practice you pass the same columns as your unique constraint: supabase.from('bookings').upsert(row, { onConflict: 'user_id,slot_start', ignoreDuplicates: true })
The docs page checked for this post does not describe the two options in detail. Test what your project returns on a repeat before you rely on it. The constraint must exist in the database for onConflict to work, so add it first.
4. Fix layer 3: idempotency keys
Some actions have no good natural key. A payment is the clear example. The same customer may buy the same product twice on purpose. Here you use an idempotency key: a unique code that the client makes once per action and sends with every attempt of that action. The server remembers the code and does the work only once.
Stripe's idempotent requests page explains it: "The API supports idempotency for safely retrying requests without accidentally performing the same operation twice." If a connection error happens, "you can safely repeat the request without risk of creating a second object or performing the update twice." Stripe suggests V4 UUIDs for keys, notes they are up to 255 characters long, and says not to use sensitive data such as email addresses as keys. For API v1, keys can be removed automatically once they are at least 24 hours old. So a key protects against quick retries, not against a repeat a week later. The key is sent in an Idempotency-Key header.
The same idea works for your own functions, for anything that creates something, sends an email or charges money:
- The page creates a random key when the form opens, or when the user starts the action. It reuses the same key for every retry of that action.
- The function receives the key and saves it in a column with a unique constraint.
- If the insert works, the function does the job. If the database says the key already exists, the function returns the earlier result and does nothing else.
Send the email or charge the card only after the insert has succeeded. That order is the whole trick. If you send first and save second, a retry sends twice.
For the Stripe launch checks themselves, see Stripe payments in an AI-built app: launch checks. This post only covers the repeat problem.
5. Webhooks and scheduled jobs
Webhooks: remember the event ids
Stripe's docs give the advice directly: "You can guard against duplicated event receipts by logging the event IDs you've processed, and then not processing already-logged events." Make a small table, for example processed_events, with the event id as a unique column. When an event arrives, try to insert its id first. If the insert fails because the id exists, stop and return a success status. If it works, do the job. If the job itself then fails, remove that id row again (or mark it as failed) and return an error, so the next retry can still do the work instead of being skipped.
Two more points from the same page. Stripe says that "In some cases, two separate Event objects are generated and sent", and suggests using the ID of the object in data.object together with the event type to spot these. And it says not to use the created time to decide whether you have already processed an event. Use ids.
Also return a success status quickly. Stripe's docs say your endpoint "must quickly return a successful status code (2xx) before any complex logic that could cause a timeout." A slow handler leads to a retry, which leads to a duplicate if you did not guard against it.
Jobs: safe to run twice
Design every scheduled job so that running it twice gives the same result as running it once. Ways to do that:
- Mark each item as done in the database, and select only items that are not done.
- Record "reminder sent" with a unique constraint on user plus reminder type plus date, and insert that row before sending the email.
- Use a lock or a "running" flag so a second run exits when the first is still going.
See scheduled jobs on Netlify, Vercel and Supabase for how jobs are triggered and why overlaps and retries happen.
6. Cleaning up duplicates you already have
Expect the constraint to fail while duplicates exist: the PostgreSQL docs say that if data would violate a constraint, "an error is raised". So remove the extra rows first, in this order.
- Back up first. Deleting rows cannot be undone. See database backups for Supabase, Neon and Firebase.
- Find the duplicates. Use the same columns as your future constraint:
select user_id, slot_start, count(*) from bookings group by user_id, slot_start having count(*) > 1; - Decide which row to keep. Look at the rows in each group. Often you keep the oldest, because other data may already point to it. But check. One copy may be paid and the other not. Another table may link to one of them (invoices, messages).
- Move or merge links first. If other tables point to the rows you will delete, point them to the row you keep, then delete the extras.
- Delete the extras, then add the constraint. Try it on a copy or a preview database first. See changing your database without losing data.
- Check the count query again. It should return no rows.
Duplicates that already caused real effects, such as a double charge, need a manual fix too. Refund or correct them in your payment dashboard, and tell the customer.
7. Prompts for your AI tool
Adapt these to your tables. Ask the tool to show the plan before it changes anything.
- "Find every place where this app inserts rows or sends emails. For each, tell me what would happen if the same request arrived twice."
- "Disable the submit button and show a saving state while the request runs. Re-enable it on error. Also ignore a second submit in the handler."
- "Write a SQL migration that adds a unique constraint on (user_id, slot_start) in the bookings table. First give me a query that lists existing duplicates. Do not delete anything yet."
- "Change the booking insert to use upsert with onConflict on those columns, and show a friendly message when the slot is already taken."
- "Create a processed_events table with a unique event_id. In the webhook function, insert the event id first and return 200 without doing the work if it already exists."
- "Make the order function accept an idempotency key from the page, store it with a unique constraint, and send the confirmation email only after the insert succeeds."
- "Move the database write out of the useEffect and into the click handler."
8. Checklist
- Every submit button is disabled while saving and shows progress.
- Success is shown clearly, so nobody clicks again.
- Each table that must not have repeats has a unique constraint on its natural key.
- Columns inside those constraints are required, not empty.
- Inserts use ON CONFLICT or upsert, and the code handles the "already exists" answer.
- Actions that create or charge use an idempotency key.
- Emails and charges happen after the database insert, not before.
- Webhook handlers store processed event ids and skip repeats, and return 2xx quickly.
- Scheduled jobs are safe to run twice and cannot overlap.
- No database write sits inside a React effect.
- Old duplicates were found, backed up, merged and removed before the constraint was added.
- You tested by double clicking, by sending the same webhook twice and by running the job twice.
Want this fixed for you?
I finish and fix apps built with Lovable, Bolt, Cursor or Replit, including duplicate orders, bookings and emails caused by double clicks and retries. Send me the details and I'll reply within 24 hours.