Saral Shiksha Yojna
Courses/Distributed Systems

Distributed Systems

CS3.401
Prof. Kishore KothapalliMonsoon 2025-264 credits

Logical Clocks (Scalar / Vector / Matrix) + Physical Time Sync

NotesStoryCaveman
Unit 2 — Models, Events & Logical Time

No big sun in sky, so no cave know "when"

Many caves. Each own tribe, own drip-clock — and every drip-clock drip different, quartz stone never tick same. When Ugg-fire and Bok-fire light, who first? No honest answer: no big sun everybody share. This the hard rock of distributed systems — a pack of computer-caves that talk only by throwing marked rocks, share no memory, hear no common drum. Wise-elder Lamport teach: stop chase real time. Chase who-cause-who.

The caves and the marked rocks

The DS model — caves joined by rock-paths called channels . Ugg warn: cave can die, path can eat a rock, path can deliver rocks out of order — first-thrown land second. Trust nothing.

State — one cave hold its own memory plus all it ever did. One path hold the rocks thrown but not yet caught.

Three thing a cave do. Local action — Ugg do thing alone, no tribe care. Send — throw marked rock. Receive — catch marked rock. Each doing change a cave's state.

Footprints in mud — happened-before

Happened-before — Lamport rule for "this cause that", three law:

Slow read: same cave, first step → next step; throw-rock before catch-rock, send → receive. Chain: and give — footprint lead to footprint. Two doings with NO path either way are concurrent: two hunts in far valleys, neither cause other.

Tally-stick clocks

A logical clock is a rule that scratch a number on each doing so footprint match number: . That is consistency. Only half the story. Strong consistency want both way: . The famous exam-trap sit right here.

One stick — Scalar (Lamport) clock

Scalar clock — each cave keep ONE number , start 0.

R1: before any doing, add one scratch ( usually 1). R2: catch a rock carrying number → take the BIGGER of own stick and rock number, THEN add one, then open rock. Want one full line-up of every doing? Tie-break — number first, then cave-name.

Ugg warn: no do this! Scalar is consistent but NOT strong. Bigger number no prove cause. Ugg alone scratch to ; Bok alone scratch to . , yet no rock ever crossed between — they concurrent. Scalar lose that truth.

Row of sticks — Vector clock

Vector clock — each cave keep a ROW , one stick per tribe, all 0.

R1: scratch OWN stick. R2 on catch: every stick take bigger of own and rock, THEN scratch own. Ugg warn: no do this! Many forget the "then scratch own" — max ALONE is wrong, must add one to own after.

Compare rows like this:

Every stick less-or-equal, and at least one truly less. If neither row beat the other → concurrent, . This clock is STRONG both way, no lie. Own stick = how many own doings came before.

Quick hunt-trace, three caves: Ugg send at . Bok catch then send → then . Zog catch → . And , so truly came before Zog-catch. Cost: a row of sticks ride every rock, — heavy in a huge tribe.

Singhal-Kshemkalyani trick — send only the stick that CHANGED since last throw to that cave. Keep (own count when last threw to ) and (own count when stick last changed); send only when . Ugg warn: this NEEDS a FIFO path — rocks must land in throw-order or the catch-cave rebuild the row wrong.

Grid of sticks — Matrix clock

Matrix clock — each cave keep an grid . Own row = own vector clock. Other spot = what Ugg know about what Bok () know about Zog () clock — second-hand knowing. Ugg warn: no flip it! It is I-know-what-J-knows, not backward.

Why the grid? Burn old rocks — garbage collection. When , EVERY tribe been seen to pass Ugg time , so all old rocks numbered already caught everywhere → safe to burn from the send-buffer. Good for copy-everywhere store-caves.

Line-up: scalar = 1 stick, forward-only, full order. Vector = sticks, strong, smell concurrency. Matrix = sticks, strong plus know-what-others-know, does the burning.

Real sun-time — sync the drip-clocks

Sometimes tribe DO need real time: mark meat-age, stamp a trade, order fire-logs. Drip-clocks drift, so:

Cristian's — ask one big time-cave. Ugg send at , cave say UTC , Ugg catch at ; run-there-and-back . Best guess — add half the run (so , gives RTT 10, set clock ). Ugg warn: one time-cave only — it die, all blind.

Berkeley — chief-cave ask ALL tribes their time, toss the wild liars, take the MIDDLE, tell each how much to bend. No sun-star needed — tribes agree only among themselves. Good in one valley (LAN).

Decentralised averaging — at agreed drum-beat all shout time, all take the middle. No chief at all.

NTP — the real mountain. Stratum 0 = star-clock, sun-stone (atomic, GPS) on top. Stratum 1 = caves touching it. Stratum 2+ = caves below, drink time from above. Trade four time-marks, work out offset plus delay. Close — few drips on far paths (ms on WAN), tiny near (sub-ms on LAN). Ugg warn: NTP NEVER perfect — path-gaps pile up, caves ask different servers.

Why keep sticks if sun-time exist?

Because sun-time is only CLOSE, never exact. Lock-sharing, snapshots, deadlock-smell need cause RIGHT, not number near. A hair of skew NTP never see will still break them. Tally-stick clocks catch cause exact, whatever the drift.

Ugg remember

  • No shared sun: chase happened-before (same-cave order, send→receive, chain). No path between two doings = concurrent.
  • Scalar = 1 stick, forward-only (big number no prove cause — the trap). Vector = sticks, strong both-way. Matrix = sticks, strong plus know-what-others-know → burn old rocks once .
  • Vector R2 = componentwise max THEN scratch own — forget the scratch and it break. Singhal-Kshemkalyani cuts rock-weight but NEEDS FIFO.
  • Sun-time: Cristian's = one master + UTC + ; Berkeley = poll + average, no UTC, LAN; NTP = strata 0→1→2, close but never perfect.
  • Even with NTP, keep logical clocks — safety needs causality exact, not "close enough."
End of storyUnit 2 — Models, Events & Logical Time · Logical Clocks (Scalar / Vector / Matrix) + Physical Time Sync