Design a File Storage and Sync Service
Chunking, delta sync, content-hash deduplication, separating metadata from blocks, change notifications across devices and handling conflicting edits.
A file storage and sync service lets a user store files in the cloud and keeps them synchronised across all their devices. A file saved on a laptop appears on the phone a moment later, and an edit made on one device reaches the others.
Uploading the whole file again each time it changes is correct but wasteful: changing one paragraph of a 1 GB file would transfer 1 GB. These notes describe how chunking and metadata make sync efficient and reliable.
1. Requirements
- Functional: upload and download files; organise them in folders.
- Functional: automatic sync across a user's devices; changes made offline sync when the device reconnects.
- Functional: file version history; sharing with other users.
- Non-functional: durability. A stored file must never be lost. This is the most important property.
- Non-functional: efficient use of bandwidth and storage; resumable transfers for large files.
- Non-functional: consistent metadata, so that all devices agree on the current state of a file.
2. Scale estimation
Assume 50 million users, 10 million of them active daily, each storing 10 GB on average. Total storage is 500 PB before replication and deduplication, which can only be held in distributed object storage. If each active user changes 20 files a day, that is 200 million file changes per day, about 2,000 per second, each producing metadata updates and notifications. Storage volume is enormous, while metadata traffic is moderate and needs strong consistency: two very different workloads.
3. Chunking
Files are not stored as single objects. The client splits each file into chunks, also called blocks, typically around 4 MB. Each chunk is identified by a cryptographic hash of its contents, such as SHA-256. The file is then described by an ordered list of chunk hashes.
- Resumable transfers: if the connection drops, only the unfinished chunk is sent again, not the whole file.
- Parallelism: several chunks can be uploaded or downloaded at once.
- Delta sync: when a file changes, only the chunks that changed are uploaded.
- Deduplication: identical chunks are stored once.
4. Delta sync
When a file is modified, the client splits the new version into chunks and computes their hashes. It compares them with the hashes of the previous version. Chunks whose hashes are unchanged are already on the server. Only the new chunks are uploaded, and then the file's metadata is updated to the new list of hashes. Editing a small part of a 1 GB file transfers a few megabytes.
With fixed-size chunks there is a weakness: inserting a single byte at the start of a file shifts every chunk boundary, so every hash changes. Content-defined chunking solves this by choosing boundaries from the data itself, using a rolling hash. An insertion then affects only the chunk in which it occurs, and the boundaries after it stay where they were.
Version 1 of report.pdf (16 MB) Version 2 (one page edited) chunk 0 hash a41f... chunk 0 hash a41f... unchanged chunk 1 hash 77c2... chunk 1 hash 77c2... unchanged chunk 2 hash 0be9... chunk 2 hash d5a0... CHANGED -> upload 4 MB chunk 3 hash 93e1... chunk 3 hash 93e1... unchanged Uploaded: 1 chunk (4 MB) instead of 16 MB. Metadata: new version row with chunk list [a41f, 77c2, d5a0, 93e1].
5. Deduplication
Because a chunk is stored under the hash of its contents, two identical chunks have the same key. Before uploading, the client sends the list of hashes to the server and asks which ones it does not have. Chunks that already exist, whether from an earlier version of the same file, from another file, or from another user, are not uploaded or stored again. The server simply adds a reference.
A popular file that thousands of users store is kept once. Each chunk has a reference count, and a chunk is physically deleted only when no file version refers to it any more. Cross-user deduplication can reveal whether a particular file already exists on the service, so some designs restrict deduplication to a single account.
6. Metadata and block storage
The system stores two kinds of data and keeps them separate. The chunk contents are large, immutable blobs. They go to object storage, which is cheap, effectively unlimited, and designed for very high durability through replication or erasure coding across zones.
The metadata is small and structured: users, folder hierarchy, file names, versions, the ordered list of chunk hashes for each version, sharing permissions and device sync state. It needs transactions and strong consistency, because renaming a folder or committing a new version must be atomic and all devices must see the same result. A relational database, sharded by user or workspace, is the usual choice.
files (file_id PK, owner_id, parent_folder_id, name, latest_version, is_deleted) file_versions (file_id, version, size, created_at, device_id) PK (file_id, version) version_chunks (file_id, version, seq, chunk_hash) PK (file_id, version, seq) chunks (chunk_hash PK, size, ref_count, storage_location) changes (owner_id, cursor BIGINT, file_id, version, op) -- per-user change journal devices (device_id PK, user_id, last_synced_cursor)
Device A (sync client: watcher, chunker, local index) | 1. changed file -> chunk + hash | 2. "which of these hashes are missing?" ------> Metadata service --> Metadata DB (SQL) | 3. upload only missing chunks ---------------> Block service ----> Object storage | 4. commit new version (expected base version) -> Metadata service | | | change event (queue) | v Device B <------ long poll / WebSocket: "changes available" -- Notification service | 5. fetch changes since last cursor ----------> Metadata service | 6. download new chunks ----------------------> Block service / CDN | 7. rebuild the file locally
7. Upload flow
The order of operations ensures that other devices never see a file whose data is incomplete. The client first uploads the missing chunks to the block service. Only when all chunks are safely stored does it ask the metadata service to commit the new version. The commit is a single transaction that writes the version, its chunk list and an entry in the user's change journal. If the client crashes midway, no version was committed; the orphaned chunks are removed later by garbage collection, and the upload can be resumed.
8. Notifying other devices
After a commit, the user's other devices need to learn that something changed. Polling the server every few seconds from every device would be wasteful. Instead each online device keeps a long-lived connection to a notification service, using long polling or a WebSocket. When a change is committed, an event is published and the notification service tells the user's connected devices that changes are available.
The notification carries no file data. On receiving it, the device asks the metadata service for all changes since its last synced cursor, downloads the chunks it lacks, and assembles the new version. A device that was offline does exactly the same when it reconnects: it presents its cursor and receives everything it missed. The per-user change journal with a monotonically increasing cursor is what makes sync reliable.
9. Conflicts
Two devices may edit the same file while one or both are offline. Both edits are based on version 5. Device A syncs first and creates version 6. When device B tries to commit, it states that its change is based on version 5, but the server's latest is now 6.
The server detects this with optimistic concurrency control: a commit is accepted only if its base version equals the current latest version. Device B's commit is rejected. For ordinary files the content cannot be merged automatically, so the client keeps both: the server's version remains the main file, and device B's version is saved alongside it as a conflicted copy with the device name and date. No work is lost, and the user decides which to keep. Real-time collaborative editors solve this differently, with operational transformation or CRDTs.
10. Additional considerations
- Version history: older versions are just older chunk lists. Unchanged chunks are shared between versions, so history is cheap. A retention policy removes old versions and decrements chunk reference counts.
- Integrity: the hash of each downloaded chunk is verified, which detects corruption in storage or transit.
- Security: encryption in transit and at rest; downloads through short-lived signed URLs.
- Cost: rarely accessed chunks are moved to cheaper cold storage tiers.
- Client efficiency: compress chunks, limit bandwidth use, and batch many small file changes.
Interview summary
- Requirements: durability first, efficient sync across devices, consistent metadata.
- Split files into chunks identified by content hash.
- Delta sync uploads only changed chunks; deduplication stores identical chunks once.
- Metadata in a transactional database, chunk data in object storage.
- Notify devices of changes over a long-lived connection; resolve version conflicts with conflicted copies.
Summary
A sync service treats a file as a list of content-addressed chunks. That single idea provides resumable uploads, delta sync and deduplication. Chunk data lives in durable object storage, while a transactional metadata store records versions and a per-user change journal. Devices are notified of changes and pull what they are missing, and conflicting edits are preserved as separate copies.
Puneet Tiwari
Full Stack Developer
