Skip to content
Back to blog
Development 11 min

Real-time Sync with CRDTs: Ending Edit Conflicts Forever

Master CRDTs to build collaborative apps without network conflicts. Technical guide on offline-first architectures and eventual consistency.

Diagram of distributed nodes syncing data using CRDT algorithms without a central server

Most developers try to solve concurrency with database locks or naive WebSockets that break as soon as a user enters a subway tunnel or loses connection. The result is the dreaded 'Last Write Wins' scenario, where one user's work silently overwrites another's. To build truly collaborative applications like Figma or Notion, we must move beyond the 'request-response' model and embrace Conflict-free Replicated Data Types (CRDTs).

What are CRDTs and why should you care?

A CRDT is a data structure that can be replicated across multiple computers over a network, where each replica can be updated independently and concurrently without central coordination. The magic happens at the end: when replicas see each other again, a mathematical algorithm ensures they converge to the exact same state.

Unlike Operational Transformation (OT)—the tech behind Google Docs that requires a complex, expensive central server—CRDTs are decentralized by nature. This enables Local-First architectures, where the app works 100% without internet and syncs changes efficiently once the connection is restored.

Types of CRDTs: From Counters to Rich Text

Not all data behaves the same way. To implement a solid architecture, we must choose the right structure for the information we handle:

  • G-Counter (Grow-only Counter): A counter that only increases. Useful for views or likes metrics.
  • PN-Counter (Positive-Negative Counter): Allows both increments and decrements, ideal for shopping carts.
  • LWW-Element-Set (Last Write Wins): The simplest, but prone to data loss if not handled with precise timestamps.
  • Text/Sequence CRDTs: Complex structures for code or text editors where character order is critical.
"Eventual consistency is not a design flaw; it is a fundamental property of distributed systems that prioritize availability over total locking."

Technical Implementation: Stack Comparison

If you're building a modern app in 2024, you shouldn't write your own CRDT algorithms from scratch unless you're a PhD in mathematics. Here are the market leaders:

1. Yjs: The de facto standard for the JS ecosystem

Yjs is incredibly fast and memory-efficient. It integrates perfectly with ProseMirror, Quill, and Monaco Editor. Its network model is agnostic; you can use WebRTC for P2P or WebSockets for a client-server model.

import * as Y from 'yjs';
import { WebrtcProvider } from 'y-webrtc';

const ydoc = new Y.Doc();
const provider = new WebrtcProvider('room-name', ydoc);
const ytext = ydoc.getText('content');

ytext.observe(event => {
  console.log('Change detected:', ytext.toString());
});

2. Automerge: JSON-focused

Originally developed by Ink & Switch, Automerge treats your entire state as a JSON object. It’s easier to reason about for task management apps or CRMs, though historically it has been slightly slower than Yjs for massive documents.

Offline-First Architecture: The Workflow

To implement this in a real environment (e.g., a logistics app in rural areas with poor connectivity), the workflow should be as follows:

  1. Local Persistence: Changes are first saved to IndexedDB (browser) or SQLite (mobile).
  2. Delta Propagation: We don't send the whole document, just the binary 'diff' generated by the CRDT.
  3. Automatic Resolution: Upon receiving remote changes, the CRDT integrates nodes without user intervention.
  4. Compaction: Old metadata is periodically cleaned to prevent file bloat.

Security and Privacy Considerations

Moving merging logic to the client brings challenges. How do we validate that a user didn't inject malicious data into their local replica before syncing? The answer usually involves cryptographic signatures for every operation and 'relay' servers that validate schemas without necessarily needing to understand the content if End-to-End Encryption (E2EE) is used.

Recommended Production Tools

  • Hocuspocus: A ready-to-use backend for Yjs (Node.js based).
  • Replicache: An extremely polished commercial alternative for UI sync.
  • ElectricSQL: For those needing to sync Postgres directly with local devices using CRDTs under the hood.

How we approach it at Julsmind SAS

At Julsmind SAS, we understand that latency kills product adoption. We have implemented local-first architectures for clients operating in unstable network conditions across LATAM. We don't just write code; we design data topologies so the user experience feels instantaneous (0ms perceived latency), using CRDTs and optimizing binary transfers to reduce cloud infrastructure costs. Our experience in Medellín has taught us that software must be resilient by design, not by accident.

Are you tired of saving conflicts in your current platform or want to take real-time collaboration to the next level? Let's talk about how a modern data architecture can transform your product. Reach out via our contact page and let's analyze your technical case today.

Have a project in mind?

Get a free quote from our team — no strings attached.

Get a quote