Cloud & Data Centers

Collaborative Documents: How Real-Time Editing Works

How several people type in one document at once: local edits, operations, conflict resolution, cursors and offline merging.

Key takeaways

  • Your editor applies your typing locally first, so it feels instant even on a slow connection.
  • Apps send small operations such as "insert this text here" instead of resending the whole document.
  • Operational transformation and CRDTs are the two main families of techniques that make concurrent edits converge.
  • Converging on the same text does not guarantee the text means what either writer intended.
  • Version history, permissions and offline behavior differ between products and deserve a test before you rely on them.

Real-time editing works by giving everyone their own copy of the document, applying each person's keystrokes to that copy immediately, and then exchanging small descriptions of those changes. Software on each side adjusts incoming changes so that every copy ends up identical, even when two people typed at the same moment.

That last part is the clever bit. Sending text across a network is easy. Making sure that two edits made without knowledge of each other combine into one sensible result, the same result on every screen, is the problem this article explains.

Why saving the whole file doesn't work

Picture the older way of sharing a document. Two people open the same file, each makes changes, and each saves. Whoever saves second overwrites the first person's work, or the system creates a "conflicted copy" that someone has to reconcile by hand.

File locking avoids the overwrite by letting only one person edit at a time. It's safe and still useful for some formats, but everyone else waits. Cloud storage that syncs ordinary files faces exactly this limit: it sees a file changed, with no idea which paragraph.

Collaborative editors get around it by working below the level of files. The editor knows the document's internal structure, such as paragraphs, characters, cells or shapes, and records each change to that structure as it happens.

Local first, then the network

When you press a key, your editor doesn't ask a server for permission. It changes your copy right away and queues a message describing the change. This is often called optimistic editing, because the app assumes the change can be reconciled later.

The alternative would be to wait for a round trip before showing each letter. Even a modest delay makes typing feel broken, and latency has a physical floor that no connection upgrade removes.

The cost is that copies are briefly different. At any instant, your screen may include edits others haven't received, and theirs may include edits you haven't seen. The system's promise is eventual convergence: once everyone has received everything, all copies match.

Edits travel as operations

An operation is a compact instruction such as "insert cat at position 12" or "delete 4 characters starting at position 30". Operations are tiny compared with the document, so they can be sent many times a second.

The connection is usually a WebSocket or a similar long-lived channel that lets the server push changes to you without being asked. A central server typically receives operations, records them in order and forwards them to the other participants.

Two editors and a shared server
Ana's editor Server Ben's editor Insert 'big ' at position 4 1 Already visible on Ana's screen Delete word at position 8 2 Sent before Ben saw Ana's edit Ana's insert 3 Ben's delete, position adjusted to 12 4 Acknowledge: edit stored 5 Ana's editor Server Ben's editor Insert 'big ' at position 4 1 Already visible on Ana's screen Delete word at position 8 2 Sent before Ben saw Ana's edit Ana's insert 3 Ben's delete, position adjusted to 12 4 Acknowledg e: edit stored 5

The position problem

Here's where it gets hard. Suppose the document reads The cat sat. Ana inserts big before cat. At the same moment, Ben deletes the word sat, which in his copy starts at position 8, counting from zero.

If Ana's copy applies Ben's instruction literally, it deletes at position 8, but her insertion pushed sat four places to the right. She'd lose the wrong characters. Ben's copy, receiving Ana's insert, happens to be fine because it lands before his change. The two screens now disagree.

Every collaborative editor needs an answer to this. There are two well-known families.

Operational transformation

Operational transformation, usually shortened to OT, rewrites an incoming operation to account for operations that happened concurrently. In the example, the system notices that Ana inserted four characters ahead of Ben's target and shifts his delete from position 8 to position 12.

The rules must cover every pair of operation types: insert against insert, insert against delete, delete against delete, and so on, including awkward cases such as two inserts at the identical spot. A tie-breaking rule decides whose text goes first, and it has to be applied the same way everywhere.

Many OT systems rely on a central server to put operations into a single order, which simplifies the rules considerably. Google has described using this approach in its online editors. The weak point is complexity: rich formatting, tables and embedded objects multiply the number of cases to get right.

Conflict-free replicated data types

A CRDT, or conflict-free replicated data type, takes a different route. Instead of numbered positions that shift, each character or item gets a permanent unique identifier and a description of where it sits relative to its neighbors. Ben's instruction becomes "delete the items with these IDs", which stays correct no matter what was inserted elsewhere.

The data structure is designed so that applying the same set of changes in any order yields the same result. That makes it a good fit for offline work and for peer-to-peer syncing, where no server decides the order. Open-source libraries such as Yjs and Automerge implement CRDTs for application developers.

The price is bookkeeping. Identifiers and records of deleted items, often called tombstones, take memory and storage, and well-built libraries spend much effort compressing them. Many real products mix ideas from both families, with a server in the middle even when the data model is CRDT-like.

Two ways to make concurrent edits agree
Operational transformation Edits refer to positions Incoming edits are rewritten Usually relies on a server to order changes Compact data, intricate rules CRDT Items carry permanent IDs Changes merge in any order Can work offline or peer to peer Simpler merging, more bookkeeping VS Operational transformation Edits refer to positions Incoming edits are rewritten Usually relies on a server to order changes Compact data, intricate rules CRDT Items carry permanent IDs Changes merge in any order Can work offline or peer to peer Simpler merging, more bookkeeping

Cursors, presence and comments

The colored cursors and name badges are called presence. They're sent over the same channel but treated as temporary: nobody needs a permanent record of where your cursor was a minute ago, so these messages can be dropped or replaced freely.

Cursor positions need the same adjustment as edits. If someone inserts a paragraph above you, your cursor should move down with your sentence instead of staying at an old number. Comments are anchored to a range of text in a similar way, and they can end up orphaned if the text they point to is deleted.

Offline editing and reconnecting

If your connection drops, a capable editor keeps working and stores your operations in a local queue. When you reconnect, it sends them and receives everything you missed, and the merge rules combine the two histories.

Short gaps merge almost invisibly. Long ones are riskier. If you and a colleague each rewrote the same section during a day apart, the result may be mathematically consistent and practically garbled, with two versions of a sentence interleaved or an edit applied inside a paragraph that was meant to be removed. Read the merged sections before you send the document anywhere.

Offline support also varies. Some web editors require you to enable it in advance on each device, and some can only view files offline. Test it before a flight, not during one.

History, permissions and recovery

Because every change is recorded, collaborative editors can usually show earlier versions and who made which edit. Products often group fine-grained operations into readable snapshots, and some let you name an important version, such as the copy you sent to a client.

Permissions are enforced by the server. A viewer's editor may hide the typing cursor, but what matters is that the server refuses their operations. Common levels are view, comment and edit. Links that allow anyone to edit deserve caution, as covered in sharing files safely with links.

Version history lives inside the same account as the document, so it isn't an independent backup. If the account is lost or locked, the history goes with it. For documents that matter, export a copy now and then; the reasoning in why sync is not backup applies here as well.

Habits that prevent collisions

The algorithms keep copies consistent, but people still step on each other. A few habits help:

  • Divide the page. Agree who owns which section before a group writing session.
  • Use suggestions. In a review, tracked suggestions let the author accept or reject changes.
  • Name versions before large restructures, so there's an obvious point to return to.
  • Avoid pasting over whole sections that others may be editing.
  • Mind the spreadsheet. Sorting or deleting rows while others type can change what their formulas refer to. Filter views that affect only your screen, where offered, are safer.

Common questions

Why do I sometimes see text jump around?

Your copy applied your edit first and then received someone else's. When their change lands above your position, your view shifts to make room.

Does the provider see what I type before I save?

In most hosted editors there is no separate save: operations stream to the server as you type, including text you later delete, and history may retain it. End-to-end encrypted collaborative editors exist, where the server relays changes it can't read, usually with fewer server-side features.

Can two copies ever fail to converge?

Correctly implemented algorithms prevent it, but software has bugs. If a document looks different on two screens after both are online, reload each one, and copy anything unsaved somewhere safe first.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Cloud & Data Centers →

Latest news on this topic