← Back to Vector clocks and ordering events
💌 Vector clocks and ordering events: cheat sheet
🎨 The analogyPen pals who number their letters and note the latest number seen from every friend.
🎓 The regular explanationWall clocks can't order events across machines, so distributed systems track causality instead. Lamport clocks give an order consistent with happened-before; vector clocks keep one counter per node and can also tell when two events are concurrent, which is how Dynamo-style stores detect conflicting writes.
- Ana's kitchen clock runs five minutes fast. She writes "meet at the park" and dates it 10:05, then posts it to Ben and Cara.
- Ben reads Ana's letter and replies "no, the café". His clock is right, so he dates his reply 10:02.
- Cara puts the letters in order by their times. Ben's reply looks older, so she thinks the park is the latest plan and goes there. Wrong.
- Even good clocks drift apart, and resetting one can move it backwards. The time on a letter only tells you what one kitchen clock said.
- Ana writes "park". The biggest number she's seen is 0, so this letter is number 1.
- Ben has seen number 1, so his café reply is number 2. A reply always gets a bigger number than the letter it answers.
- Meanwhile Cara has written three letters to her cousin, numbered 1 to 3. She has never seen Ana's or Ben's letters.
- Cara writes "I'll bring cake" as number 4. Ana sees 2 then 4 and thinks Cara knew about the café. She didn't. One number can't show that.
- Everyone starts with a list of zeros. Ana bumps her own number and writes "park" with the list A1 B0 C0.
- Ben reads it. For each friend he keeps the bigger number, his or the letter's. Now his list says he has seen Ana's first letter.
- Ben bumps his own number and writes "café" with A1 B1 C0. Cara reads it and copies the bigger numbers into her list.
- Cara has heard from both, so her "see you at the café" letter says A1 B1 C1. Ana reads it and learns that Cara knew about both letters.
- Ana keeps her own park letter (A1 B0 C0). Ben's café reply arrives with A1 B1 C0.
- Earlier, before Ben's letter reached her, Cara wrote "I'll bring cake" with A0 B0 C1.
- Park against café, friend by friend. Every number on park is the same or smaller. So park came before café, and Ben knew about it.
- Café against cake. Café is bigger for Ana and Ben, cake is bigger for Cara. Neither knew about the other, so they were written side by side.
- Dana and Eli can't reach each other. Ana asks Dana to add cake to the plan. Dana bumps her number, so the plan now says D2 E1.
- At the same time, Ben asks Eli to add juice. Eli bumps his number, so his plan says D1 E2.
- The keepers swap news and compare lists. Each plan has a bigger number somewhere, so neither knew about the other. Both keep both versions.
- Cara reads the plan, gets both versions, and combines them. Her new plan, D3 E2, beats both old lists, so it replaces them.
- The plan holds two versions written side by side. Ana's added cake at 10:01. Ben's added juice at 10:02, by his kitchen clock.
- The quick way, last write wins. Juice has the later time, so it stays and the cake is thrown away. Nobody tells Ana.
- The careful way. Cara reads both versions and combines them, so the plan says cake and juice. Nothing anyone added is lost.
- Ana asks Dana to add cake. Dana writes it down with the list D1.
- Ben read the plan with cake, asks Dana to make it cake and juice, but forgets to send back the list from the plan he read. Dana can't tell what he had seen, so she keeps both versions side by side.
- Cara asks for the plan and gets both versions, together with the combined list.
- Cara combines them into one plan and sends it back with the list she was given. Dana's new list beats both old ones, so the two versions are replaced.
🧩 Key questions
- Ben replies to Ana's letter, but his reply has an earlier time written on it than hers. How can that happen?
- Their kitchen clocks don't agree, so the times can't show which letter came first. Ana's clock runs fast. Only what each friend had read tells you which letter came first.
- Ben's letter has the single number 2 and Cara's has 4. What can Ana be sure of?
- Only that Cara's letter wasn't read by Ben before he wrote his. She may never have seen his. A bigger number on its own can't show that two letters were written without knowing about each other. The list can.
- Ana's list says A3 B1 C0. A letter arrives with A1 B4 C2. What does her list say after reading it, before she writes anything?
- A3 B4 C2: for each friend, the bigger of the two numbers. Keeping the bigger number means she never forgets anything she knew. Before her next letter she bumps her own number to A4.
- One version of the plan says D2 E1 and another says D1 E2. What should a plan keeper do?
- Keep both, because neither was written knowing about the other, and let the next reader combine them. Each list has a bigger number somewhere, so they were written side by side. Throwing either away would lose a change.
- Two side-by-side plans are settled by last write wins. What's the risk?
- Someone's change is quietly thrown away, and wrong clocks may even pick the wrong one. Combining the plans, or using a list people only add to, keeps everyone's changes.