Algorithms · Working together

Last-writer-wins sync

Figma multiplayer: last writer wins, per property. Two people drag the same box at once. Who wins, and why does it feel instant?

The problem

Picture a Figma file with two people in it. Ana drags a box to the right. At the same moment, Ben recolours that same box red. Both of them expect their change to stick, and both are watching the canvas live.

Figma's 2019 post on its multiplayer technology tackles this. Edits from many people arrive at nearly the same time, and everyone must end up looking at the same document without anyone's work vanishing for no reason.

The obvious way

The simplest fix is to lock things. Ana grabs the box, so Ben has to wait. It is safe, but it feels awful: Ben can't recolour a box just because someone nudged it. Lock the whole document and it's worse, since only one person can edit at a time. That defeats the point of working together.

The clever idea

Think of a shared form with separate fields: name, address, phone. If you fix the phone number and I fix the address, we don't fight. Only if we both rewrite the phone field does someone's edit lose, and then the later one wins.

Figma does this with every object. A central server is the authority and decides the order of changes. Conflicts are resolved per property: different properties both survive, and for the same property the last value the server receives wins. Each client applies its own edits immediately, so it feels instant.

Figma took inspiration from CRDTs but did not use a full one. Because a server already orders the changes, it could skip the extra machinery. Below, we run both rules on the same edits.

How the code does it

Both rules get the same edits, in the same time order. Rule A, whole-object, builds a fresh shape from the starting copy plus the edit. When Ben's recolour lands, it rebuilds the box from start, so Ana's move to x=100 is wiped out even though Ben never touched x.

Rule B, per-property, only overwrites the named keys. The box ends up at x=100 and red. The loss check shows what remains: Ana@3 still loses, because Ben also set the star's x and was later. Same property, so last writer wins.

Trade-offs

Per-property merging is simple and cheap, but it is blunt: a lost edit just disappears, with no merge of two intentions. It also leans on the server being the authority. Don't use it for text where two people type in the same sentence, since overwriting a whole field would destroy typing.

Where else you'll see it

Database fields in many apps behave this way: two people editing different columns of one row don't clobber each other. CRDTs take the idea further when there is no central server to order things.

Ana moves the box and Ben recolours it. With per-property last-writer-wins, what happens?

  • Ben's edit wins and the box goes back to its old position
  • Both edits survive
  • Whichever edit arrived first is thrown away
Show the answer

Both edits survive. They changed different properties (x and color), so neither overwrites the other. Only edits to the same property conflict.

Change it and see
  • Change Ben's star edit to color
  • Swap Ana@3 and Ben@4 times
  • Add a third edit on the box's x

The code

javascript, with what each line means beside it. It compares the clever way with locking the whole document.

const start = {Start with two shapes: a box and a star
box: { x: 0, color: "blue" },Box begins at x 0, blue
star: { x: 5, color: "gold" }Star begins at x 5, gold
};
const edits = [List the edits both people make
{ who: "Ana", time: 1, id: "box", props: { x: 100 } },Ana moves the box to 100
{ who: "Ben", time: 2, id: "box", props: { color: "red" } },Ben recolours the box red
{ who: "Ana", time: 3, id: "star", props: { x: 50 } },Ana moves the star to 50
{ who: "Ben", time: 4, id: "star", props: { x: 80 } }Ben moves the star to 80
];
const inOrder = [...edits].sort((a, b) => a.time - b.time);Sort edits by time, oldest first
function wholeObject(doc, edit) {Rule A: whole-object replace
doc[edit.id] = { ...start[edit.id], ...edit.props };Swap in a fresh copy of the shape plus the edit
}
function perProperty(doc, edit) {Rule B: per-property
Object.assign(doc[edit.id], edit.props);Overwrite only the properties the edit names
}
function merge(applyEdit) {Merge takes a rule for applying edits
const doc = JSON.parse(JSON.stringify(start));Copy the starting document
for (const edit of inOrder) applyEdit(doc, edit);Apply every edit in time order
return doc;Hand back the merged document
}
function lostEdits(doc) {Find edits whose changes didn't survive
return edits.filter(e =>Look at each edit's properties
Object.entries(e.props).some(([k, v]) => doc[e.id][k] !== v));Lost if any value differs in the final document
}
for (const [name, fn] of [["Whole object", wholeObject], ["Per property", perProperty]]) {Try each rule in turn
const doc = merge(fn);Merge using that rule
const lost = lostEdits(doc).map(e => e.who + " @" + e.time);Name the people whose edits were lost
print(name + " lost: " + (lost.join(", ") || "nothing"));Print who lost out
show(doc, name);Draw the final document as a table
}

Read more

Run it. Then change it.

The code runs right in the page and draws as it goes. Free, nothing to install.

More on working together