Local-First Sync & CRDTs vs Optimistic UI & Rollback Safety
Optimistic UI rolls back if the server rejects; Local-First treats local storage as primary and merges via CRDTs.
Structured Comparison Matrix
| Dimension | Local-First Sync & CRDTs | Optimistic UI & Rollback Safety |
|---|---|---|
| Mental Model | Persists every edit locally in IndexedDB/OPFS at 0 ms network wait and uses Conflict-free Replicated Data Types (CRDTs) so offline peers merge deterministically. | Applies speculative UI state immediately on user action while retaining an immutable confirmed snapshot for atomic rollback if the network request fails. |
| Maturity & Domain | State & Architecture | State & Architecture |
| Prerequisites | ||
| When Not to Use | When not to use CRDTs: For strict financial ledgers, inventory counts, or unique-username claims where two offline writes cannot both succeed, use server-authoritative transactions instead. | When not to use Optimistic UI: Never fake success on irreversible or high-stakes actions, payment charges, inventory reservations, password changes, or destructive account deletions; for local multi-step editing history, use Undo/Redo & Structural Sharing instead. |
Engine Mechanics & Trade-Offs
Writes mutations locally first at 0ms network wait, encoding operations with causal/Lamport clocks so offline peers merge deterministically upon reconnection.
Key Pitfalls: Unbounded tombstone growth: Deleted characters in sequence CRDTs stay as invisible tombstones so concurrent edits know where they sat; without snapshot compaction, a 10 KB note can bloat to 50 MB of history. Wall-clock Date.now() tie-breakers: Relying on a user's system clock for Last-Write-Wins lets a laptop with a cl
Separates confirmed server state from an ephemeral speculative overlay, using mutation sequence IDs and rollback snapshots to preserve consistency under out-of-order network responses.
Key Pitfalls: Out-of-order toggle races: Rapidly clicking Like -> Unlike fires two requests; if Request #1 resolves slower than Request #2, naive reconciliation flickers the UI back to Liked. Use a monotonic request counter or AbortController. Fake IDs leaking into URLs: Creating a comment optimistically with id: "temp-123" and lett