39. Design File Storage and Sync (Google Drive / Dropbox)
39.6 How does chunking work?
Step 5: Deep dives (10 minutes)
Splitting files into blocks lets you resume big transfers, upload only what changed, and store identical blocks once. Dropbox has used chunks of up to 4 MB, hashed with SHA-256.
Figure: fixed boundaries shift with every insertion; content-defined boundaries move with the content.
- Fixed-size chunks (every 4 MB): simple and fast. But inserting bytes near the start shifts every boundary, so every chunk's hash changes. In the chapter's code, adding 15 bytes to the start of a 200 KB file makes all 49 fixed chunks new.
- Content-defined chunking (CDC): slide a rolling hash over the bytes and cut wherever it matches a pattern, with a minimum and maximum size. Cut points depend on the content, so after an insertion they line up again. In the chapter's code, the same edit changes 1 of 35 chunks.
Our pick: fixed-size blocks are fine for most files (photos, videos and PDFs are rarely edited in the middle); content-defined chunking helps for large files edited in place, like documents and databases. Some systems use both: fixed blocks for storage and a smarter delta for transfer.
Memory code: "Cut by content, not by ruler, and an edit only changes its neighbours."