Algorithms · Counting & data
Chunking and deduplication
How Dropbox uploads a 1 GB file in a second. Change one sentence in a huge file and only that little piece travels. Fingerprints make it possible.
The problem
Picture a huge file in your Dropbox folder: a video, a design file, a big archive. You fix one tiny thing in the middle and press save. Dropbox now has to get that change onto its servers and your other devices.
People do this all day, with files that are enormous and edits that are small. If every tiny edit costs a full upload, sync feels slow and wastes a lot of network.
The obvious way
The simplest plan: whenever the file changes, upload the whole file again. It's easy to write and it works. But a 1-byte edit to a huge file sends the entire file, and uploading a file someone already stored sends it all over again. Almost all of those bytes are ones the server already has.
The clever idea
Think of a book copied page by page. Instead of mailing the whole book after fixing one typo, you tell the library which pages you have, and it says "we have all of those except page 48." You mail only page 48.
Dropbox's file format (described in its 2014 post "Streaming File Synchronization") works this way. Every file is split into 4 MB blocks, and each block gets a SHA-256 fingerprint. A file is then identified by its list of fingerprints, a blocklist. The server skips any block it already has.
How the code does it
This line cuts the 400 KB file into 100 blocks of 4 KB, a scaled-down version of Dropbox's 4 MB blocks. The next line fingerprints each one. The server dict on line 11 is keyed by those fingerprints, so asking "do you have this block?" is a single lookup.
The first sync sends all 100 blocks. Then the edit touches one block, so the sync loop sends exactly 1. The whole-file way would send all 400 KB again, while the smart way sends 4 KB. Syncing the original again sends 0, because the server has every block. That's the "already stored" case: nothing needs to travel.
Trade-offs
You pay to hash every block and keep a lookup of what the server has, which is wasted effort for tiny files that fit in one block. In this toy, fixed-size slicing also means that inserting bytes near the start shifts every later block's contents and changes their hashes, so a plain edit-in-place is the happy case.
Where else you'll see it
Hash-the-content-and-skip-what-you-have shows up in backup tools, in Git (which names things by content hashes), and in container image layers. Wherever big data changes a little, this idea saves the work.
A 100-block file gets 10 bytes changed, all inside one block. How many blocks does the smart sync send?
- 100
- 1
- 10
Show the answer
1. Only the block containing the edit has a new hash. The other 99 hashes are already on the server, so they're skipped.
- Change BLOCK to 1024
- Edit bytes in two distant places
- Change the random seed to 8
The code
Python, with what each line means beside it. It compares the clever way with uploading the whole file every time it changes.
import hashlib, randomLoad tools for hashing and randomness BLOCK = 4096Blocks are 4 KB eachrandom.seed(7)Fix the random seed so runs matchoriginal = random.randbytes(400 * 1024)Make a 400 KB fake file def blocklist(data):Define a function that makes a blocklist chunks = [data[i:i + BLOCK] for i in range(0, len(data), BLOCK)]Slice the file into 4 KB chunks return [(hashlib.sha256(c).hexdigest(), c) for c in chunks]Pair each chunk with its SHA-256 fingerprint server = {}The server starts empty def sync(data):Define the smart sync sent = []Start a list of what we send for h, chunk in blocklist(data):Go through each block and its hash if h in server:If the server already has the hash sent.append(0)Skip it, mark 0 else:Otherwise server[h] = chunkStore the block on the server sent.append(1)Mark it as sent, 1 return sentHand back the sent list first = sync(original)Upload the original for the first timeedited = bytearray(original)Make an editable copy of the fileedited[200000:200010] = b"CHANGED!!!"Overwrite 10 bytes in the middleedited = bytes(edited)Freeze it back into bytessecond = sync(edited)Sync the edited filecopy = sync(original)Sync the original again, like a duplicate whole_kb = len(edited) // 1024The whole-file way sends every KBclever_kb = sum(second) * BLOCK // 1024The smart way sends only new blocksshow([whole_kb, clever_kb], "KB sent: whole file vs new blocks")Draw both as barsshow([first, second, copy], "rows: first upload, edit, same file again")Draw one row of squares per syncprint(sum(first), sum(second), sum(copy))Print how many blocks each sent- the fingerprint line 9Identical bytes always give the same hash, so the hash can stand in for the block when asking the server.
- the whole trick lines 16–20One dict lookup decides if a block travels. Known hash means skip; unknown means store and send.
- tiny edit line 25Ten bytes change, all inside one 4 KB block, so only that block's hash is new.
Read more
- dropbox.tech: Chunking and deduplication
- Ask Bento: What happens if bytes are inserted?
- Ask Bento: How does Git use content hashes?
- Ask Bento: What is a hash collision?
Run it. Then change it.
The code runs right in the page and draws as it goes. Free, nothing to install.