Four things go wrong when you sync Zoho CRM with another system in both directions: duplicates, lost updates, deletes and echoes. Each has a fix you can make concrete:
- Duplicates: put an idempotency key on every write, and add a unique constraint on the CRM ID.
- Lost updates: use optimistic locking on both sides (
If-Matchon the external API,If-Unmodified-Sinceon Zoho), and read the record back after every conditional write. - Real conflicts: don't auto-merge. Queue the record and let a person pick the winner.
- Deletes: use tombstones, not hard deletes.
- Echoes: compare content hashes so the sync ignores its own writes.
This post is about a prototype I built to test that design against a real Zoho CRM org. It's not client work. Everything ran in my own test org with synthetic data, and some parts did not get run. Those are listed near the end.
Why is two-way sync harder than it looks?
One-way sync is mostly plumbing. Two-way sync is a distributed-systems problem with two databases, neither of which is the single source of truth. These are the four failure modes I designed against:
| Failure | What it looks like |
|---|---|
| Duplicate | A timeout, the client retries, and now there are two customers on the external side |
| Lost update | Sales edits the phone number in CRM while billing edits it in the ERP; whichever sync runs last silently wins |
| Delete accident | Someone deletes a record on one side and the sync deletes it on the other, past the point where you can restore it |
| Echo | The sync writes to CRM, CRM reports the change, and the sync pushes it back out, forever or until rate limits stop it |
"Last write wins by timestamp" handles none of these safely. The sync's own write-back moves Modified_Time, and clocks on the two systems disagree.
How do you stop duplicates on retries?
Every create sent to the external API carries an Idempotency-Key. On the external side the idempotency record is written in the same transaction as the business record:
- Same key, same request → return the stored response; nothing new is created.
- Same key, different body →
409. - The external table also has
UNIQUE(crm_id), so two different keys for the same CRM record still produce only one row.
On the Zoho side, External_ID is a unique field. I measured how Zoho reports a collision:
- A plain Insert with a duplicate value returns
400 DUPLICATE_DATA, anddetails.duplicate_record.idcontains the existing record's ID. You can switch to the update path without a second lookup. - A batch with one duplicate and one new record returns
207, with per-element results in input order. - Upsert tells you what it did in
data[].details.action(insertorupdate) along with theid. A match on the unique field also returnsduplicate_field. Don't treat HTTP 200 alone as "created". - COQL right after a write can return zero rows because of indexing lag. Verify writes with GET-by-ID instead.
How do you detect a conflict instead of overwriting?
Each synced record stores an agreed state on the CRM side: External_ID, External_Version and Sync_Hash (a SHA-256 of the business fields after normalisation). Before writing in either direction, the sync compares the current hash on both sides with that agreed hash:
| CRM changed? | External changed? | Action |
|---|---|---|
| yes | no | push to external with If-Match: <agreed version> |
| no | yes | pull into CRM with If-Unmodified-Since: <Modified_Time just read> |
| yes | yes, different content | Conflict: overwrite neither side |
| yes | yes, same content | converged, just update the agreed state |
Hashes beat timestamps here because writing sync metadata doesn't change the business-field hash.
The optimistic lock on the Zoho side behaved like this in my org:
- A stale
If-Unmodified-Sincereturns412 ALREADY_MODIFIEDas a normal response. From Deluge'sinvokeurlit doesn't throw, so you branch on the status code andcode. - The comparison has one-second precision. Two conditional writes in the same second both succeeded. A
412alone can't protect you against a same-second race, so every conditional write to CRM is followed by a GET-by-ID read-back that checks the expected hash before the sync treats the write as done.
That second point is the one I'd most want another Zoho developer to know before they rely on If-Unmodified-Since.
How should a person resolve a conflict?
When the sync finds a conflict, it writes a Conflict row to a custom module (Sync_Queue) with both versions: the CRM hash and Modified_Time, and the external hash and version. Nothing is overwritten.
An admin picks "CRM wins" or "external wins". The choice records which versions the person saw. On the next run the sync re-reads both sides. If either changed after the person decided, it doesn't apply the decision and sends the row back for another look.
In the prototype, the vertical-slice test for this (V-03) passed 9 of 9 checks. Both sides were edited, the sync detected both_changed and overwrote neither, and after resolving "CRM wins" the external record was at version 4, both sides matched, and the status was back to Synced.
How should deletes propagate?
They shouldn't propagate destructively. The design uses tombstones on both sides and leaves physical deletion to a person:
- Deleted in CRM → the external record gets
deleted_atand later updates to it return410. It can be restored. - Deleted externally → the CRM record is not deleted. It gets
Sync_Status = Deleted_External, and a person decides. - Delete on one side and edit on the other → conflict.
I measured what happens when a record is restored from the Zoho recycle bin:
- It comes back with the same record ID, and field values (including
External_ID) are preserved. Modified_Timechanges to the restore time, so a modified-time scan picks up the restore automatically.- With the scopes I had, restoring through the API returned
401. Only the UI restore worked.
How do you stop the sync from echoing its own writes?
There are two layers:
- Don't trigger automation with sync writes. On Insert via
invokeurl,"trigger": []stopped the workflow, but{}did not. I checked this with three positive and three kinds of negative controls. (I haven't measured whether the same holds for Update/Upsert.) - Skip changes you made yourself. When an incoming change's hash equals the stored
Sync_Hash, or its version is at or below the agreedExternal_Version, it's skipped.
In the echo test (V-05), the change left in the external feed after the conflict resolution was skipped with zero writes.
Which other Zoho CRM behaviours should you know?
All measured in a real org (API v8, JP data center, Ultimate trial at the time):
| Behaviour | Result |
|---|---|
COQL id in (...) |
Max 100 IDs; 101 returns 400 LIMIT_EXCEEDED. Chunk your lookups |
| COQL datetime literals | Must include an offset. Without one, with a space separator, or date-only: 400 INVALID_QUERY. +09:00, other offsets and Z compare as the same instant |
| COQL time comparison | Second precision |
zoho.encryption.sha256 |
Matched 7/7 known test vectors (lower-case hex) |
| Calling a function from outside | POST /crm/v7/functions/<name>/actions/execute?auth_type=oauth works, but arguments only arrive via the arguments= query parameter; a JSON body arrived empty. REST execution can only be enabled in the UI |
| Unique field definition | {"case_sensitive": false} works. A malformed value is silently ignored and the field is created without uniqueness |
invokeurl to an unresolvable host |
No exception; responseCode 500 |
Why put the mock external platform on Cloudflare Workers?
The sync needs something to sync with, so I wrote a mock external platform: a REST API with an OpenAPI 3.1 contract, idempotency, versions with If-Match, a change feed, tombstones and restore. The local version is plain Node.js with no dependencies (16/16 tests). I then ported it to Cloudflare Workers (21/21 tests: the same 16 plus 5 specific to Durable Objects).
The interesting design question was atomicity. D1 has no interactive transactions, and the mock needs "read, decide, write" to be atomic for idempotency and unique crm_id. Instead of D1, every request goes to one Durable Object. Its transactions are synchronous code with no await inside, so nothing can interleave. Each commit's changed rows are written in one coalesced storage write. In tests, 10 concurrent creates with the same crm_id produced exactly one record on Node, and 8 concurrent creates did the same on workerd.
There's a cost. Throughput is one object's worth, and attachments stored as base64 can't reach Zoho's 20 MB file limit. I capped them at 5 MB, and on Workers Free about 0.2 MB is realistic because of the 10 ms CPU limit. That's fine for a mock, but it doesn't tell you how to build a production platform.
What did this prototype not prove?
- The Deluge-only version didn't run end-to-end. The vertical slice passed through a Python driver talking to Zoho's REST API, with the mock on the same machine. I wrote the same flow as a Deluge function, registered it, and confirmed its hashes matched the Python reference. Then the org's trial ended and it went back to the Free edition, which blocks the custom modules and the Functions API. I didn't pay to upgrade a prototype org, so "the sync runs purely inside CRM" is not demonstrated.
- The Notification API isn't measured. Registering a watch channel failed with
401 OAUTH_SCOPE_MISMATCH, so the inbound notification path is design only. The mock's notification endpoint hasn't been checked against real Zoho notification payloads. - Not done: attachment sync, Bulk API, Contacts/Cases, lock contention under a scheduled worker, and the full acceptance test list.
Checklist for your own two-way sync
- Every external write has an idempotency key, stored in the same transaction as the change.
- Unique constraints on both sides (
crm_idexternally,External_IDin Zoho), and you know how each side reports a duplicate. - Conflicts are decided by content hash against an agreed base, not by timestamps.
- Zoho updates use
If-Unmodified-Sinceand a GET-by-ID read-back (same-second writes both pass). - Conflicts go to a queue that a person resolves; the decision is re-checked before it's applied.
- Deletes become tombstones or status flags; physical deletion is a human decision.
- Sync writes use
"trigger": [], and incoming changes matching your stored hash are skipped. - COQL lookups are chunked at 100 IDs, and datetime literals always include an offset.
The code (mock platform, Workers port, Python driver, Deluge functions) and the full verification notes are in GitHub repository. It's sample/prototype code, and the README lists what was and wasn't verified.
