Prompt
How do I build a reliable sync between my product and a customer CRM?
Latest observation
Building a reliable sync between your product and a customer CRM is mostly about choosing the right sync model, being explicit about data ownership, and engineering for retries, idempotency, and drift.
Here’s a practical blueprint.
1) Start by defining the sync contract
Before writing code, decide:
- Which objects sync: contacts, companies, deals, tickets, notes, activities, custom fields, etc.
- Direction:
- One-way: product → CRM or CRM → product
- Two-way: both directions
- System of record for each field:
- Example: your product owns
lifecycle_stage, CRM ownsowner, both can updatenotes
- Example: your product owns
- Sync timing:
- Real-time via webhooks
- Near real-time via polling
- Batch/scheduled sync
A reliable sync depends on avoiding ambiguity. If both systems can edit the same field, you need conflict resolution rules.
2) Use stable IDs and an ID mapping layer
Never rely on email, name, or company name as the primary key.
Maintain a mapping table like:
internal_idcrm_providercrm_object_typecrm_object_idlast_synced_atsync_versionoretagsync_status
This lets you:
- match records safely
- handle renamed/changed entities
- support reconnects and re-imports
- avoid duplicate creation
3) Make every write idempotent
Assume requests will be retried.
Design updates so that the same event can be applied more than once without causing duplicates or corruption.
Practical ways:
- Include an idempotency key per sync event
- Store processed event IDs
- For create operations, check whether the mapping already exists before creating
- For updates, use “upsert” logic where supported
This is one of the biggest reliability wins.
4) Use webhooks for change detection, polling for reconciliation
A robust pattern is:
- Webhooks for fast event capture
- Scheduled polling/reconciliation to catch missed events
Why both?
- Webhooks can be dropped, delayed, or misconfigured
- Polling alone can be slow and rate-limit heavy
Recommended approach:
- Webhook arrives
- Persist event immediately
- Queue sync job
- Worker applies change
- Nightly reconciliation job compares records and repairs drift
5) Put a queue between ingestion and processing
Do not sync directly in the webhook request path.
Instead:
- webhook receiver validates and stores the event
- enqueue job
- worker processes asynchronously
This gives you:
- retry capability
- backpressure handling
- better observability
- protection from CRM API slowness/outages
Use a dead-letter queue for repeatedly failing jobs.
6) Handle conflicts explicitly
If both systems can edit the same record, choose a conflict policy:
Common policies:
- Last write wins
- Source priority wins (e.g. CRM always overrides product on certain fields)
- Field-level ownership (best for most cases)
- Manual review for conflicting updates
If you can, avoid symmetric ownership on the same fields. Field-level ownership is usually the cleanest.
7) Track sync state and versions
For each synced object, store:
- last successful sync timestamp
- last seen external updated_at
- last processed external event ID
- hash/checksum of mapped fields
- version/etag if the API provides one
This helps you:
- detect stale state
- avoid unnecessary writes
- identify drift
- resume after failures
8) Build for rate limits and partial failures
CRMs often have:
- strict rate limits
- intermittent 429s/5xxs
- batch endpoints with partial success
Implementation tips:
- exponential backoff with jitter
- respect
Retry-After - chunk batch requests
- retry only transient failures
- treat validation errors as non-retryable
- log per-record outcomes in batch operations
9) Normalize and validate data carefully
CRM schemas are often messy.
Watch out for:
- required fields differing by CRM
- enum/value mapping differences
- phone/date/timezone formatting
- custom field types
- truncation limits
- multi-select normalization
Create a transformation layer:
- internal canonical model
- provider-specific adapters
This keeps CRM-specific quirks out of your core app logic.
10) Make deletions and merges first-class cases
CRM records get:
- deleted
- archived
- merged
- deduplicated
Your sync needs to know what to do when that happens:
- soft delete vs hard delete
- propagate delete or stop syncing
- if records merge, remap internal IDs to the surviving external record
- detect “not found” responses and reconcile mapping
11) Add observability from day one
You need to answer:
- What changed?
- Why did the sync fail?
- Which record is out of sync?
- Is the system falling behind?
Log and metric ideas:
- events received per provider
- jobs processed / failed / retried
- webhook lag
- API error rates
- time since last successful sync
- count of drifted records
- reconciliation mismatches
Also keep a searchable sync audit trail per record.
12) Design for onboarding and re-syncs
Customers will:
- connect the wrong CRM account
- revoke permissions
- ask for a full re-sync
- change field mappings later
Support:
- initial backfill sync
- selective re-sync by object type/date range
- reconnect flow
- config versioning for field mappings
- ability to pause/resume sync
13) Recommended architecture
A common reliable architecture looks like this:
- Connector
- auths to CRM
- receives webhooks / polls API
- Event store
- persists incoming changes
- Queue
- work items for sync jobs
- Worker service
- transforms and writes data
- handles retries and conflict rules
- Mapping store
- internal IDs ↔ CRM IDs
- Reconciliation job
- periodic consistency checks
- Admin/ops UI
- sync health, failures, replay, manual fix tools
14) A simple reliability checklist
If you want a minimum viable robust sync, make sure you have:
- stable ID mapping
- idempotent writes
- async queue-based processing
- webhook + polling reconciliation
- field ownership rules
- retry/backoff with dead-lettering
- rate limit handling
- sync audit logs
- re-sync/replay tools
15) If you’re choosing between sync styles
A quick rule of thumb:
- Product metadata to CRM: usually one-way, event-driven, easy
- Lead/contact updates both ways: two-way but with field ownership
- Complex revenue workflows: likely needs explicit workflow mapping and reconciliation
- Mission-critical customer data: build reconciliation and ops tooling from the start
If you want, I can also give you:
- a reference architecture diagram,
- a database schema for sync state, or
- a step-by-step implementation plan for a specific CRM like Salesforce, HubSpot, or Pipedrive.