Duplicates can come from retries, double submissions, or multiple sources claiming the same entity. Handling them means: preventing duplicates (unique constraints, idempotency keys), detecting them (dedup keys, matching rules), and resolving them (merge, keep one, flag for review).
| Strategy | How |
|---|---|
| Unique constraint | DB UNIQUE on (user_id, external_id) so duplicate insert fails |
| Idempotency key | Store (key → result); retries return same result, no second insert |
| Upsert | INSERT ... ON CONFLICT DO UPDATE so “duplicate” becomes update |
| Dedup in batch | Before insert, query or hash to detect existing; skip or merge |
Define a business identity (e.g. email, external_id, composite key) and enforce it with constraints or idempotency. For existing duplicates, run one-off dedup (merge/delete) and then enforce uniqueness going forward.