Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Distributed Systems

What are CRDTs? How shared documents stay in sync

Three people edit the same document offline, then reconnect. A CRDT makes every copy end up identical with no referee. We start with a counter, then text.

Derpx0612 Nov · 4 min read
Contents
Wait, what's the actual problem?So what is a CRDT?How do you count without a referee?How do two copies merge?What if messages repeat or get lost?Why is text so much harder?What's the trick for text?So does Google Docs use this?Sources
0% read · 4 min leftBack to top ↑
On this page0%
Contents
Wait, what's the actual problem?So what is a CRDT?How do you count without a referee?How do two copies merge?What if messages repeat or get lost?Why is text so much harder?What's the trick for text?So does Google Docs use this?Sources

Three people edit the same shared document. One has no signal, one has a connection that keeps dropping, and one is fine. Later everyone reconnects, and all three copies must show the exact same text.

Who decides whose edit wins? Some systems use a central server. But there is a trick where nobody needs to decide at all. It is called a CRDT, and we will build it up from the simplest example.

The idea

A CRDT is a shared thing built so that merging copies always works, like building blocks that can only click together one way.

Wait, what's the actual problem?

Say you keep a copy of the data on your phone, and a second person keeps one on theirs. Each copy is called a replica. You both edit while offline, and later the copies meet.

Now there are two different versions and no referee. That clash is the problem.

Normal copiesTwo offline copies disagree. Who wins?
→
CRDT copiesSame edits received, same result. No referee.

So what rule makes the "after" side possible?

So what is a CRDT?

CRDT stands for Conflict-free Replicated Data Type. The 2018 survey by Preguiça, Baquero and Shapiro gives two rules, paraphrased here.

CRDT, in plain words

Any copy can be changed without asking the other copies first. And once two copies have received the same set of updates, they end up in the same state, every time.

Notice what is missing: no waiting, no lock, no "ask the server". The rules are baked into the data structure itself. Let's see that with the simplest thing possible.

How do you count without a referee?

Three people count how many times a shop was visited. The obvious design is one shared number. But offline, two people both see 5, both add 1, and now they have to merge two 6s. Is it 6 or 7? (It is 7, but the copies cannot know that.)

The fix is the grow-only counter. Each replica owns one slot and only ever adds to their own slot.

ReplicaA's slotB's slotC's slot
A, after 2 visits200
B, after 3 visits030

Now the copies need to meet. How do they merge?

How do two copies merge?

Go slot by slot and keep the larger number. The total is the sum of all slots. Click Next and watch it happen.

Replica A
Replica B
Merged copy

A holds 2 0 0 after 2 visits

Step 1 of 4
Each replica only ever writes its own slot, so merging can never overwrite someone else's work.

Notice the merged copy has both replicas' counts. Why does this never break? Taking the max gives the same answer in any order, can be repeated safely, and can be grouped any way. So duplicate and late messages are harmless.

Let us test that with a bad network.

What if messages repeat or get lost?

Say B's phone sends its state to A, but the network drops some packets and keeps retrying. With a plain "add" you could count the same update three times, which gives a wrong total. With max, repeats change nothing.

Hit play to watch packets get lost on an unreliable link.

B's phone
×
Unreliable network
A's phone
Every third packet is lost (a made-up demo, not a measurement). With a max-merge, retries and late arrivals are harmless.

Did you see the lost one? Nothing breaks, because the next copy of B's state carries the same information.

What you get

Offline edits just work. Messages can arrive late, twice or out of order.

What it costs

A grow-only counter can't go down. Subtracting needs a slightly bigger design.

Counters are the easy case. Text is harder.

Why is text so much harder?

Numbers are easy because adding commutes (order does not matter). Text is trickier. If the doc reads "hi" and two people both insert at position 2, whose letter goes first? And if you also delete a letter, every position after it shifts.

So "insert at position 2" is a bad instruction. By the time it reaches someone else, position 2 may mean something different.

PositionsInsert at position 2 (shifts when others edit)
→
IDsInsert after the letter with ID A5 (never shifts)

What's the trick for text?

Give every character a name. Sequence CRDTs give each character its own unique ID, and the ID decides where it sits. Several designs exist, like Treedoc, Logoot and RGA, and the exact rule differs between them. The crdt.tech papers page lists them all.

The shared idea is that an edit means "put this letter between these two IDs", not "at spot 2". IDs don't move when others type, so the instruction stays true on every copy.

If two people insert in the same gap, the design has a fixed tie-break rule, so every copy picks the same order. That rule is the "referee", and it lives inside the data instead of on a server.

Edit offline
Reconnect
Swap updates
Merge rule
Same doc
↺ repeat
No coordinator anywhere in this circle.

So does Google Docs use this?

I do not know what runs inside it, and I will not guess. What I can say is that crdt.tech describes tools like Google Docs, Trello and Figma as routing communication through a server, while CRDTs do not assume a single server. So the pitch is editing offline and peer to peer.

Key takeaways
  • A CRDT is data built so copies can change on their own and still end up identical once they've seen the same updates.
  • The grow-only counter works because each replica owns one slot and merging takes the max per slot.
  • Text CRDTs give every character a unique ID, so edits say "between these two IDs" instead of "at position 2".
  • There's usually extra bookkeeping to pay for not needing a referee (my own reading, not a number from the sources).

Sources

  • Preguiça, Baquero, Shapiro, Conflict-free Replicated Data Types (CRDTs), 2018
  • Shapiro, Preguiça, Baquero, Zawirski, Conflict-free Replicated Data Types, SSS 2011, paper list on crdt.tech
  • crdt.tech, the CRDT resources site
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Distributed Systems · 6 minHow does your computer know the time? (And why it can be wrong)Distributed Systems · 4 minHow does TCP work on bad Wi-Fi?Distributed Systems · 3 minWhy mobile apps are hard: your phone is on bad internet
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)