bigonotes
System DesignDesign Problems

File Storage and Sync (like Dropbox)

A file storage and sync service run through the Interview Approach: upload files up to 50 GB in chunks, download them back, and keep every device in sync…

10 min readLast updated

A file storage and sync service lets you save files in the cloud, open them from anywhere, and see a change made on your laptop show up on your phone a few seconds later. The hard parts are the size of the files, up to 50 GB, and keeping many devices in step without losing anyone's work. The answer to both is to split files into chunks and keep a small metadata record next to them. This page runs it through the Interview Approach, one step at a time.

1. Functional requirements#

Three actions define the product:

  1. A user can store files, such as pictures and videos.
  2. A user can retrieve those files.
  3. A user's changed files sync across all their devices.

One constraint shapes everything else: a single file can be up to 50 GB.

Out of scope: sharing files with other users, real-time co-editing of documents, search inside files, and previews or thumbnails.

2. Non-functional requirements#

These four qualities shape the design:

Quality Requirement
Scalable Hold 10 billion files for 100 million daily active users
Availability over consistency Uploads and downloads keep working when parts fail. Another device may see a change a few seconds late
Low sync latency A change on one device reaches the user's other devices in under 1 minute
Fault tolerant Once a file is saved, it is never lost

A quick estimate shows which numbers change the design:

  • Storage: 10 billion files at an average of 1 MB is about 10 PB of file data. That can't live in a database, so file bytes go to blob storage and the database only holds metadata.
  • Metadata: 10 billion rows at about 1 KB each is about 10 TB. Too big for one comfortable Postgres instance for long, so plan to shard.
  • Large files: a 50 GB file over a 100 Mbps connection takes over an hour. One dropped connection must not restart the whole upload, so uploads have to be chunked and resumable.
  • Sync checks: if 100 million devices each asked "anything new?" every 30 seconds, that's over 3 million requests per second, almost all answered "no". Asking is too expensive; the server should tell devices when something changes.

3. Actors#

Two actors talk to the system, one person and one piece of software:

Actor What they do Access
User Uploads, downloads and opens files through the client app Signed in, sees only their own files
Sync agent Runs in the background on each of the user's devices. Uploads local changes and pulls remote ones Acts with the user's credentials, one per device

The sync agent is what makes this a sync service and not just storage. It watches the local folder, so the user never has to upload by hand.

4. API routing#

Three endpoints cover upload, download and sync. They go through an API gateway and load balancer to the servers:

Purpose Endpoint Response
Upload a file POST /v1/files 201 with the file ID
Download a file GET /v1/files/{fileId} The file, streamed in chunks, plus its metadata (optional)
Get changes between local and remote GET /v1/files?changed_since={timestamp} A list of the files that changed since that time

The upload request carries the file and its metadata:

{
  "file": binary,
  "metadata": FileMetadata
}

FileMetadata holds the name, path, size and content type. Step 6 changes how the bytes travel for large files, but the three routes stay the same.

5. High-level design#

Build the design one functional requirement at a time. Five boxes are enough: client, API gateway, server, blob storage (S3) and a Postgres database.

Store a file (requirement 1). The client sends POST /v1/files through the API gateway to a server. The server splits the file into chunks, stores each chunk in blob storage such as S3, and gets a URL back for it. Then it writes the file's metadata to Postgres:

Column Meaning
id (primary key) File ID
user_id Owner of the file
metadata Name, path, size, content type
s3_url Where the file's chunks live in blob storage
created_at When the file was first stored
updated_at When the file last changed
chunks Ordered list of {chunk_id, metadata}, where chunk metadata is its index, size and hash

Noting down the chunks is what makes the rest possible. The file in blob storage is just a set of pieces, and this list says which pieces make up the file and in what order.

Retrieve a file (requirement 2). The client sends GET /v1/files/{fileId}. The server reads the metadata, fetches the chunks in order, and streams them back to the client chunk by chunk. The client replays the chunks in order to rebuild the file and shows it to the user. Nothing ever holds a whole 50 GB file in memory.

Sync across devices (requirement 3). A sync agent on each device handles both directions.

Syncing a change from the current device:

  1. The agent observes changes on the device: new content, renamed files, and changed modified dates and times.
  2. It puts each change, with its metadata, on a local queue and works through it, calling the same upload endpoint. Only the chunks that changed are uploaded.
  3. The server stores the new chunks in S3 and gets their URLs back.
  4. The server updates the database with the new metadata and a new updated_at. If two devices change the same file, the latest change wins.

Syncing to the other devices:

  1. The user opens a file or folder, or the server pushes a change notice to the agent.
  2. The agent compares the local state with the cloud state by calling GET /v1/files?changed_since={timestamp} with the time of its last sync.
  3. On a mismatch, it calls the download endpoint to get the latest metadata.
  4. It downloads only the changed chunks, rebuilds the file and shows it to the user.

6. Address the non-functional requirements#

The basic design works for small files on a quiet day. Now take each quality and find where it breaks.

Large files (the 50 GB constraint)#

Sending a 50 GB file in one request body through the API gateway and server fails three ways. Connections drop, the servers spend all their bandwidth copying bytes, and any failure restarts from zero. Three changes fix it:

  • Chunk on the client. The client splits the file into chunks of about 5 MB, so a 50 GB file is about 10,000 chunks. It hashes each chunk (for example SHA-256) and sends only the metadata and the chunk list in POST /v1/files.
  • Upload straight to blob storage. The server answers with a short-lived pre-signed URL for each chunk, and the client uploads the chunks directly to S3, in parallel. The server only handles metadata. When every chunk has arrived, the client tells the server and the file is marked complete.
  • Resume, don't restart. The server records which chunks have arrived. After a dropped connection, the client asks which chunks are missing and uploads only those.

Hashes give a bonus. If a chunk with that hash already exists, the client skips it, which makes edits cheap: change one page of a large file and only one or two chunks travel. Downloads work the same way in reverse, with pre-signed URLs, so the client downloads only changed chunks. The trade-off is a smarter client and many more metadata rows, since each file now has a list of chunks to track.

Low sync latency (under 1 minute)#

Agents asking changed_since on a timer either miss the 1-minute target or flood the servers, as the estimate showed. Push instead:

  1. After a file's metadata is updated, the server publishes a change event to a queue or pub/sub topic, keyed by user.
  2. A notification service holds an open connection (WebSocket or long poll) to each online agent and pushes a short "something changed" message to that user's other devices.
  3. The agent then calls changed_since and downloads only the changed chunks.

The push carries no file data, so a lost message only delays sync. As a safety net, agents also check changed_since every few minutes and when they reconnect. The trade-off is millions of open connections to manage.

Use a server-assigned value for changed_since, not the device's clock. Device clocks drift, and a device that is a minute behind would miss changes. A per-user change counter that increases with every update is even safer than a timestamp.

Availability over consistency#

One server and one database would take the whole service down when either fails. Run several stateless servers behind the load balancer, and add read replicas of Postgres in several availability zones. If a replica or server fails, traffic moves to the others.

The cost is that devices may briefly disagree, which is the availability over consistency choice from step 2. The bigger question is conflicts: two devices edit the same file while one is offline. "Latest change wins" is simple, but it silently throws away the other edit. A safer version keeps the latest change as the file and saves the losing edit as a conflicted copy, so nothing is lost and the user decides.

Fault tolerance#

"Files have to be saved" means durability. Blob storage like S3 already copies every chunk across several availability zones, so chunk data is safe once the upload succeeds. The metadata needs the same care: run Postgres with a synchronous standby so a committed row survives the loss of the primary, and take regular backups.

Order matters. Write the metadata only after every chunk is confirmed in blob storage, so a file record never points at missing data. If an upload is abandoned halfway, its chunks are orphans; a background job deletes chunks that no file references after a day or so. Keeping old versions of the chunk list for a while also lets a user restore a file they deleted or overwrote by mistake.

Scalable#

File bytes scale with blob storage, which grows without any work from you. The metadata is where scale bites: 10 billion files and a burst of updates every time devices sync. Shard the metadata by user_id. Every request, including changed_since, is about one user's files, so each one goes to exactly one shard.

Inside a shard, index (user_id, updated_at) so changed_since reads a small range and never scans the table. Store chunks in their own table keyed by file ID once the chunk lists get long, instead of one large column on the file row.

Whiteboard sketch#

The sketch below is the design from steps 5 and 6: a client and a sync agent talking to servers through an API gateway, with file chunks in S3 and metadata in Postgres.

Whiteboard sketch: the client connects both ways to an API gateway and load balancer, which connects both ways to a server offering upload(file, filemetadata) and download(filemetadata). The client also uploads chunks to Blob Storage (S3) via the service, and downloads only changed chunks. The server stores chunks in blob storage and gets a URL back, then sends "Write Metadata" to a Postgres database holding FileMetadata: id, userid, metadata, s3URL, creationDate and chunks { chunkid, metadata }. Blob storage stores the file as chunks, which are replayed later at the client end. An agent exchanges "Push Change" messages with the API gateway and has an arrow to the Postgres database for its metadata updates

Key takeaways#

  • Three functional requirements: store files, retrieve them, sync changes across devices. Files can be up to 50 GB.
  • File bytes go to blob storage, and Postgres keeps only metadata and the ordered list of chunks.
  • Chunk files on the client (about 5 MB each), hash every chunk, and upload straight to blob storage with pre-signed URLs. Uploads resume instead of restarting.
  • Hashes mean only changed chunks travel, in both directions.
  • A sync agent on each device uploads local changes and pulls remote ones with changed_since.
  • Meet the 1-minute sync target with push notifications, plus an occasional changed_since check as a safety net. Use a server-side counter or timestamp, never the device clock.
  • Choose availability, and keep the losing edit of a conflict as a copy instead of discarding it.
  • Write metadata only after the chunks are stored, and shard metadata by user_id.
Revision notes · 2 min

Core idea#

A file storage and sync service splits files into chunks stored in blob storage and keeps a metadata record with the ordered chunk list in a database. A sync agent on each device uploads local changes and pulls remote ones.

Requirements#

  • Functional: store files, retrieve them, sync changed files across devices. Files up to 50 GB.
  • Non-functional: 10 billion files and 100 million DAU, availability over consistency, sync in under 1 minute, fault tolerant (a saved file is never lost).
  • Estimate: about 10 PB of file data, about 10 TB of metadata. A 50 GB upload takes over an hour, so it must be chunked and resumable.
  • Polling every 30 seconds from 100 million devices is over 3 million requests per second, so push changes instead.

Key points#

  • Actors: user (client app) and sync agent (one per device, in the background).
  • API: POST /v1/files, GET /v1/files/{fileId} (streamed in chunks), GET /v1/files?changed_since={timestamp}.
  • Metadata row: id, user_id, metadata, s3_url, created_at, updated_at, chunks (chunk_id plus index, size, hash).
  • Download replays the chunks in order on the client.
  • Local change: agent queues it, calls the upload endpoint with only changed chunks, server stores them and updates metadata and updated_at.
  • Remote change: agent compares local and cloud state with changed_since, then downloads only changed chunks.

Step 6 changes#

Requirement Change
Large files Client chunks (~5 MB) and hashes. Pre-signed URLs to upload straight to S3. Resume missing chunks
Sync latency Change event to a queue, notification service pushes to agents, agent calls changed_since. Periodic check as a safety net
Availability Stateless servers, read replicas across zones. Keep conflict losers as a copy
Fault tolerance S3 replicates chunks. Postgres synchronous standby. Write metadata after chunks. Clean orphan chunks
Scale Shard metadata by user_id. Index (user_id, updated_at)

Pitfalls#

  • Sending 50 GB through the servers in one request.
  • Using the device clock for changed_since. Clocks drift, so changes get missed.
  • Polling for changes on a short timer.
  • Plain last-write-wins silently loses the other edit.
  • Writing metadata before the chunks are confirmed.

Short line#

Chunk and hash files on the client, upload chunks straight to blob storage, keep metadata and chunk lists in Postgres sharded by user, and push change notices so each device's agent pulls only the changed chunks within a minute.

Found a mistake? Email ask@bigonotes.com