Prompt
I need advice on handling retries, duplicates, and reconciliation when syncing invoice data to accounting systems. What pattern is best?
Latest observation
For syncing invoice data to accounting systems, the most robust pattern is usually:
Outbox + idempotency + reconciliation
With a workflow-oriented sync design rather than “fire-and-forget” API calls.
Why this is the best fit
Accounting integrations commonly face:
- transient API failures
- duplicate webhooks / retries
- partial writes
- eventual consistency
- manual corrections in the accounting system
- differences between internal invoice state and external posted state
A good pattern needs to handle all of these without creating duplicate invoices or drifting out of sync.
Recommended pattern
1. Use an internal source of truth
Keep invoice state in your own database as the canonical record.
Store:
- internal invoice ID
- external accounting system ID
- sync status
- last successful sync timestamp
- last attempted payload hash/version
- retry count / error state
- correlation IDs
2. Write to an outbox/event table
When invoice data changes, write a durable sync job/event in the same transaction as the invoice change.
Example:
- invoice created
- invoice updated
- payment applied
- credit note issued
Then a worker processes those events asynchronously.
This avoids losing sync intent if the process crashes after the invoice commit but before the API call.
3. Make external writes idempotent
Always send an idempotency key or equivalent unique external reference.
Best options:
- use your internal invoice ID as an external reference
- map each invoice to one external record
- include a deterministic idempotency key per operation/version
If the accounting API supports idempotency headers, use them. If not, use:
- upsert semantics if available
- lookup-by-external-reference before create
- strict deduplication on your side
4. Treat sync as a state machine
Don’t model sync as a single boolean.
Use states like:
- pending
- sending
- synced
- retrying
- failed
- needs_reconciliation
This helps you distinguish:
- “API temporarily unavailable”
- “record created but response lost”
- “duplicate request detected”
- “external system manually edited”
5. Use retries with backoff, but only for safe operations
Retry only when the operation is safe to repeat or is idempotent.
Recommended retry rules:
- retry network errors, 5xx, 429
- backoff exponentially with jitter
- stop after threshold
- send to dead-letter / reconciliation queue after repeated failure
Avoid blind retries on non-idempotent create calls unless you have idempotency guarantees.
6. Reconcile periodically
Even with good retries, you need reconciliation because accounting systems can drift.
Run scheduled reconciliation jobs that:
- compare internal invoices vs external records
- detect missing external records
- detect mismatched totals/status/tax/payment state
- re-fetch external objects and compare hashes or key fields
- flag exceptions for manual review
This is especially important if:
- users can edit invoices in the accounting system
- the external API is eventually consistent
- webhooks can be lost or duplicated
The practical “best pattern” in one sentence
Use an outbox-driven async sync worker with idempotent external writes and scheduled reconciliation against the accounting system.
What to avoid
Avoid:
- direct synchronous writes with no persistence of intent
- assuming retries are harmless without idempotency
- using only webhooks as your truth
- one-way sync without periodic reconciliation
- relying on “did it succeed?” based only on HTTP response if the connection can drop after the provider processed it
A good implementation blueprint
On invoice change
- Save invoice update in DB
- Insert sync event into outbox
- Commit transaction
Worker
- Pull event
- Mark as processing
- Send to accounting API with idempotency key
- Save external ID and success state
- On failure, retry with backoff
- On repeated failure, mark for reconciliation/manual review
Reconciliation job
- Query internal invoices in sync scope
- Query external invoices by date/status/reference
- Compare records
- Repair missing/incorrect mappings
- Escalate ambiguous cases
If you want the shortest answer
Best pattern: transactional outbox + idempotent API calls + retry/backoff + periodic reconciliation.
If you want, I can also give you:
- a reference architecture diagram
- a database schema for sync tracking
- retry/reconciliation rules by failure type
- a concrete pattern for Stripe/Xero/QuickBooks-style APIs
Brands