Understanding Conflicts
Conflicts arise in these scenarios:- Concurrent Edits: Multiple users edit the same data simultaneously
- Offline Changes: User makes changes offline, server state changes meanwhile
- Failed Syncs: Local changes fail to sync, remote state advances
- Delayed Responses: Slow network causes response to arrive after new changes
Built-in Conflict Prevention
Legend-State includes several mechanisms to prevent conflicts:Pending Changes Tracking
The sync system tracks which changes haven’t been confirmed by the server:Generation Tracking
Changes are tracked with generation numbers to prevent stale updates:Optimistic UI with Rollback
Changes apply immediately but can be reverted on error:Conflict Resolution Strategies
Strategy 1: Last Write Wins
Simplest strategy - most recent change wins:Strategy 2: Server Wins
Server state always takes precedence:Strategy 3: Client Wins (Retry)
Keep retrying until local changes succeed:Strategy 4: Deep Merge
Merge local and remote changes:Strategy 5: Manual Resolution
Let user decide how to resolve conflicts:Strategy 6: Operational Transformation
Transform operations to work on any state:Handling Specific Conflict Scenarios
Scenario: Concurrent Field Updates
Different users edit different fields:Scenario: Array Conflicts
Handling conflicts in lists:Scenario: Delete Conflicts
Handling deleted items:Scenario: Offline Divergence
User is offline for extended period:Preventing Conflicts
Use Incremental Sync
Only sync changes, not full state:Debounce Rapid Changes
Batch rapid edits into single sync:Use Timestamps
Let server detect conflicts via timestamps:Use Version Numbers (Optimistic Locking)
Monitoring Conflicts
Track Sync Errors
Monitor Pending Changes
Check Sync State
Advanced Patterns
Custom Conflict Resolution
Conflict-Free Replicated Data Types (CRDTs)
Three-Way Merge
Best Practices
Always enable retrySync for critical data
Always enable retrySync for critical data
Use timestamps for conflict detection
Use timestamps for conflict detection
Prefer object/Map over arrays
Prefer object/Map over arrays
Handle errors gracefully
Handle errors gracefully
Use soft deletes for critical data
Use soft deletes for critical data
See Also
- synced() Function - Create synced observables
- syncState - Monitor sync status
- Remote Sync - Sync plugins
- Error Handling - Handle sync errors