Data synchronization between systems keeps every team working from the same records. This guide builds sync workflows on n8n 2.x: webhook-driven sync with conflict detection, incremental sync with checkpoints, and a scheduled consistency check across systems.
Petru Constantin is the founder of DeviDevs and designed the multi-agent platform the company runs on, with guardrails enforced in code. Code examples are written against the n8n 2.47 (stable channel) documentation.
Bidirectional CRM Sync
Sync data between CRM and other systems:
// Workflow:
// Webhook "CRM Webhook" (POST, Authentication: Header auth)
// -> Data Table "Get": sync_state row where entity_id = {{ $json.body.entity_id }}
// (node setting Always Output Data on, so a new entity still continues)
// -> Code "Resolve Conflict" (this node)
// -> If proceed
// true: Postgres "Insert or Update" into the entity table,
// then Data Table "Upsert" of the new sync state
// false: Slack "Send" to a data-quality channel with the conflict
// Webhooks arrive often, and the n8n docs warn that workflow static data
// "may behave unreliably under high-frequency workflow executions", so the
// sync state lives in a data table. When you write back to the CRM, skip events
// caused by your own sync user, or every write triggers another webhook.
// Code node "Resolve Conflict", mode: Run Once for Each Item
// $json: the stored state for this entity (empty when there is none)
const event = $('CRM Webhook').item.json.body;
const state = $json;
const actionMap = {
'contact.created': 'create',
'contact.updated': 'update',
'contact.deleted': 'delete',
'deal.created': 'create',
'deal.updated': 'update',
'deal.stage_changed': 'update',
'company.created': 'create',
'company.updated': 'update',
};
const action = actionMap[event.event_type] ?? 'unknown';
const source = event.source ?? 'crm';
const eventTime = new Date(event.timestamp ?? Date.now()).getTime();
const lastTime = state.last_modified ? new Date(state.last_modified).getTime() : null;
let conflict = null;
if (lastTime !== null && Math.abs(eventTime - lastTime) < 5 * 60 * 1000 && state.last_source !== source) {
conflict = 'concurrent_modification';
}
if (event.data?.version != null && state.version != null && event.data.version < state.version) {
conflict = 'version_mismatch';
}
// Strategy: stale versions are blocked; for contacts the CRM wins; otherwise last write wins.
let proceed = action !== 'unknown';
if (conflict === 'version_mismatch') {
proceed = false;
} else if (conflict) {
const priority = { crm: 1, erp: 2, database: 3 };
proceed = event.entity_type === 'contact'
? (priority[source] ?? 99) <= (priority[state.last_source] ?? 99)
: eventTime >= lastTime;
}
return {
json: {
entity_id: event.entity_id,
entity_type: event.entity_type,
source,
action,
data: event.data,
conflict,
proceed,
new_version: (state.version ?? 0) + 1,
last_modified: new Date(eventTime).toISOString(),
},
};Incremental Data Sync
Handle large datasets with incremental sync:
// Workflow:
// Schedule Trigger (every 15 minutes)
// -> Postgres "Get Checkpoint", Execute Query:
// SELECT last_sync_time FROM sync_checkpoints
// WHERE sync_name = 'crm_to_warehouse' ORDER BY last_sync_time DESC LIMIT 1
// (node setting Always Output Data on, so the first run without a checkpoint still continues)
// -> HTTP Request "Fetch Changes" (credential for the CRM API)
// Send Query Parameters: modified_after = {{ $json.last_sync_time ?? '1970-01-01T00:00:00Z' }},
// limit = 100
// Options > Pagination: Update a Parameter in Each Request,
// Type Query, Name cursor, Value {{ $response.body.next_cursor }}
// Set the pagination stop condition on has_more = false (or an empty next_cursor);
// without it the last page is requested again with an empty cursor.
// -> Code "Transform and Validate" (this node)
// -> If _valid
// true: Postgres "Insert or Update" into contacts_staging, matching on id
// false: Postgres "Insert" into sync_errors
// -> Postgres "Save Checkpoint", Execute Query with Query Parameters:
// INSERT INTO sync_checkpoints (sync_name, last_sync_time, records_synced)
// VALUES ('crm_to_warehouse', $1, $2)
// Store the largest updated_at you received, not the current time,
// so records changed during the run are picked up next time.
// Never build INSERT statements by pasting values into SQL: use the
// Insert or Update operation or $1, $2 Query Parameters.
// Code node "Transform and Validate", mode: Run Once for All Items
// Input: one item per page from the paginated HTTP Request, each with a data array.
const isValidEmail = email => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const normalizePhone = phone => (phone ? String(phone).replace(/[^\d+]/g, '') : null);
const records = $input.all().flatMap(item => item.json.data ?? []);
const syncedAt = new Date().toISOString();
return records.map(record => {
const email = record.email ? String(record.email).toLowerCase().trim() : null;
const errors = [];
if (!record.id) errors.push('Missing ID');
if (!email || !isValidEmail(email)) errors.push('Invalid email');
return {
json: {
id: record.id,
external_id: record.external_id,
email,
first_name: record.first_name?.trim() ?? null,
last_name: record.last_name?.trim() ?? null,
full_name: `${record.first_name ?? ''} ${record.last_name ?? ''}`.trim(),
company: record.company_name ?? null,
phone: normalizePhone(record.phone),
created_at: record.created_at,
updated_at: record.updated_at,
source_system: 'crm',
sync_timestamp: syncedAt,
_valid: errors.length === 0,
_errors: errors,
},
};
});Multi-System Data Consistency
Maintain consistency across multiple systems:
// Workflow:
// Schedule Trigger (every 6 hours)
// -> HTTP Request nodes that read the same sample of IDs from the CRM, the ERP and the warehouse
// -> Merge / Code step so each item looks like { id, crm, erp, warehouse }
// -> Code "Compare Systems" (this node)
// -> If inconsistent_records > 0: Slack "Send" to a data-quality channel,
// and a Data Table "Insert" per record into a repair queue that a person reviews
// Three REST APIs give you no shared transaction to roll back: write to the source
// of truth first, fan out from there, and let this check report what drifted.
// Code node "Compare Systems", mode: Run Once for All Items
const criticalFields = ['email', 'phone', 'company'];
const systems = ['crm', 'erp', 'warehouse']; // in priority order: CRM is the source of truth
const rows = $input.all().map(item => item.json);
const report = rows.map(row => {
const issues = [];
for (const field of criticalFields) {
const values = systems
.filter(sys => row[sys] && !row[sys].error)
.map(sys => ({ system: sys, value: row[sys][field] ?? null }));
const distinct = new Set(values.map(v => JSON.stringify(v.value)));
if (distinct.size > 1) {
issues.push({ field, values, recommended: values[0] });
}
}
return { id: row.id, issues };
}).filter(r => r.issues.length > 0);
return [{
json: {
checked_at: new Date().toISOString(),
samples_checked: rows.length,
inconsistent_records: report.length,
consistency_pct: rows.length
? Number(((rows.length - report.length) / rows.length * 100).toFixed(2))
: null,
report,
},
}];Conclusion
Data synchronization needs explicit handling of conflicts, incremental updates and drift between systems. In n8n 2.x that means a data table or database for sync state instead of workflow static data on busy webhooks, the HTTP Request node's built-in pagination with a checkpoint for large datasets, Postgres Query Parameters or the Insert or Update operation instead of SQL built from strings, and a scheduled consistency check whose findings a person reviews before anything is repaired.
From production
In JARVIS, the multi-agent system DeviDevs runs on, a script deduplicates records in both directions between the DeviDevs cold outreach list and a Google Sheet that other people also edit. Because each write replaces the whole file, the script reads the sheet's modifiedTime before and after, refuses to write if the file has changed in between and exits with a dedicated code. Discovery runs on a local snapshot, so it does not depend on OAuth or the network. In an n8n sync workflow this is optimistic concurrency (check a version field before you overwrite): a record that changed under you stops the write.
Official references
Related on this site: AI-Powered n8n Workflows: Integration Patterns, n8n Data Transformation: ETL Pipeline Workflows, n8n Email Automation: Complete Workflow Implementation Guide. More articles in the n8n Automation category.