Google Drive

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-04-16

Problem Statement

Design a Google Drive–like cloud-based file storage and synchronization system that allows users to upload, store, share, and access files (documents, images, videos, etc.) from any device securely and efficiently.


The system must support real-time file collaboration, versioning, access control, syncing across devices, and scalable storage management with high availability and durability.


🎯 Objective


Build a scalable, distributed, and fault-tolerant file storage service that:


•Allows users to upload, download, edit, and share files/folders.
•Synchronizes files seamlessly across multiple devices.
•Maintains file versions and edit history.
•Provides fine-grained access control (read, write, share).
•Supports real-time collaboration for documents.
•Ensures data durability, consistency, and security.


✅ Functional Requirements


1. User Management


•Users can sign up, log in, and manage their profiles.
•Authentication using OAuth or federated identity (e.g., Google, Microsoft).
•Manage multiple devices per user.


2. File Management


•Upload, download, delete, rename, and move files/folders.
•Support large file uploads with chunking/resume.
•Maintain metadata (name, type, size, owner, timestamps).
•File versioning — users can restore or view previous versions.
•Trash management with retention period.


3. Folder & Hierarchy Management


•Nested folder structure with CRUD operations.
•Folder sharing with inherited permissions.
•Path-based or object-based access.


4. Sharing & Permissions


•Share files/folders with other users or public links.
•Permission levels: Viewer, Commenter, Editor, Owner.
•Access revocation and link expiration.
•Track who accessed or edited files.


5. Synchronization


•Sync files automatically between local clients and cloud.
•Detect and resolve file conflicts.
•Handle offline edits and background sync when online.


6. Collaboration


•Real-time editing for documents (optional advanced feature).
•Concurrent editing conflict resolution (via operational transform / CRDT).
•Live cursors, comments, and chat integration.


7. Search & Metadata


•Full-text search on file names, content (via indexing service).
•Tag-based or recent file discovery.
•Quick access shortcuts and starred files.


8. Storage Management


•Store large amounts of data efficiently and durably.
•Split large files into chunks and distribute across multiple nodes.
•Replicate files for fault tolerance.
•Deduplicate identical files across users.


9. Version Control


•Maintain multiple versions of the same file.
•Allow rollback to earlier versions.
•Automatic cleanup of old versions to save space.


10. Notifications


•Notify users when shared files are edited, deleted, or accessed.
•Sync clients get update notifications (push/pull model).


⚙️ Non-Functional Requirements


| Attribute | Requirement |

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

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

| **Availability** | 99.99% uptime |

| **Durability** | 11 nines (99.999999999%) for file storage |

| **Consistency** | Strong consistency for metadata, eventual for file sync |

| **Latency** | Upload/download latency < 1s for small files |

| **Security** | End-to-end encryption, role-based access control |

| **Reliability** | Automatic recovery and replication |

| **Observability** | Metrics for sync success, latency, storage health |


🧩 Core Components


| Component | Responsibility |

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

| **API Gateway** | Entry point for clients (upload, download, share) |

| **Authentication Service** | OAuth/JWT-based auth and user sessions |

| **Metadata Service** | Manages file/folder metadata, hierarchy, and permissions |

| **Storage Service** | Stores actual file chunks in distributed storage (e.g., S3, GFS) |

| **Upload/Download Service** | Handles chunked uploads, resumable transfers |

| **Sync Service** | Manages background syncing and conflict resolution |

| **Search Indexer** | Builds search index for metadata and content |

| **Notification Service** | Pushes changes to clients in real time |

| **Versioning Service** | Tracks and stores file revisions |

| **Access Control Service** | Validates sharing permissions for each request |

| **Audit & Logging Service** | Tracks all file operations for compliance |


🧱 High-Level Architecture

1 2 +------------------------+ 3 | Client Devices | 4 | (Web, Mobile, Desktop) | 5 +-----------+------------+ 6 | 7 v 8 +---------+---------+ 9 | API Gateway | 10 +---------+----------+ 11 | 12 +-------------------------------------------------+ 13 | Backend | 14 |-------------------------------------------------| 15 | | 16 | +---------------+ +----------------------+ | 17 | | Auth Service | | Metadata Service | | 18 | +---------------+ +----------------------+ | 19 | | | | 20 | v v | 21 | +---------------+ +----------------------+ | 22 | | Access Control| | Versioning Service | | 23 | +---------------+ +----------------------+ | 24 | | | | 25 | v v | 26 | +---------------+ +----------------------+ | 27 | | Sync Service | | Search Indexer | | 28 | +---------------+ +----------------------+ | 29 | | | | 30 | +---------------+ +----------------------+ | 31 | | Notification | | Storage Service | | 32 | | Service | | (Object Store / GFS) | | 33 | +---------------+ +----------------------+ | 34 +-------------------------------------------------+ 35 | 36 v 37 +--------------------------+ 38 | Distributed Object Store | 39 | (S3 / GFS / HDFS) | 40 +--------------------------+


🧾 Data Model (Simplified)



File Metadata Table

| Field | Description |

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

| file_id | Unique file identifier |

| user_id | Owner of the file |

| name | File name |

| parent_folder_id | Folder reference |

| size | File size |

| version | Current version number |

| is_deleted | Boolean flag |

| permissions | Access control list (ACL) |

| created_at | Timestamp |

| updated_at | Timestamp |


File Chunk Table

| Field | Description |

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

| chunk_id | Unique chunk ID |

| file_id | Reference to file |

| storage_path | Location in object store |

| checksum | Integrity hash |

| sequence_number | Order of chunk |

| size | Size of chunk |


Version Table

| Field | Description |

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

| file_id | File/folder reference |

| user_id | User with access |

| access_level | VIEWER / EDITOR / OWNER |

| shared_via | Direct link / explicit share |

| expires_at | Optional expiry timestamp |



⚡ Scalability & Performance Considerations


| Challenge | Solution |

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

| Large file uploads | Multipart uploads with parallel chunking |

| Hot files accessed by many users | CDN caching and read replicas |

| Metadata scale | Sharded metadata DB (based on user_id or file_id) |

| Sync conflicts | Versioning and last-writer-wins merge |

| Search performance | Use Elasticsearch for metadata and content |

| Storage cost | Deduplication and compression |

| Data durability | Triple replication across regions |



🔐 Security & Compliance

•Encryption:
•At-rest: AES-256 encryption for files.
•In-transit: HTTPS/TLS for all communications.
•Access Control:
•Role-based permissions (Viewer/Editor/Owner).
•Access tokens for shared links.
•Data Privacy:
•GDPR-compliant data deletion.
•Secure audit logs for every action.
•Authentication:
•OAuth 2.0 / OpenID Connect for identity federation.

🧠 Tech Stack (Example)

LayerTechnology

Frontend Clients

React (Web), Kotlin/Swift (Mobile)

Backend Framework

Java Spring Boot / Go / Node.js

Database (Metadata)

PostgreSQL / CockroachDB / Spanner

Storage Layer

AWS S3 / GCP Storage / HDFS

Search

Elasticsearch / OpenSearch

Queue

Kafka / RabbitMQ

Cache

Redis

Monitoring

Prometheus + Grafana

Auth

OAuth2.0 / Keycloak / Firebase Auth

🚨 Failure & Recovery

FailureMitigation

Node failure

Replicated data and auto-failover

Metadata DB corruption

Daily backups and multi-region replication

File upload interruption

Resumable uploads with chunk checksum validation

File conflict

Version creation and user conflict resolution

Data loss

Replication + periodic integrity checks

💡 Possible Extensions

•Real-time collaborative document editing (like Google Docs).
•Offline mode with background sync.
•Machine-learning–based content organization (auto-tagging, OCR).
•Integration with third-party apps (e.g., Slack, Gmail).
•Advanced sharing policies (org-level permissions).
•Preview support for media files.

✅ End Goal

Design a highly available, secure, and distributed file storage system (like Google Drive) that enables uploading, sharing, syncing, and collaborating on files with versioning, access control, and durability guarantees across billions of files and users.

Solution

Comments