Skip to content
Tech Intuition
Tech Intuition

Appearance

Theme

Signal · Dark with amber

Background

Slow stars and the odd comet

Connect

Setting the stage…

Story · System design · 14 minutes

Design a file sync service: one letter, one gigabyte

You change one letter in a one-gigabyte file. What should travel to your other devices? This story cuts a file into blocks, breaks the first cut on purpose, and builds the system that keeps every device in step. Scroll to begin.

Chapter 01

What a file sync client sees: a file is a row of bytes

A file, to a sync client, is a long row of bytes. This one is eighty letters, so you can read it; a real one is millions of bytes with no letters in them. A copy lives on a laptop, a copy on a phone and a copy on a server, and every change to one has to reach the others.

Meet the cast: a laptop, a phone, and a server that keeps the file's pieces and a journal of changes. The strip across the top lists what must always hold. By the end of the story you will have seen every way it turns red, and the one rule behind each fix.

Chapter 02

Splitting a file into blocks so that only what changed travels

The obvious design: when the file changes, send the file. Change one letter and all eighty go again, to every device, on every save. For a one-gigabyte file, one corrected letter is a gigabyte on the wire. The cost of a change should follow the size of the change.

So cut the file into blocks and name each block by a hash of its letters. The client can now say which names it already has, and only a block with a new name has to travel. Change one letter in the middle and one block of twenty goes, not four.

Now the edit that breaks it. You add one letter at the very start of the file, the way you would fix a missing capital or add a heading. The blocks are still twenty letters long.

Predict

How many blocks must be sent?

The picture waits for you. Scroll on to skip.

Chapter 03

Content-defined chunking: cutting where the content says

So stop cutting by position. Slide a small window along the file and keep a hash of the last few letters. Wherever that hash lands on a rare value, here when its last three bits are all zero, make a cut. A cut now depends only on the letters just before it, never on where they are in the file.

Add the letter at the start again. The window sees the same letters in the same company, so it cuts in the same places. Only the first chunk is new: thirteen letters sent instead of the whole file. The cuts follow the content, so an insert cannot shift them.

Try it yourself. Choose how the file is cut, what kind of edit to make, and drag it anywhere along the file. Most edits change one chunk. An edit just before a cut can change two, because the window reaches a few letters past it. Fixed blocks only win for edits that move nothing.

Real chunkers work in kilobytes and megabytes, and the size is a trade. Small chunks send less per edit and cost more records, more hashes and more requests; large chunks do the opposite. Dropbox documents four-megabyte blocks, and tools that cut by content, such as restic, tend to sit around a megabyte.

Chapter 04

A content-addressed block store: one copy of every block

Now the system around the cut. The laptop hashes its six chunks and asks the server which of the names it lacks. The server holds five from the last version and asks for one. The question costs a few hundred bytes, whatever the size of the file.

Only that chunk travels. When the server confirms it holds every block, the laptop sends the commit: a tiny message that says this file is now these six names, written on revision 7. The server writes revision 8 and entry 13 of its journal. Blocks first, commit second: that order is the next chapter.

Because a block's name is its content, the server keeps one copy however many files contain it. A copy of a file costs a commit and no data at all. Here the phone adds backup.docx, identical to the thesis: the offer lists six names, the server lacks none, and the laptop learns of the file without a block travelling.

Keep every version, as a sync service does for undo, and cutting by content pays twice. Upload a 1 MiB file, then save it twenty times, each save inserting 100 bytes: those 21 versions took 10.6 MB with fixed blocks and 1.2 MB with content-defined chunks. History costs only the chunks that changed.

Chapter 05

Blocks first, then the commit: crashes, retries and garbage collection

Crashes are where the order of writes matters. The laptop has uploaded its new chunk, and the server has stored it, but the laptop dies before it commits. The phone is waiting for news.

Predict

What does the phone see?

The picture waits for you. Scroll on to skip.

When the laptop wakes it asks again. The server already holds every block, including the orphan, so there is nothing to send: one commit, and the phone catches up. The crash cost a little wasted space and no bytes on the retry.

Reverse the order and the same crash is a disaster. The laptop commits first, so the journal announces the file, the phone asks for its blocks, and one is not there. The file can be listed and cannot be opened, and the strip goes red.

One more way to point at nothing. An old upload left an orphan block on the shelf. Today the laptop saves the same edit, the server says it already has every block, and the laptop skips the upload. Meanwhile the sweeper, which frees blocks no file names, makes its rounds.

Predict

The sweeper runs after the server said it has every block and before the commit arrives. What breaks?

The picture waits for you. Scroll on to skip.

The cure is two small rules. Saying a block exists renews its lease, so the sweeper leaves it alone. And a commit checks that every block it names is still there, in the same step that counts the reference. Deleting is the one operation here that deserves a delay, a log and an undo.

Chapter 06

The change journal: cursors, notifications and what a device missed

How does the phone find out? The server numbers every commit in an append-only journal: entry 13 is this file's new revision. It sends the phone a hint with no data in it, only that something changed. The phone keeps a cursor, the number of the last entry it has applied, and it is 12.

The phone asks for everything after 12. It gets one entry with a block list, notices it lacks one block, fetches it and writes the file. The cursor moves to 13. Catching up is replaying a log from where you stopped, which makes it resumable and safe to repeat.

Hints get lost. This one never arrives, so for a few seconds the phone is out of date and does not know it. It checks on its own schedule anyway, asks for everything after its cursor, and catches up. The hint is a hurry-up. The journal is the truth.

A subtler trap. The laptop is uploading a big movie and the phone a small note. The server numbered the movie's entry 13 when it started and the note's 14. The note finishes first. The phone checks in between, sees entry 14, and moves its cursor to 14.

Predict

Entry 13 commits a moment later. Will the phone ever get it?

The picture waits for you. Scroll on to skip.

Number entries when they become visible, inside the commit, under the folder's lock. Then commit order and number order are the same thing, and a cursor can never jump over a change. Same two uploads, same check-in, and this time the movie reaches the phone.

Chapter 07

Two devices, one file: how a sync service handles conflicts

Now the case that defines the product. The laptop and the phone are both offline, and both edit the same file. The laptop changes the first chunk. The phone changes one in the middle. Neither knows about the other.

Both reconnect, and the server does what many demos do: the last commit wins. Everything converges, which feels like success. But the laptop's edit is gone and nobody was told. The strip shows the second promise broken: an edit was lost without a trace.

Same two edits, a better rule. Every commit names the revision it was written from. The laptop's commit is on revision 7 and lands first. Then the phone's commit arrives, also written on revision 7, but the file is now on revision 8.

Predict

What should happen to the phone's edit?

The picture waits for you. Scroll on to skip.

How does a device decide, path by path? It keeps three views: what it holds now, what both sides last agreed on, and what the server holds. If only one side changed, that side wins. If both made the same change, nothing to do. If both changed it differently, keep both. And an edit beats a delete.

Does the rule hold up? The control below runs 5,000 random sessions: a laptop and a phone each make one to three offline edits among six files, then sync in a random order. Count the sessions in which an edit ended up nowhere.

Chapter 08

Scaling a file sync service: folders, hot shares and block storage

Zoom out. Order matters only inside one folder, so a folder is a namespace: its own journal, on its own metadata shard. A shared folder is a namespace of its own that appears in every member's tree. A million folders are a million independent journals, and no commit waits for another folder.

The limit is the big shared folder. One commit in a folder with fifty thousand members is fifty thousand hints and, soon after, fifty thousand pulls for the same few entries. Coalesce the hints, spread the pulls with jitter, and serve the journal's tail from a cache: it is append-only, and everyone asks for nearly the same range.

Underneath, blocks are immutable and named by their hash, so the store checks itself. A scrubber reads blocks all day and re-hashes them. A mismatch means a bad copy, and repair rebuilds it from the other fragments. Erasure coding keeps durability high at a fraction of the cost of three full copies.

Chapter 09

The file sync design in one minute

That is the whole design. Before you scroll on, say it back in your own words: send the difference, not the file; cut by content so an insert cannot move the cuts; store each block once; write blocks before the commit; number the journal when entries become visible; keep both versions of a conflict.

The whole story

Say it back

  1. 01A sync client sees a file as bytes, and its job is to make every copy identical while sending as little as possible and losing nothing.
  2. 02Cut a file into blocks named by a hash and only blocks with new names travel, but cut by position and one inserted byte renames every block after it.
  3. 03Cut where the content says, with a rolling hash, and the cuts survive an insert. The price is a pass over every byte and smaller, more numerous pieces.
  4. 04Store each block once, so a copy or an old version costs a commit and almost no data.
  5. 05Blocks first, then the commit, and sweep only behind a lease and a verified commit, so nothing ever points at data that is not there.
  6. 06Number the journal when entries become visible, let devices pull from a cursor, and treat notifications as hints.
  7. 07Commit against a base revision and keep both versions of a conflict, because a policy that overwrites loses edits without a trace.
  8. 08Shard metadata by folder, cache the journal's tail, coalesce hints, and let the block store verify itself.

Now say it out loud

Why does inserting one byte at the start of a file re-send every fixed block?

A block is defined by where it starts, and an insert moves where everything starts. Every later block holds different bytes and gets a different name. Content-defined chunking cuts where a hash of the last few bytes says to, so the cuts stay with the content.

A client crashes between uploading its blocks and committing. What does the phone see?

The old version, complete. The journal changes only at the commit, and the uploaded blocks are orphans that nothing refers to. The retry finds them already stored and commits without sending data. The reverse order would announce a file whose blocks are missing.

How can a device's cursor skip a change?

If entries are numbered when work starts and become visible later, a slow commit can appear after a faster, higher-numbered one. A device that pulls in between moves its cursor past it. Number entries inside the commit, under the folder's lock, so visibility order is number order.

Two devices edit one file offline. Why not let the last one to sync win?

It loses an edit without a trace. Commit against a base revision, refuse a stale commit, and keep the loser's version as a conflicted copy, so nothing is lost and a person can decide.

Keep going

Bytes. A sentence of eighty letters laid out in four rows of twenty: every save writes a few bytes and the client must work out what the server lacks.

Send a message

It goes straight to my inbox. I usually reply within a few days.