Course/Chapter 7/4. Two-Way Data Sync
    intermediate
    Chapter 7: Real-World Automation Projects

    4. Two-Way Data Sync

    Synchronize data between two platforms with conflict resolution.

    25m Lesson 4 of 5

    Build a bidirectional sync between two systems (e.g., Airtable and Google Sheets) that handles conflicts, incremental updates, and error recovery.

    Sync Architecture

    1. Detect changes: Query both systems for recently modified records
    2. Compare: Match records by a shared unique key (e.g., email)
    3. Resolve conflicts: "Last write wins" or manual review queue
    4. Apply updates: Push changes to the target system
    5. Log: Record what was synced for audit and debugging

    Conflict Resolution Strategies

    • - Last write wins: The most recently modified record takes precedence
    • - Source priority: Designate one system as the "source of truth"
    • - Field-level merge: Merge non-conflicting fields from both systems
    • - Manual queue: Flag conflicts for human review

    Code Examples

    Incremental Sync with Change Detection
    javascript
    // Get records modified since last sync
    const lastSync = $env.LAST_SYNC_TIME || "2025-01-01T00:00:00Z";
    
    const sourceRecords = $input.first().json.source_records;
    const targetRecords = $input.last().json.target_records;
    
    // Build lookup map for target
    const targetMap = new Map();
    targetRecords.forEach(r => targetMap.set(r.email, r));
    
    const toCreate = [];
    const toUpdate = [];
    const conflicts = [];
    
    sourceRecords.forEach(src => {
      const target = targetMap.get(src.email);
      if (!target) {
        toCreate.push(src); // New record
      } else if (new Date(src.updated_at) > new Date(target.updated_at)) {
        toUpdate.push({ ...src, target_id: target.id }); // Source is newer
      } else if (src.updated_at !== target.updated_at) {
        conflicts.push({ source: src, target }); // Conflict
      }
    });
    
    return [{ json: { toCreate, toUpdate, conflicts } }];

    Pro Tips

    • 💡Always use "last modified" timestamps for incremental sync instead of syncing all records every time
    • 💡Implement a "sync_status" field to track which records have been processed
    • 💡Add a "dry run" mode that logs what would change without actually modifying data

    Comprehension Quiz

    Answer 2 of 2 correctly to unlock lesson completion.

    1. What strategy uses the most recently modified record?

    2. What should you use for incremental sync instead of syncing all records?

    Pass the quiz above to unlock lesson completion