Skip to content
LLD Learn/Distributed & Large-Scale Modeling
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Distributed Consistency Basics for Schema Design

5 min read

You'll learn to

  • -Explain why timestamps cannot reliably order edits made on two disconnected machines
  • -Read and compare vector clocks by hand: newer, older, or concurrent
  • -Store a vector clock in a schema and explain what the database cannot decide on its own
  • -Judge when this extra metadata is justified and when a plain updated_at column is enough

Everything in this course so far has assumed one database, one source of truth, and a clear idea of what happened before what. Distributed systems break that. When several independent machines (nodes) can each accept writes, even while they cannot reach each other, the data needs extra information so the machines can work out what happened in what order once they reconnect. This chapter introduces that information at the depth an LLD round expects.

Why "What Happened First" Gets Complicated

Suppose Mission Control and a remote spacecraft both edit the same log entry while out of radio contact. When the link returns, whose edit came first? Comparing timestamps seems obvious, but the two machines' clocks are never perfectly in step. One may run a few seconds fast, so the edit that really came second can carry the earlier timestamp. What we actually need to know is not what time it was, but whether one edit could have been influenced by the other.

An Analogy: Two Editors on Two Planes

Two editors each take a copy of a recipe onto separate flights. Each keeps a tally for every editor: how many edits of theirs they have seen. When they land and compare tallies, three things can be true. One tally is at least as high everywhere and higher somewhere, so that editor saw everything the other saw. The tallies are identical, so nothing differs. Or each editor is ahead on a different line, so they worked in parallel and someone has to reconcile the two versions.

  • -The tally for each editor is a vector clock: one counter per node.
  • -Making an edit means adding 1 to your own counter only.
  • -Meeting and syncing means taking the highest value for each counter, because you now know everything the other knows.
  • -Each editor ahead on a different line is two concurrent updates: a genuine conflict.

Vector Clocks, Traced by Hand

Both sides start in sync with the clock {mission_control: 1, spacecraft: 0}. Out of contact, Mission Control edits, so its clock becomes {mission_control: 2, spacecraft: 0}. The spacecraft edits too, giving {mission_control: 1, spacecraft: 1}. Compare the two. Mission Control is ahead on its own counter (2 against 1), but behind on the spacecraft's (0 against 1). Neither clock is at least as high everywhere, so neither update could have seen the other. They are concurrent. Run the code to check the rules.

Comparing vector clocks: newer, older, or concurrent

Comparing the original synced clock with Mission Control's new one says the new one is newer, which is what we expect: it simply extends the old history. Comparing the two new clocks reports a real conflict. After someone resolves it, the merged clock takes the highest value of each counter, so it records that the result has seen both edits.

What the Schema Stores, and What It Cannot Decide

In a schema, the clock travels with the row as a small JSON column. The comparison rule from the code lives in the application.

Storing a vector clock alongside a record
CREATE TABLE mission_logs (
    id INTEGER PRIMARY KEY,
    log_entry TEXT NOT NULL,
    -- vector clock as JSON: {"mission_control": 3, "spacecraft_endurance": 1}
    vector_clock TEXT NOT NULL,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Comparing two versions' vector clocks: if every counter in version A is <=
-- the corresponding counter in version B, A happened-before B. If neither
-- dominates the other, the two versions are concurrent - a genuine conflict.

When two versions are concurrent, the database cannot choose between them. That needs an explicit conflict-resolution strategy: last write wins (simple, but one edit is silently lost), application-specific merge logic, or showing both versions to a human. Naming the strategy is a real design decision, and a good interview answer states it out loud.

When This Metadata Is Actually Justified

Vector clocks are real complexity, and the honest guidance echoes the locking lesson from Phase 1: justify them with a concrete requirement. That requirement is nodes that really can write independently while disconnected and must reconcile later. A typical client-server application has one authoritative database and no offline writes, so an ordinary created_at and updated_at pair is enough.

Reaching for vector clocks when the prompt does not describe independent, occasionally disconnected writers is the schema-design version of over-engineering with unnecessary design patterns. Name the requirement that justifies the complexity before you introduce it.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Ready to Build This?

Design Mission Control in the LLD Lab's Interstellar act.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!