GOAT EngineerPreparing your learning space…

System Design · Interview breakdown · Free article

Design Cloud File Storage (Dropbox, Drive)

Upload a 1 GB file over bad Wi-Fi, send only what changed, and keep a laptop, a tablet and a phone in agreement. Built one step at a time, with experiments you can run.

A file storage and sync service keeps a folder of files identical on every device a person owns, and lets them share it. The app splits each file into 4 MB chunks named by their SHA-256 hash and uploads only the chunks the server lacks, straight to object storage. A version is published by one database transaction, and only if the file is still on the version the device edited. Each folder has a change log numbered in commit order. Devices keep a cursor into it and hold an open connection that only says “something changed”.

Difficulty: Medium. Reported in interviews at Meta, Clio, Coinbase, Autodesk, Apple. Also asked as Design Dropbox, Design Google Drive, Design OneDrive, Design a file sync service, Design cloud storage.

Practise one part of it on a canvas that tests your design: Accept Large Uploads Without Melting Your Servers.

How do you design a file storage service like Dropbox?

A file storage and sync service keeps a folder of files the same on every device a person owns, and lets them share it with other people. You save a file on your laptop. A few seconds later it is on your phone. Dropbox, Google Drive and OneDrive are the famous ones. The hard part is keeping several copies of one folder in agreement when devices go offline, edit the same file, and come back days later.

  1. Save: Uploading 60% · 1 GB. The hotel Wi-Fi drops at 602 MB. Nothing already sent should be sent again.
  2. Another device: Phone: file updated. The phone learns within seconds. A laptop that was asleep for a week still catches up.
  3. Offline edit: Tablet · 2 versions. The tablet and the laptop both changed the deck. Neither edit may vanish.
  4. Share: Shared with 12 people. A removed teammate must lose access to the next download.
  5. Restore: Trash · 30 days. A deleted file stays deleted, even on the laptop that was off.
Sana sees one folder. Behind it, every device holds its own copy. The service keeps the copies in agreement.

Picture Sana, a journalist, in a hotel room. She drags a 1 GB interview video into her synced folder. The progress bar reaches 60% and the Wi-Fi drops. When it comes back, the upload carries on from 600 MB, not from zero.

Next morning she opens her pitch deck on the tablet, on a train with no signal, and rewrites a slide. Her laptop at the office changed the same deck an hour earlier. When the tablet reconnects, both edits are still there.

Interviewers like this problem because the first drawing is easy: an app, an API, a database for file names and a bucket for bytes. The rest of the round is about what happens between the moment a file changes on one device and the moment every other device agrees.

Here is the design in brief. The app cuts each file into 4 MB chunks, names each chunk by its SHA-256 hash, and asks the metadata service which chunks it lacks. It uploads only those chunks, straight to object storage, through short-lived signed URLs. When every chunk is stored, one database transaction publishes the new version. It succeeds only if the file is still on the version the device edited. The same transaction appends an entry to the folder’s change log. Every other device holds an open connection that says “this folder changed”, then reads the log from its own saved cursor and downloads only the chunks it lacks.

The rest of this page is about the moments that design has to survive.

Here is the first one. The upload is one HTTP request. The Wi-Fi drops after 602 MB. The request fails. The server has nothing it can use. Sana’s laptop sends all 1,024 MB again, on the same bad Wi-Fi.

Here is the second. The laptop saves version 8 of the deck at 09:20. The tablet, offline since 09:05, edits its version 7 and uploads at 10:10. Suppose the server keeps whichever upload arrived last. Then the laptop’s chart disappears from the current file, and nobody is told.

What is the interviewer testing?

Most candidates draw a client, an API, a metadata database and object storage in the first ten minutes, and say that big files are split into chunks. That drawing is right. A mid-level answer often stops there.

The interviewer then pushes on the moments the drawing has to survive. An upload dies at 60%. One slide changes in a large file. A phone has to hear about a save. Two devices edit one file offline. A laptop comes back after 45 days. A senior answer handles the first four. A staff answer also handles the returning laptop, chunk cleanup and a shared folder with 20,000 members.

Spend most of the deep-dive time on the upload, the sync cursor and the conflict. How to run a round has the time plan for a 45 to 60 minute round and a 15-minute screen.

What should you ask before drawing anything?

Four questions change the design. Ask them in the first two minutes. If the interviewer leaves one open, say your assumption out loud.

How big?
A service for one office can keep every file in one database. A service for 500 million accounts cannot. Assume 500 million accounts, 100 million people active on a given day, each saving about two files a day. Files average a few megabytes. The largest are 50 GB.
Which devices, and for how long offline?
Picture a laptop left in a drawer for six weeks, then switched on. Assume desktop apps that keep the whole folder, phones that download a file when it is opened, and a web page. A device may be offline for any length of time. The change history is kept for 30 days.
Do two people type into the same document at once?
That is a live co-editing problem, like Google Docs. It needs the document’s structure. Assume files are opaque bytes. Two edits to one file made apart are kept as two files, not merged.
How long can a deleted file be restored?
A person who deletes a folder by mistake wants it back next week. Assume deleted files and old versions can be restored for 30 days. After that their bytes may be reclaimed.

Functional requirements

On the board: Functional requirements
1. Upload any file, up to 50 GB; resumable
2. Download a file, or any older version
3. Sync changes to every device, even offline
4. Share a file or folder; view or edit; revoke
5. Versions and trash, restorable for 30 days
out: co-editing, search, previews

Say it, don’t write it: The rule that never bends is that a saved edit is never silently lost.

Two people use the service: an owner who saves files on their devices, and a person they share with.

  1. A person uploads a file of up to 50 GB from any device. An interrupted upload resumes.
  2. A person downloads a file, or any older version of it.
  3. A change made on one device appears on the person’s other devices, including devices that were offline.
  4. A person shares a file or folder with someone else, as viewer or editor, and can revoke it.
  5. Old versions and deleted files can be restored for 30 days.

Out of scope

  • Two people typing into the same document at once; the collaborative editor breakdown covers it
  • Searching inside files
  • Previews and thumbnails, beyond a mention
  • How the object store keeps bytes safe inside itself; we use one
  • Plans, billing and quota pricing

Two words need pinning down now. A file is a thing with a name in a folder. It keeps its identity when it is renamed or moved. A version is one complete set of bytes for that file. Saving a file creates a new version; renaming it does not. Keeping the two apart is most of this design.

Non-functional requirements

On the board: Non-functional requirements
• Saved = durable; never a half-written file
• Online devices see a change in < 5 s (p99)
• An offline edit is never silently lost
• A dropped upload resends at most 1 chunk
• A deleted file stays deleted everywhere
• 7k saves/s; one busy folder stalls no other

Say it, don’t write it: The bytes can take minutes to arrive. What a device believes about the folder must never be wrong.

A 1 GB upload over hotel Wi-Fi takes minutes, and nobody minds. A phone that shows an old version of the deck as current is different. It is wrong, and Sana will act on it. So the service holds two kinds of data that deserve different treatment. The bytes are big and slow, and never change once written. The folder’s metadata is small, changes all the time, and must be exactly right.

  • A save the app reports as done survives the loss of a data centre.
  • A reader never sees a version with a missing chunk.
  • An online device sees a change within 5 seconds, at the 99th percentile.
  • An offline edit is never silently lost.
  • A dropped upload resends at most one chunk.
  • A file deleted on one device stays deleted on every device.
  • About 7,000 saves a second at peak, and one busy folder never slows another.

The 7,000 is the one number worth working out at this point, because it decides how the metadata is stored. 100 million people save about two files a day, which is 200 million saves. Spread over the 86,400 seconds in a day, that is about 2,300 saves a second. Peaks run about three times the average, so plan for 7,000. Each save is a small database transaction, and 7,000 a second is more than one database should carry. So the metadata is split across many databases, by folder. The other numbers on this page appear in the deep dive that needs them. They are illustrative interview targets, not measurements of any real service.

Suppose the metadata database cannot confirm a commit. Then the app keeps the file marked as “waiting to sync” and tries again, rather than showing a green tick for a version nobody recorded.

Core entities

On the board: Core entities
File(id, namespace, parent, name, head)
Version(file, n, base, chunk list, size)
Chunk(hash, size, state, refs, pins)
Namespace(id, seq) · Change(ns, seq, file)
Device(id, one cursor per namespace)

Say it, don’t write it: A file keeps its ID when it moves. A version never changes once written.

Follow one save through the system. Sana’s deck is file F9 in her namespace, which is her folder tree. F9’s head is version 7, a list of chunk hashes. The laptop saves: it uploads two new chunks and commits version 8, based on version 7. The commit moves F9’s head to 8 and appends change 42 to the namespace’s log. The phone’s cursor for that namespace was 41, so it reads change 42 and downloads the two new chunks.

Every record except the device’s cursor lives in the metadata database. The bytes of each chunk live in object storage, as an object named by the chunk’s hash.

Look at the file and the version. When Sana renames the deck, the file’s name changes and its ID stays F9. Her phone renames its local copy instead of downloading 200 MB again. A new file created later at the old name gets a new ID, so it cannot be mistaken for the old one. Dropbox’s API works the same way: an ID survives a move, and a new file at the same path gets a new ID (file access guide).

The version is the other half. A version is written once and never changed. The file’s head points at one of them. Publishing a save means moving that pointer, and nothing else. That is why a reader can never see half a save.

API

On the board: API
POST /uploads {file, base, hashes} → missing
PUT  <signed URL> one chunk → object store
POST /uploads/:id/commit → version | 409
GET  /files/:id?version=n → chunks + URLs
GET  /changes?ns&cursor → entries, cursor
GET  /notify (long poll) → “ns 7 changed”

Say it, don’t write it: The base version on every commit is what lets the server spot a conflict.

The app talks to the metadata service over ordinary HTTPS. File bytes never pass through it. They go to object storage through signed URLs the metadata service hands out.

RequestResponseRule it enforces
POST /uploads201 with an upload ID and a signed URL for each missing chunkThe caller may write to this folder; the same request ID returns the same upload
PUT signed URL200, or 412 if that chunk already existsThe URL names one object, lasts 15 minutes, and carries the chunk’s SHA-256
POST /uploads/{id}/commit200 with the new version, 409 if the base is stale, or 400 naming missing chunksEvery chunk is stored, and the file is still on the base version
GET /files/{id}?version=nThe chunk list with a signed download URL for eachThe caller may read this file now
GET /namespaces/{id}/changesChanges after the cursor, and the new cursor; or 410 resetReturned in commit order, at most 500 at a time
GET /notifyThe namespaces that changed, or nothing after about 60 sOnly says “look”; never carries the change itself
POST /files/{id}/move, DELETE /files/{id}200 with a new change number, or 409Carries the version the app last saw
Concrete exchangeMeaning
POST /uploads with {"fileId":"F9","baseVersion":7,"chunks":["9f2c…","a71e…","03bd…"],"requestId":"u-81"}201 {"uploadId":"U7","missing":[{"hash":"a71e…","url":"https://…"}]}: the server already has two of the three
POST /uploads/U7/commit200 {"fileId":"F9","version":8,"seq":42}
GET /namespaces/7/changes?cursor=41200 {"entries":[{"seq":42,"fileId":"F9","kind":"edit","version":8}],"cursor":42}

Three fields exist for a reason. The request ID makes a start that timed out safe to send again: the laptop retries and gets upload U7 back, not a second upload (idempotency). The base version on the commit is how the server notices that the file moved on while this device was editing. The cursor is a number the device keeps. A device that was off for an hour asks for everything after the last change it applied.

The signed URL does two jobs besides saving our servers the bytes. It names one object and allows one method for 15 minutes, so it cannot be used to overwrite anything else. And it carries the chunk’s SHA-256. Object storage checks the bytes it receives against that hash. A chunk that does not match its name is refused. S3 supports both. It verifies a SHA-256 checksum sent with an upload (object integrity). An If-None-Match header makes a write fail with 412 when the key already exists (conditional writes).

Step 1: upload a file

Sana saves the deck on her laptop. The app reads the file, cuts it into 4 MB chunks and computes each chunk’s SHA-256. It calls POST /uploads with the file, the version it edited and the list of hashes.

The metadata service checks that Sana may write to this folder. It looks up which of those chunks the service already holds for her account, and answers with a signed URL for each missing one. The app uploads those chunks in parallel, straight to object storage.

Then the app calls commit. The metadata service checks that every chunk is stored. In one transaction it writes version 8, points the file’s head at it, and appends a change to the folder’s log. Only then does the app show a green tick.

The interactive version of this example is available when JavaScript loads.

Notice the order. Chunks are stored first. The version is published second. If the commit fails, some chunks sit in object storage that nothing points at, and cleanup removes them later. The other order would publish a version whose bytes might never arrive, which is a file that cannot be opened.

Step 2: download on another device

Sana opens the deck on her phone. The phone calls GET /files/F9, which returns version 8’s chunk list with a signed download URL for each chunk. The phone fetches the chunks it lacks, several at a time, checks each one’s SHA-256, and writes them into a temporary file.

When every chunk is in, the phone renames the temporary file over the old one. A rename is atomic only inside one file system, so the temporary file lives in the same folder. A reader of the phone’s copy sees the old version or the new one, never a mix.

The interactive version of this example is available when JavaScript loads.

A dropped download resumes the same way an upload does. The phone keeps the chunks it has verified, and asks again only for the rest. Inside one chunk, an HTTP Range request picks up from the last byte received.

Step 3: every device finds out

So far the phone found the new version because Sana opened the file. Sync means it finds out on its own. Every commit appends an entry to its namespace’s change log, numbered in commit order: change 41, change 42. Each device keeps, for each namespace it follows, the number of the last change it applied. That number is its cursor.

The phone keeps one connection open to the notification service: “tell me when namespace 7 passes 41.” The request waits up to about 60 seconds. When the laptop’s commit lands, its new change entry is passed on to the notification service. The waiting request returns at once. Feeding the notification service from the entries the commit wrote means a commit that is recorded is a commit that gets announced. The phone then calls GET /changes with cursor 41, applies change 42, and saves 42 as its cursor.

The interactive version of this example is available when JavaScript loads.

The notification carries no details. That is deliberate. If it is lost, delayed or sent twice, nothing is wrong: the device reads the log from its cursor and gets exactly what it missed. The sync deep dive below shows why the numbering has to follow commit order.

Step 4: share a folder, keep history

Sana shares the “Pitch” folder with her editor, Omar, as an editor. The shared folder becomes its own namespace, with its own change log, mounted into both people’s folder trees. Omar’s devices start following namespace 12 from cursor 0 and download the folder.

Every metadata call checks that the caller is a member of the namespace with the right role. A signed URL is only handed out after that check. When Sana removes Omar, the next call from his devices is refused. No new URLs are signed for him.

History needs no new box either. An older version is an older chunk list. A deleted file is a file row with a deletion time, plus a change entry saying so. Both are kept for 30 days, and so are the chunks they point at.

The interactive version of this example is available when JavaScript loads.

On a quiet day this design already works. The deep dives below take the moments it has to survive, one at a time.

The hotel Wi-Fi drops after 602 MB of a 1 GB upload. What has to be sent again?

Sana’s interview video is 1,024 MB. She drags it into her synced folder from a hotel room. The Wi-Fi drops after 602 MB, and comes back a few seconds later.

Start with the simplest thing that could work. The app sends the whole file to our API in one POST request, and the API writes it to object storage. Now watch the drop. The request fails at 602 MB. The API was holding part of a request that never finished, so it throws those bytes away. When the Wi-Fi returns, the app starts again from the first byte.

Say the Wi-Fi holds the second time. Sana’s laptop has sent 602 MB, then 1,024 MB. That is 1,626 MB for a 1,024 MB file. On Wi-Fi that drops every few minutes, a large file may never finish at all.

There is a second problem, and this one is ours. 100 million people save about two files a day, which is 200 million saves. Most saves change only part of a file, so count about 1 MB of new bytes each. 200 million × 1 MB is 200 TB a day. Divide by 86,400 seconds, and about 2.3 GB arrive every second. In this design all of it passes through our API servers. Each new file is also downloaded by about 2 of the owner’s other devices, so about 400 TB a day goes back out. Servers that carry that much need the network and memory of a storage system, only to pass bytes along.

So take our servers out of the byte path first. The app asks the API for permission to upload. The API checks that Sana may write to this folder. Then it answers with a signed URL: a link that lets the app upload to one object in storage, and expires in 15 minutes. The app sends the bytes straight to object storage. Our servers now handle a request of a few hundred bytes instead of 1 GB.

The drop still costs the same. The upload is still one request. At 602 MB it fails, storage keeps nothing, and the retry sends all 1,024 MB again.

Now cut the file into pieces. Split the video into 4 MB chunks. 1,024 ÷ 4 is 256 chunks. Each chunk is its own small upload, with its own signed URL. For now, say the app sends one chunk at a time. Replay the drop. At 602 MB, 150 chunks are fully stored, which is 600 MB. One more chunk was on its way, and the 2 MB of it that had arrived is lost. When the Wi-Fi returns, the app sends the other 106 chunks, 424 MB. The whole upload cost 1,026 MB for a 1,024 MB file.

The first tab sends the file as one request. The second sends 4 MB chunks. Play both, and watch what the drop costs.

The interactive version of this example is available when JavaScript loads.

Chunks raise three new questions.

First, after the drop, how does the app know which 150 chunks arrived? It cannot trust its own memory. The laptop may have restarted, and a chunk it believes it sent may have failed in its last second. So the server keeps the list. The app opens an upload session with POST /uploads, listing every chunk it plans to send. After a drop, it asks for the session again. The server answers with the chunks it still lacks.

Second, how does anyone know a chunk arrived intact? Suppose one chunk is damaged on the way. The app names each chunk by the SHA-256 hash of its bytes. The signed URL carries that hash as a signed checksum header, so the app cannot send a different one. Object storage computes the hash of what it received, and refuses the upload if the two differ. So a chunk stored under a name holds exactly the bytes that name describes.

Third, when does the new file appear on Sana’s phone? Not while chunks are arriving. A phone that opened the file after chunk 140 would get 560 MB of a 1 GB video. So nothing is visible until the app calls commit. The server checks that all 256 chunks are stored. Then one database transaction publishes the new version. Before that moment, the phone sees the old file. After it, the phone sees the whole new one.

The chunk size is a trade. Smaller chunks lose less on a drop. They also find more to reuse when a file changes, as the next deep dive shows. Larger chunks mean fewer requests and fewer rows to track. Dropbox cuts files into 4 MB blocks and hashes each with SHA-256 (content hash), so 4 MB is a good default to name.

In practice the app sends four chunks at a time, so one slow chunk does not hold up the rest. A drop then loses up to four chunks in flight. That is 16 MB at most, still small next to 602 MB. A session that never commits expires after a day, and the cleanup section below removes its chunks.

S3’s own multipart upload also resumes a large file, so it is fair to ask why not use it. It resumes one object, and its parts belong to that object. Sana’s next save of the video is a new object. S3 can copy the unchanged parts into it without uploading them again, but the new object still stores its own copy of every part. Chunks named by their hash are separate objects. The next version points at the ones it shares and stores them once, which is what the next deep dive needs.

The gap: A long upload over a bad network will be cut off partway.

Option 1: Upload through the API servers

The app POSTs the whole file to our API, which writes it to object storage.

Works when: Files are small and users are few, as in an internal tool.

Breaks when: Our servers carry 200 TB a day, and a drop at 602 MB throws away 602 MB.

Option 2: One signed URL, one PUT

The API checks permission and signs one URL. The app uploads the whole file straight to storage.

Works when: A failed upload is cheap to repeat, such as a profile photo.

Breaks when: It is still one request, so the drop costs 1,626 MB in total.

Option 3: S3 multipart upload

Upload one object in parts, and resume by sending the missing parts.

Works when: One large object must resume and nothing reuses its pieces, as in a backup archive.

Breaks when: Parts belong to one object, so the next version stores its own copy of every unchanged part.

Option 4: 4 MB chunks named by their hash, in an upload session (start here)

Each missing chunk gets its own signed URL. The server tracks which arrived, and commit publishes the version.

Works when: Always, for a sync service. A drop costs the chunks in flight, and the next version can reuse chunks.

Watch for: Every chunk costs a request and a row, so one large request becomes many small ones.

In the interview: Open with option 4, and give the arithmetic: 256 chunks, and a drop costs the chunks in flight rather than 602 MB. Name option 2’s signed URL as the reason our servers never carry bytes. Bring up option 3 only if the interviewer asks why not S3’s own multipart upload, and say its parts cannot be reused by the next version.

Say this: One request is one failure, so a drop at 602 MB throws away 602 MB. I cut the file into 4 MB chunks named by their SHA-256, and record them in an upload session. Each missing chunk goes straight to object storage through its own signed URL. A drop then costs only the chunks in flight, and the version is published after every chunk is stored.

Sana adds 2,000 rows to the top of a 200 MB file. How much does her laptop upload?

Sana keeps survey results in a 200 MB CSV file. She makes two edits on different days. First she corrects 600 rows in the middle, which changes 64 KB of the file. Later she pastes 2,000 new rows at the very top, which adds 192 KB.

The simplest answer uploads the whole file on every save. Each edit then costs 200 MB of upload for a change smaller than a photo. It also stores a second 200 MB copy. Sana would not notice on office Wi-Fi. The service would, because most of what it receives would be bytes it already holds.

The last deep dive already gives something better. The file is cut into 4 MB chunks, and each chunk is named by its hash. Call these fixed-size chunks blocks, to tell them apart from a kind that comes later. A 200 MB file is 200 ÷ 4 = 50 blocks. Before uploading, the app sends the list of hashes in POST /uploads, and the server answers with the ones it lacks.

Try the first edit. The 600 corrected rows sit inside one block. That block’s bytes change, so its hash changes. The other 49 blocks hold the same bytes as before, so their hashes match. The laptop uploads one block, 4 MB.

Now try the second edit. The 192 KB of new rows go in front of everything. Every old byte moves 192 KB later in the file. The cuts stay where they were, at 4 MB, 8 MB, 12 MB. So every block now holds a different slice of the data, and its hash is new. The file is now 200.2 MB in 51 blocks, and not one of them matches an old hash. The laptop uploads the whole file again.

The cuts are tied to positions in the file. An insertion moves every byte after it, so it moves the data past every cut.

One fix keeps the cuts where they are and searches harder. This is the rsync idea. The laptop still has the hashes of the blocks it last synced. Instead of cutting the new file at fixed places, it slides a 4 MB window along it, one byte at a time. At each position it checks whether the window matches one of the old blocks. That sounds slow, but a rolling checksum is cheap to move: slide the window one byte, and the checksum is updated by adding the byte that enters and removing the byte that leaves. Only when that cheap checksum matches does the laptop compute a strong hash to confirm it. Past the new rows, the window lines up with old block 1, then block 2, and so on. The laptop sends only the new rows, about 0.2 MB.

That fixes the upload and leaves the storage. The server still stores each version as fixed 4 MB blocks. So it rebuilds the new file from the old blocks and the new rows, and cuts it at fixed places again. It gets 51 new blocks, and stores 200.2 MB for a 192 KB insertion.

The better fix moves the cuts. Let the content decide where a chunk ends, instead of its position in the file.

Here is how. Read the file, and at every byte compute a hash of the few dozen bytes just before it. Cut wherever that hash ends in a chosen pattern. Say the rule is that the last 22 bits of the hash are zero. That happens at about one byte in 4 million, so chunks average about 4 MB.

Now look at what the insertion does. A cut depends only on the bytes just before it. The new rows change the bytes at the top, so the chunk that holds them changes. After them, the old data is unchanged, only shifted. The same short patterns appear, 192 KB later, and the cuts land on the same bytes as before. Every chunk after the first is identical to an old one, with the same hash. Only the first chunk is new: the new rows plus the old first chunk. In the experiment below the old first chunk is 4.9 MB, so the new one is 5.1 MB. The laptop uploads that one chunk, and the server stores it. This is content-defined chunking.

It has costs of its own. Chunks now vary in size. In the experiment, the chunk that holds Sana’s first correction is 8.2 MB, so that edit uploads 8.2 MB instead of 4 MB. A chunker can also cut too often or too rarely by luck. FastCDC’s normalized chunking makes a cut unlikely before 4 MB and likely after it, so most chunks land close to 4 MB.

One kind of file defeats every method. A file compressed as one stream, such as a .gz archive, changes every byte after an edit. A .zip or a .pptx is kinder, because it compresses each part separately, so the untouched parts can still be found.

The four tabs are the four methods, and the two chips are Sana’s two edits. The blocks are drawn to scale.

The interactive version of this example is available when JavaScript loads.

The experiment runs each method for real on a scaled-down model of the file, in which each byte stands for 16 KB. The model is small, so its content-defined chunks average 4.5 MB rather than 4.

Reusing chunks across accounts looks like free storage. It also leaks information. Suppose Mallory, an ordinary signed-in user, wants to know whether anyone has stored a particular document, say a leaked contract. She uploads it. If the server replies that it already has every chunk, she has learned that someone has the file. Researchers showed this attack against real cloud storage services in 2010, using only whether an upload was skipped and how fast it finished. So the check “which chunks do you already have?” only ever looks inside the caller’s own account.

The gap: An edit changes a few bytes of a large file, and an insertion shifts every byte after it.

Option 1: Upload the whole file again

Every save uploads every byte.

Works when: Files are small, such as a short text note.

Breaks when: A 64 KB correction costs 200 MB of upload and 200 MB of new storage.

Option 2: Fixed 4 MB blocks (start here)

Cut every version at the same 4 MB offsets, and upload only blocks whose hash the server lacks.

Works when: Edits change bytes in place. The correction costs one block, 4 MB.

Watch for: Rows pasted at the top shift every boundary, so all 51 blocks are new.

Fix within it: When insertions matter, move to option 4.

Option 3: A rolling match, the rsync idea

Slide a window over the new file to find old blocks at any offset, and send only what matches nothing.

Works when: Bandwidth is the cost that matters and storage is not. The insertion uploads 0.2 MB.

Watch for: The server still stores fixed blocks, so it stores 200.2 MB for the insertion.

Option 4: Content-defined chunks

Let a rolling hash of the bytes choose each cut, so cuts fall back into place after an insertion.

Works when: The service pays for both upload and storage. The insertion costs 5.1 MB of each.

Watch for: Chunk sizes vary, so the correction costs 8.2 MB, and a file compressed as one stream defeats every method.

Fix within it: Normalized chunking keeps most chunks close to 4 MB.

In the interview: Open with option 2: the upload already cuts files into 4 MB blocks, so reuse by block hash comes free, and it handles an edit in place. When the interviewer asks about an insertion near the start, show why every block shifts. Then offer option 4, which saves the upload and the storage, or option 3 if they mention rsync, which saves only the upload. Whichever you pick, the lookup for existing chunks stays inside the caller’s own account.

Say this: I reuse the 4 MB blocks the upload already makes, so a block whose hash the server holds is not sent again. That handles an edit in place. An insertion near the start shifts every boundary, so if that matters I cut chunks where the content says to, with a rolling hash, and the cuts fall back into place. The check for which chunks exist stays inside the caller’s own account.

The laptop saves a file. How does the phone find out, and what if it was asleep?

Sana’s laptop, phone and tablet all follow the same folder. She saves the deck on the laptop. The phone should show the new version within 5 seconds. The tablet is switched off in her bag, and should catch up whenever it wakes.

The simplest answer is polling. Every 30 seconds, each device asks for the folder’s full listing and compares it with its own. That works for one office. Now count at our size. About 50 million devices are online at the busiest hour. 50 million ÷ 30 seconds is about 1.7 million listings a second. Almost every answer is “nothing changed”. And a change can still take 30 seconds to arrive, six times the 5-second target.

A better start is a change log. Every commit appends an entry to the folder’s log, such as “file F9 is now version 8”. A device remembers its place in the log, and asks only for entries after it. That remembered place is its cursor. A device that slept for a week asks for a week of entries, and nothing more.

That leaves one question: how do you name a place in the log? The obvious answer is a time.

Watch two saves in one folder. The laptop starts saving the deck at 10:00:02.900, so its change is stamped 02.900. Its commit is slow, and will not finish until 03.400. Meanwhile the phone saves a receipt photo. That commit starts at 03.100 and finishes almost at once.

At 03.200 the tablet asks for changes after its last one. It finds the phone’s change, stamped 03.100, and saves 03.100 as its cursor. At 03.400 the laptop’s change finally becomes visible, stamped 02.900. From now on the tablet asks only for changes after 03.100. It will never see the laptop’s save. It shows the old deck, with a green tick.

The stamp recorded when the laptop’s commit started. The cursor needed the order in which changes became visible. Those are two different orders.

A database ID fails the same way. Say each change row gets an auto-increment ID when it is inserted. The laptop’s row is inserted early and gets 41. The phone’s row gets 42 and commits first. The tablet reads 42 and saves it. Then 41 appears, behind the cursor. The ID was handed out when the row was written, not when it became visible.

So hand the number out at the very end. The commit takes the folder’s next change number as its last step. Replay the two saves. The phone’s commit reaches its last step first and takes 41. The tablet reads at 03.200, sees 41 and saves it. The laptop’s commit reaches its last step at 03.400 and takes 42. The tablet’s next read asks for changes after 41, and gets 42.

One small gap is left. Two commits can reach their last step at almost the same moment. Commit X takes 43, and commit Y takes 44. Y’s transaction finishes a millisecond before X’s. A tablet reading in that millisecond sees 44 and saves it. Then X appears behind it, with 43.

So taking the number and becoming visible have to happen as one step. The commit takes the number by adding one to a counter on the folder’s own row. The database locks that row until the transaction finishes. Commit Y has to wait for the row. When X finishes, Y reads the counter X left behind and takes 44, and by then X is already visible. At a stricter isolation level the database refuses Y instead, and Y retries, which ends the same way. Numbers now appear in exactly the order commits become visible, and no change can land behind a cursor.

The lock has a price. Only one commit in a folder can hold it at a time. The bytes were uploaded before the commit began, so the lock is held only for the commit’s last step, about 2 milliseconds. 1,000 milliseconds ÷ 2 is 500 commits a second for one folder. That is plenty for a person’s files. The shared-folder section below deals with a folder that needs more.

The three tabs replay the same two saves with a timestamp, a database ID and a counter taken at commit.

The interactive version of this example is available when JavaScript loads.

The log makes sync correct. It does not make it fast, because the phone still has to know when to read. So the phone keeps one request open to a notification service: “tell me when folder 7 passes 41”. The request waits up to about 60 seconds. When the laptop’s commit writes change 42, that entry is passed to the notification service, and the waiting request returns at once. The phone reads the log after 41, applies change 42 and saves 42.

The notification says only “folder 7 changed”, and that is on purpose. If it is lost, the phone’s next request asks whether folder 7 has passed 41, and the answer comes back at once. If it arrives twice, the second read finds nothing new. The log is the truth, and the notification only says where to look. Google Drive’s changes API is built this way. Its notifications “don’t contain details about the changes”, and a client reads the change feed from a saved page token (managing changes).

Now compare the costs. At the peak from the requirements, about 7,000 saves a second each wake the owner’s other 2 devices. That is 14,000 wake-ups a second, instead of 1.7 million listings. The new cost is connections. 50 million devices each hold one open request. A notification server can hold tens of thousands of quiet connections, so this takes a fleet of a few thousand machines, each doing very little work.

A phone in the background cannot keep a connection open, so it gets a mobile push notification instead. The tablet in the bag gets nothing at all. When it wakes, it reads the log from its cursor, in pages of 500, and catches up.

The gap: A device has to hear about every change in its folder, and later carry on from the last one it applied.

Option 1: List the whole folder every 30 seconds

Each device fetches the full listing and compares it with its own.

Works when: Devices and folders are few, as in a service for one office.

Breaks when: At 50 million devices that is 1.7 million listings a second, and a change still waits up to 30 seconds.

Option 2: Changes since a timestamp

Each change is stamped with a time, and a device asks for changes after the last stamp it saw.

Works when: Commits in one folder never overlap.

Breaks when: A slow commit stamped 02.900 becomes visible after a cursor at 03.100, and is never read.

Option 3: An auto-increment change ID

The database numbers each change row, and a device keeps the last ID it saw.

Works when: Only one commit in a folder is ever in progress.

Breaks when: The ID is handed out at insert, so a slow commit lands behind the cursor the same way.

Option 4: A counter taken at commit, a cursor, and a push that only says look (start here)

The commit’s last step takes the folder’s counter under the row lock. A device reads everything after its cursor, and a push only says the folder changed.

Works when: Always, at this size. No change can appear behind a cursor, and a lost push only delays a device.

Watch for: One folder needs more than about 500 commits a second.

Fix within it: Split the busy folder into several namespaces, each with its own counter, as the shared-folder section shows.

In the interview: Open with option 4, and name its three parts: numbered at commit, a cursor per folder, and a push that only says look. When the interviewer asks why not a timestamp, walk through option 2’s skipped save, and add that option 3 fails the same way. Mention the 500-a-second ceiling before they ask about a busy folder.

Say this: A timestamp or an auto-increment ID is handed out before a commit becomes visible, so a slow commit can land behind a device’s cursor and never be read. I number changes with the folder’s counter as the commit’s last step, so numbers follow commit order. Each device keeps a cursor and reads everything after it. A push only says the folder changed, so a lost push only delays a device.

The tablet edited the file offline while the laptop changed it too. Which edit wins?

At 09:00 the laptop and the tablet both hold version 7 of the deck. The tablet goes offline on a train at 09:05. At 09:20 the laptop adds a chart and saves version 8. At 09:30 the tablet rewrites the summary slide of its version 7. At 10:10 the tablet reconnects and uploads.

The simplest rule keeps whichever upload arrives last. The tablet’s upload arrives last, so it becomes version 9. Version 9 is the tablet’s copy of version 7 with a new summary. The laptop’s chart is not in it, and nobody is told. Sana finds out in front of a client. Keeping the edit with the later modified time does not help either. The tablet’s edit at 09:30 is later than the laptop’s at 09:20, so it still wins, and device clocks disagree anyway.

A lock looks like it would prevent this. The app takes a lock on the server when it opens a file, and only the lock holder may save. But the tablet is on a train, and cannot reach the server to ask for the lock. It either refuses to let Sana edit, or lets her edit and meets the same clash at 10:10. A laptop that goes to sleep with the file open also holds the lock until someone clears it. A lock that expires after a few minutes fixes the sleeping laptop. It does nothing for the tablet on the train.

The working answer starts from something the tablet does know: which version it edited. So every commit names its base. The tablet’s commit says “this is based on version 7”. The server looks at the file’s head, the version the file currently points at, which is 8. The base is not the head, so someone else saved in between. The server refuses the commit with 409.

The tablet does not throw its edit away. It saves the edit as a new file beside the original, named for the device or the person, the words “conflicted copy” and the date. Now the folder holds version 8 with the chart, and a conflicted copy with the new summary. Neither edit is lost, and Sana can see there are two.

That check is a compare-and-set on the file’s head: move the head to the new version, only if it is still the base. The laptop’s commit passed it at 09:20, because the head was 7. The tablet’s failed at 10:10, because the head was 8.

The three tabs are last upload wins, a lock, and the base check. Step through what happens to the deck in each.

The interactive version of this example is available when JavaScript loads.

Dropbox does this. It creates a conflicted copy when someone edits offline while someone else edits, and names it the same way (conflicted copies). The first commit wins the file, and the second becomes a new file.

Two details make it hold up. The tablet may lose the server’s reply and retry ten times as it reconnects. So it creates the copy with its upload’s ID. The server remembers that ID, and answers every retry with the copy it already made. And the same rule covers renames and deletes. A move or a delete carries the version the app last saw. A delete from a device that never saw the chart is refused, and the device is told why.

Merging is the obvious next idea. With the common base, version 7, a three-way merge could combine the chart and the summary when they touched different lines. But a deck is not lines of text, and a file store does not understand its format. Live merging belongs to a document editor such as Google Docs, which merges edits keystroke by keystroke with techniques such as operational transformation or CRDTs. A file store promises something smaller that is always true: no edit is lost, and a person can see there are two.

The gap: The tablet edited version 7 while the server moved on to version 8.

Option 1: The last upload wins

Keep whichever upload arrives last, or has the later modified time.

Works when: Only one device ever writes the file, as in a backup.

Breaks when: The tablet’s upload becomes version 9, and the laptop’s chart vanishes without a word.

Option 2: Lock the file while someone edits it

An app takes a lock when it opens a file, and only the lock holder may save.

Works when: Every device is online while it edits, as on an office network drive.

Breaks when: A tablet on a train cannot ask for the lock, and a sleeping laptop holds it until it expires.

Option 3: Name the base version, and keep both on a conflict (start here)

Every commit names its base. A stale base gets 409, and the edit is saved as a conflicted copy.

Works when: Always, for files the service treats as opaque bytes.

Watch for: A person now has two files to reconcile by hand.

Option 4: Merge the two edits

With the common base, a three-way merge combines edits that touched different lines.

Works when: The file is text, and the edits touched different lines.

Watch for: A deck is not lines of text. Merging is a document editor’s job, not a file store’s.

In the interview: Open with option 3; the phrase that carries it is “every commit names its base”. Rule out options 1 and 2 in a sentence each. Offer option 4 for text files only if the interviewer asks about merging, and say that live co-editing is out of scope.

Say this: Last upload wins loses the laptop’s chart without telling anyone, and a lock cannot be taken on a train. So every commit names the version it was based on, and the server refuses a commit whose base is no longer the head. The refused edit is saved as a conflicted copy beside the original, so neither edit is lost. Merging is a document editor’s job, not a file store’s.

A laptop comes back after 45 days. Why do deleted photos not come back with it?

Sana’s old laptop spends 45 days in a drawer. While it is away, her phone deletes two beach photos on day 3 and adds invoice.pdf on day 10. Before the drawer, the laptop had created notes.txt and never uploaded it. report.docx sits unchanged on both. The server keeps 30 days of change log.

The simplest answer compares folders. The laptop wakes, lists its folder, lists the server’s, and looks at the differences. It finds these.

  • invoice.pdf is on the server and not on the laptop.
  • report.docx is on both.
  • notes.txt is on the laptop and not on the server.
  • The two beach photos are on the laptop and not on the server.

Look at the last two lines. notes.txt is new work that must be uploaded. The photos were deleted on purpose and must be removed. To a folder comparison they look exactly the same: the laptop has them, and the server does not. A laptop that uploads whatever the server lacks brings the deleted photos back to every device.

So a delete has to leave a trace. When the phone deletes a photo, the server keeps the file row. It sets deleted_at on it, and appends a delete entry to the change log. That entry is called a tombstone. A device back after 10 days replays the log from its cursor, reads that beach1.jpg was deleted, and removes its copy. Taking a file out of the trash is a new version and a new log entry, so every device hears about it the normal way.

Tombstones work for any device that comes back within 30 days. This laptop is back after 45. The log entries between its cursor and the oldest one kept are gone, and the two deletes are among them.

For a cursor older than the log, the server answers 410 reset. It sends the folder’s listing as it is now, and a new cursor to carry on from. A big folder’s listing takes many pages, and the phone keeps saving while the laptop reads them. A file moved from a page not yet read to one already read would be missed. So the server builds the whole listing as of one change number, and serves every page from that snapshot.

The listing alone brings back the same puzzle, because the photos and notes.txt still look the same. The last piece is on the laptop. The app keeps a journal of every file it has synced, and at which version. With it, each file has an answer.

  • beach1.jpg and beach2.jpg: the journal says they were synced, and the server no longer has them. They were deleted elsewhere, so they move to the laptop’s own trash.
  • notes.txt: the journal has no record of it. It was created here, so it is uploaded.
  • invoice.pdf: the server has it and the laptop does not, so it is downloaded.
  • report.docx: the same version on both, so it stays.

Then the laptop reads the log from its new cursor, like any other device.

One case has no clean answer: a file the laptop synced, the server deleted, and the laptop edited while away. Removing it loses the edit. Uploading it brings back a deleted file without a word. So the laptop uploads it as a conflicted copy, the same answer as an offline edit.

Both tabs start from the reset listing. The first uploads whatever the server lacks. The second compares with the journal.

The interactive version of this example is available when JavaScript loads.

The journal matters on a normal day too. The app’s watcher on the local disk reports every file it writes, including files the app itself just downloaded. Without a record of what it applied, the app would upload its own download back as a new edit. File watchers can also drop events when they fall behind. Linux’s inotify says so in its manual. So a desktop app rescans its folder against its journal now and then.

The gap: To a returning laptop, a file deleted elsewhere and a file it created itself look the same.

Option 1: Delete the row, and let devices compare folders

A deleted file is removed from the metadata, and a returning device compares folders.

Works when: Nothing is ever deleted, as in an archive that only grows.

Breaks when: The laptop uploads the photos it finds missing, and the deleted photos come back.

Option 2: Record each delete in the change log

A delete is a tombstone in the log, and a device replaying the log removes its copy.

Works when: Every device comes back within the 30 days the log is kept.

Breaks when: A 45-day-old cursor finds the two deletes already gone from the log.

Option 3: Tombstones, a reset, and the device’s own journal (start here)

An old cursor gets 410 reset with a listing, and the journal tells deleted-elsewhere from created-here.

Works when: Always. Deleted photos go to local trash, and new files upload.

Watch for: A file synced, deleted on the server and edited on the laptop has no clean answer, so it becomes a conflicted copy.

In the interview: Open with option 3, and name its three parts: tombstones, a reset for a cursor older than the log, and the device’s journal. Use option 1 to explain why a delete must be recorded, and option 2 to show that tombstones alone run out at 30 days.

Say this: To a returning laptop, a file deleted elsewhere looks like a file it created, so I record every delete as a tombstone in the change log. The log is kept 30 days, so an older cursor gets a reset: the folder as it is now, taken at one change number. The laptop compares that listing with its own journal of what it synced. A synced file the server lacks was deleted elsewhere, and a file never synced is new.

If they push: when is it safe to delete a chunk?

Chunks are shared. One chunk can sit in version 7 and version 8 of a file, and in a copy of it in another folder. So a chunk can be deleted only when nothing uses it. Counting users looks enough: each chunk row counts the versions that name it, and cleanup deletes the chunk when the count reaches zero.

Watch a race. At 10:00:00 the laptop asks which chunks exist, is told that chunk a71e does, and skips uploading it. At 10:00:01 the last old version using a71e expires. Its count reaches zero, and cleanup deletes it. At 10:00:02 the laptop commits a version that names a71e. The new version points at bytes that are gone. Waiting 7 days after the count reaches zero narrows the window, and does not close it. An upload told that a71e exists on the last day of the wait races the delete exactly the same way.

Checking and deleting are two steps, and the upload acts in between. The fix is to make both sides write the same row. When the server tells an upload that a chunk exists, it pins the chunk on the chunk’s row, for the life of the session. A session can expire, and its pins with it. So the commit claims each chunk by updating the chunk’s row too, and only if the chunk is still stored. Cleanup marks a chunk as deleting in one conditional update, only if its count and pins are both zero and it has been unused for 7 days. The database puts the two writes to the chunk’s row in order. Whichever arrives first wins, and the other changes nothing. A commit that loses finds the chunk marked, fails, and the device uploads that chunk again.

The count itself can be wrong. A decrement retried after a timeout subtracts twice, and the count reaches zero while a version still names the chunk. So also keep a row for each pair of version and chunk. Those rows are the truth, and the count on the chunk’s row is a fast copy of it. A nightly job recounts each chunk from the pairs and repairs any count that drifted. The 7 days are a margin for bugs like that, not the proof of safety.

The code has three tabs: the commit, the change log read from the sync deep dive, and cleanup.

The interactive version of this example is available when JavaScript loads.

After the mark, cleanup deletes the object, and then the row. Object storage can add one more margin. With versioning on, and a rule that expires old versions after a few days, a deleted object can be recovered for those few days.

Cleanup is also what keeps the bill honest. New bytes arrive at 200 TB a day, which is 73 PB a year before the store’s own copies. Old versions and the trash are kept for 30 days, and their chunks stop costing anything only if cleanup runs. Track the bytes reclaimed per day. A cleanup job that silently stops looks like a busy month.

Who may read a file, and how fast can they get it?

Access is checked in one place, the metadata service, on every call. Each namespace has members with a role, viewer or editor. A folder shared with someone is a namespace they are a member of, and everything inside it inherits that role. A share link is a token that grants viewer access to one file or folder. It can be revoked.

The bytes are protected by the signed URLs. The metadata service signs a download URL only after the permission check. Each URL lasts 15 minutes. So revoking Omar stops new URLs at once. A URL he was handed a minute ago still works until it expires. A copy he already downloaded stays on his laptop. No server can take it back. Say both limits out loud when you describe revocation.

Downloads are fast for the same reason uploads are. The chunks of a version are separate objects, so an app fetches four or eight at a time. A file shared as a public link may be downloaded by thousands. It can be served through a CDN, because a chunk named by its hash never changes and can be cached forever.

Everything is encrypted in transit with TLS, and at rest by the object store and the database. End-to-end encryption, where only the devices hold the keys, is a product choice with a price. The server can no longer reuse chunks, generate previews, scan for malware or search inside files.

A team folder has 20,000 members. What breaks?

A company shares one folder with 20,000 employees, about 40,000 devices. Someone saves a file in it every second.

The notifications are the first problem. Each save could wake 40,000 devices. The notification service is keyed by namespace, not by file: a device waiting on namespace 12 is woken once, whatever happened in it. The device then reads every change after its cursor in one call. Ten saves in one second cost each device one wake-up and one read.

The reads are the second. 40,000 devices waking in the same second all ask for the same page of the change log. That page is the newest one, so it is cached. The metadata service also spreads the wake-ups over a second or two, so the devices do not all ask at once.

The counter is the third. One namespace takes about 500 commits a second through its lock. A folder that needs more is split: each big subfolder becomes its own namespace with its own log and counter. That is the same split by namespace that spreads all the metadata across databases. A device then follows several cursors, one per namespace, which it already does for every folder shared with it.

Walk one save through the final design

At 09:20 Sana’s laptop saves the deck, file F9, based on version 7. The app cuts it into 50 chunks and calls POST /uploads. The metadata service checks her role, pins the 48 chunks her account already holds, and signs URLs for the other 2. The laptop uploads them straight to object storage, which checks each one’s SHA-256.

The laptop commits. One transaction claims the 50 chunks and checks that F9’s head is still 7. It writes version 8, moves the head, and appends change 42, taken from namespace 7’s counter. The new change entry reaches the notification service.

The phone’s long poll returns. The phone reads the changes after 41 and gets change 42. It fetches the 2 new chunks in parallel and checks them. Then it renames the temporary file over the old one, and saves 42 as its cursor. The tablet is on a train. It will read the same log when it reconnects, and if it edited version 7, its commit will be refused and saved as a conflicted copy.

The interactive version of this example is available when JavaScript loads.

RequirementWhere it is metThe trade-off
FR1 upload, resumableChunks with signed URLs; an upload session; commit publishes a versionMany small requests instead of one large one
FR2 download any versionA version is a chunk list; chunks fetched in parallel and checkedOld versions cost storage for 30 days
FR3 sync every deviceA change log numbered at commit, cursors per namespace, long-poll notificationsOne namespace takes about 500 commits a second
FR4 share and revokeNamespaces with members and roles; URLs signed after the checkA URL already issued works for up to 15 minutes
FR5 versions and trashTombstones and old versions kept 30 days; cleanup with pins and claimsA cleanup job that stops costs money silently

How does the answer change for Google Drive, backup or photo sync?

Interview variantWhat carries overWhat changes
Design Dropbox, Google Drive or OneDriveEverything on this pageNothing; these are the product names for the same question
Design cloud storage, like S3Immutable objects named by key, checksumsThat is the object store itself: disks, replication and erasure coding, with no folders or sync
Design a backup serviceChunks, reuse and versionsOne device writes, nobody edits elsewhere, and restore points matter more than live sync
Design photo sync, like Google PhotosUpload, the change log and devices following itPhotos are rarely edited, so conflicts are rare; thumbnails and search matter more
Design a file-sharing link serviceChunks, signed URLs and a CDNNo devices to keep in agreement; links expire and downloads dominate
Design a distributed file system, like GFS or HDFSLarge files split into chunks with a metadata serviceClients are servers in one data centre, and the metadata holds where each chunk replica lives

What should an answer reach at each level?

LevelWhat the answer shows
Intern or mid-levelSeparates metadata from bytes and sends bytes straight to object storage. Splits files into chunks so an upload resumes. Keeps a list of chunks per version.
SeniorPublishes a version by one transaction after every chunk is stored. Detects conflicts with a base version and keeps both edits. Uses a change log with a cursor and push that only says look.
StaffExplains why a timestamp or an auto-increment cursor skips a change, and numbers changes at commit. Handles the returning device with tombstones, a reset and a journal. Makes cleanup safe against a racing upload.
PrincipalSets the promises with the business: how long history and the trash last, what revocation can and cannot stop, whether reuse crosses accounts. Explains the cost of end-to-end encryption, and when a shared folder must be split.

Test yourself with the quiz, then pick another problem from the System Design interview breakdowns.

By GOAT Engineer. Back to all breakdowns. Privacy Policy · Contact.