Video in progress

Strong Middle 🏗️ System design

In a file sync service like Dropbox, what do splitting files into chunks and deduplication give you?

A file is split into chunks of about 4 MB and each chunk is stored under its hash. When a file changes, only the changed chunks are uploaded, an interrupted upload resumes from where it stopped, and identical chunks are stored once. The file's metadata is the list of chunk hashes, in a separate database.

📖 Answer

You changed one paragraph in a 1 GB document. Re-uploading the whole file is 1 GB of traffic. With chunks, it's one 4 MB chunk.

The structure:

files(file_id, owner_id, path, version, updated_at)
file_versions(file_id, version, chunk_hashes[])     -- the order of chunks
chunks(hash PRIMARY KEY, size, storage_key, ref_count)

The chunks themselves are in object storage, keyed by the content hash (for example, SHA-256).

The upload flow.

  1. The client splits the file into chunks and computes their hashes.
  2. It sends the hash list to the server: "which of these don't you have?".
  3. It uploads only the missing chunks (straight to storage with a pre-signed URL).
  4. When everything is uploaded, the new version's metadata is committed.

This solves three things at once: delta sync, resuming an interrupted upload, and deduplication (if 100 people upload the same movie, the chunks are stored once).

Subtle points:

  • With fixed 4 MB boundaries, if you add 1 byte at the start of the file, every chunk shifts and they all become "new". Content-defined chunking (choosing boundaries based on the content with a rolling hash) solves this.
  • Dedup across users is a privacy risk: from the answer to "does the server have this hash?" you can learn that someone else has a particular file. Often dedup is done within a single user or organization.
  • A chunk can be deleted only when no version points to it — ref count or background "garbage collection".
  • A change on two devices at the same time is a version conflict. Usually a "conflicted copy" file is created, and merging is left to the user.

To announce syncing, clients get a "there's a change in this folder" message through long polling or WebSocket, and then ask for the metadata.