Design a File & Photo Upload Service (like Google Drive)

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-05-01

Problem Statement

Design a highly scalable, reliable, and secure file upload and storage service (similar to Google Drive or Dropbox) that allows users to upload, store, view, share, and manage files and photos from multiple devices.


The system should support large file uploads, automatic resumptions, versioning, and preview capabilities (especially for images, videos, and documents).


🎯 Objective


Build a distributed system that can:


•Handle massive concurrent uploads efficiently.
•Store user files and metadata reliably and durably.
•Support file sharing, previewing, and syncing across devices.
•Optimize storage and retrieval for different file types (documents, photos, videos).
•Ensure security, availability, and data integrity.


✅ Functional Requirements


1. User Operations


Upload Files/Photos

•Support large files (up to several GBs).
•Allow resumable uploads if network fails.
•Show upload progress in real-time.

View Files

•Display uploaded files with metadata (name, size, date).
•Support file previews (for images, docs, videos).

Download Files

•Users can download original files securely.

Share Files

•Generate shareable links with configurable access (view/edit).
•Support collaborative editing (future extension).

Search and Filter

•Search by name, file type, or metadata tags.

Sync

•Support synchronization across devices.


⚙️ Non-Functional Requirements


| Attribute | Goal |

| ------------------- | --------------------------------------------------------- |

| **Scalability** | Handle millions of users and billions of files |

| **Durability** | Ensure zero data loss (≥99.999999999% durability like S3) |

| **Availability** | ≥99.99% uptime |

| **Latency** | Upload < 5s/GB (chunked), metadata queries < 100ms |

| **Security** | Data encryption (at rest and in transit) |

| **Reliability** | Resume failed uploads, detect corruption |

| **Cost Efficiency** | Optimize storage tiering and CDN usage |


🧩 Core Components


| Component | Responsibility |

| ----------------------------- | ------------------------------------------------------------ |

| **Upload API Service** | Handles user uploads, chunking, and validation. |

| **File Storage Service** | Stores actual file blobs (S3 or distributed file system). |

| **Metadata Service** | Manages file info (owner, type, size, version, permissions). |

| **User Service** | Handles user authentication & authorization. |

| **Sharing Service** | Generates and validates shared links. |

| **Thumbnail/Preview Service** | Generates previews for photos/docs. |

| **Sync Service** | Syncs changes across devices. |

| **Notification Service** | Alerts users on upload completion, sharing, or updates. |

| **CDN Layer** | Serves frequently accessed files and thumbnails globally. |


🧱 High-Level Architecture

1 2 +-------------------------+ 3 | Client App | 4 | (Web / Mobile / Desktop)| 5 +-----------+-------------+ 6 | 7 v 8 +----------+-----------+ 9 | API Gateway | 10 +----------+-----------+ 11 | 12 +-----------------------------------------+ 13 | | 14 v v 15+--------+ +--------------+ 16| Upload | | Metadata | 17| Service|<-----> Object Storage ---->| Service | 18+--------+ +--------------+ 19 | | 20 | v 21 | +---------------+ 22 | | DB (SQL/NoSQL)| 23 | +---------------+ 24 | 25 +--> Chunk Processor 26 | + Deduplication 27 | + Virus Scan 28 | 29 v 30+------------------+ 31| Thumbnail Service| 32+------------------+ 33 | 34 v 35 +--------------+ 36 | CDN Layer | 37 +--------------+ 38 | 39 v 40 +------------------+ 41 | End Users / APIs | 42 +------------------+


⚡ Key Flows


1. File Upload Flow


1.User initiates an upload request → POST /upload/initiate
2.System returns upload session ID and chunk size (e.g., 5MB).
3.User uploads file chunks → PUT /upload/{sessionId}/chunk
4.Each chunk is stored temporarily (in object store like S3).
5.After all chunks uploaded → client calls POST /upload/complete
6.Service assembles chunks, verifies checksum, updates metadata DB.
7.Notification is sent upon success.


2. File Download Flow


1.User requests file → GET /files/{fileId}
2.Service validates access (ACLs or token).
3.File is served via CDN or directly from object store.


3. File Sharing Flow


1.User generates a share link → POST /share
2.System issues a signed URL (with expiry or permissions).
3.Another user accesses the file using that URL.


🧾 Data Model (Simplified)


| Field | Description |

| ------------ | ------------------------- |

| file_id | Unique ID (UUID) |

| user_id | Owner of the file |

| file_name | Original name |

| file_type | MIME type |

| file_size | In bytes |

| storage_path | Location in object store |

| checksum | SHA256 for integrity |

| version | For version history |

| created_at | Upload timestamp |

| shared_with | List of users/permissions |


🔒 Security Considerations


•All uploads/downloads use HTTPS.
•Files are encrypted:
•At rest → AES-256.
•In transit → TLS 1.3.
•Access Control Lists (ACLs) define who can view/edit/download.
•Signed URLs prevent unauthorized access.
•Virus Scanning on upload (optional async process).


🧠 Scalability Considerations


| Challenge | Solution |

| ----------------------- | ---------------------------------------------------- |

| **Large file uploads** | Chunked upload + resumable sessions |

| **Hot files (popular)** | Serve via CDN edge cache |

| **Metadata scaling** | Shard metadata DB by user or file ID |

| **Storage growth** | Use tiered storage (hot/cold/archive) |

| **Concurrency** | Use message queues for async processing (Kafka, SQS) |

| **Fault tolerance** | Replicate data across regions |

| **Sync across devices** | Use event-driven updates via WebSockets |


📊 Observability and Monitoring


•Track:
•Upload success/failure rate
•Avg upload time per MB
•File access latency
•Storage usage per user
•Alerting on:
•Failed uploads > threshold
•Storage errors
•Abnormal latency spikes


🧩 Possible Extensions


•Real-time collaboration (Docs, Sheets).
•AI photo categorization (for Google Photos-like behavior).
•Trash & restore system.
•File versioning & diffing.
•Offline sync mode.


✅ End Goal


Design a Google Drive–like upload and storage platform that can:


•Handle massive file and image uploads reliably.
•Provide secure access and sharing features.
•Support previewing, versioning, and multi-device syncing.
•Scale seamlessly with user growth and storage demand.


Solution

Comments