Prompt
How do I handle duplicate invoices in an accounting sync?
Latest observation
Handling duplicate invoices in an accounting sync is mostly about idempotency, reliable matching, and clear conflict rules. A good approach is to prevent duplicates first, then detect and resolve them safely when they still happen.
1) Define what “duplicate” means
A duplicate may be:
- The same invoice sent twice from your system
- The same real-world invoice created in two systems
- A retried API request that was processed twice
- A record that matches on some fields but not all
Usually, you want to compare a combination of:
- External invoice ID
- Customer/vendor ID
- Invoice number
- Invoice date
- Currency
- Total amount
- Tax amount
- Status
No single field is enough in all cases.
2) Use an idempotency key
For create/update sync operations, send or store a unique key per source invoice, such as:
source_system + invoice_id- or a hash of stable invoice fields
Then:
- If the same request is retried, return the existing invoice instead of creating a new one.
- Store the mapping between your internal invoice and the accounting system’s invoice ID.
This is the single best way to prevent duplicates.
3) Maintain a cross-system mapping table
Keep a sync table like:
source_invoice_idaccounting_invoice_idsource_systemlast_synced_atsync_statuschecksum/version
When a sync runs:
- Check whether the invoice already exists in the accounting system
- If yes, update it
- If no, create it and save the mapping
4) Do duplicate detection before create
Before creating a new invoice, search the accounting system by:
- Invoice number
- Customer/vendor
- Date range
- Amount
If there’s a strong match, treat it as already synced and either:
- update the existing one, or
- flag it for manual review
Avoid creating based on “looks similar” unless your rules are well-defined.
5) Use a deterministic matching strategy
Create a priority order for matching:
- Exact external ID match
- Exact invoice number + customer/vendor
- Invoice number + date + total amount
- Fuzzy match only for exceptions/manual review
This reduces accidental merges.
6) Decide how to handle true duplicates
You need a policy. Common options:
- Ignore the second one if it is clearly the same invoice
- Link it to the existing invoice and mark as duplicate
- Void/cancel the duplicate in the accounting system if allowed
- Create an exception record for manual review if you’re not sure
If money has already been posted, do not auto-delete unless your accounting rules permit it.
7) Handle retries safely
API retries are a major cause of duplicates. To avoid them:
- Make create operations idempotent
- Retry only on transient errors
- Use the same idempotency key across retries
- Confirm whether the accounting system supports idempotency headers
If it doesn’t, your application should emulate idempotency using your mapping table.
8) Track sync state and versioning
Store invoice lifecycle states such as:
pendingsyncedduplicate_detectedneeds_reviewvoided
Also track a version or checksum so you know whether the source invoice changed after sync.
9) Build exception handling
Sometimes duplicates are ambiguous. For example:
- Same invoice number used twice by a vendor
- Partial payments or credit notes that resemble duplicates
- Reissued invoices with the same number but different amounts
In these cases:
- Don’t auto-merge
- Flag for manual review
- Log the matching evidence used
10) Reconcile regularly
Even with good sync logic, do periodic reconciliation:
- Compare source invoices and accounting invoices
- Identify duplicates created historically
- Repair mappings
- Produce an audit trail
Practical rule set
A simple robust rule set could be:
- If
external_invoice_idexists in mapping → update existing accounting invoice - Else if exact match on
invoice_number + customer + total + date→ treat as duplicate - Else create new invoice and store mapping
- If confidence is below threshold → send to review queue
Things to avoid
- Don’t rely only on invoice number
- Don’t use fuzzy matching as the default path
- Don’t delete duplicates automatically without auditability
- Don’t sync blindly without a source-to-target mapping
Best practice summary
The safest design is:
- Idempotency key
- Mapping table
- Deterministic duplicate detection
- Manual review for ambiguous cases
- Audit log for every decision
If you want, I can also sketch:
- a database schema for duplicate handling, or
- a sync algorithm/pseudocode for implementing it.