1) Problem Clarification / Làm rõ bài toán
EN
We need to design a distributed system that supports:
- storing files of any size
- syncing files across devices
- folder structure
- sharing links
- file versioning
- conflict handling
- offline client sync
VI
Thiết kế hệ thống có khả năng:
- lưu file mọi kích thước
- đồng bộ đa thiết bị
- cấu trúc thư mục
- link chia sẻ
- version file
- xử lý conflict
- sync khi offline
2) Core Architecture Overview / Kiến trúc tổng quan
Client → Sync Service → Metadata Service → Metadata DB
↓
Chunking Service → Blob Storage (S3/HDFS)
↓
Notification Service → Clients
VI
Client → Sync → Metadata Service → Metadata DB
Chunking file → Blob Storage
Noti → đồng bộ tới các thiết bị khác
3) File Chunking Strategy / Chia nhỏ file (Chunking)
EN
Large files must be split:
- fixed-size chunks (4MB / 8MB)
- variable-size chunks (Content-Defined Chunking / CDC)
Benefits:
✔ parallel upload
✔ deduplication
✔ resumable upload
VI
File lớn phải chia nhỏ:
- chunk cố định 4–8MB
- chunk biến thiên (CDC)
Lợi ích:
✔ upload song song
✔ dedupe
✔ resume dễ dàng
4) Metadata Service (the Brain) / Metadata Service (bộ não của hệ thống)
EN
Stores:
- file tree (folders)
- chunk list per file
- user permissions
- version history
Must be strongly consistent.
A typical schema:
FILES:
file_id
parent_id
name
version
size
owner
timestamps
FILE_CHUNKS:
file_id
chunk_hash
order
VI
Lưu:
- cây thư mục
- danh sách chunk
- quyền người dùng
- version file
Schema:
FILES(...)
FILE_CHUNKS(...)
Metadata cần strong consistency.
5) Blob Storage / Lưu trữ nội dung file
EN
Use highly scalable storage:
- S3-compatible
- HDFS
- Ceph
- GFS-inspired system
Store by hash:
/blobs/<hash>
VI
Dùng storage phân tán:
- S3
- HDFS
- Ceph
Lưu file theo hash:
/blobs/<hash>
6) Deduplication / Loại bỏ trùng lặp
EN
If two files share same chunks:
- upload once
- reference from metadata
VI
Nếu 2 file có chunk giống nhau:
- upload 1 lần
- metadata trỏ chung chunk đó
Giảm storage cực lớn.
7) Versioning / Quản lý phiên bản
EN
Version strategy:
- increment version per change
- store delta or full chunk list
VI
Tăng version mỗi khi client chỉnh sửa.
Có thể lưu delta hoặc full chunk list.
8) Sync Protocol (Dropbox model)
EN
Steps:
- Client computes file changes
- Hash chunks → detect changed chunks only
- Upload only changed parts
- Metadata service updates version
- Notifications sent to other devices
VI
- Client phân tích thay đổi
- Hash chunk → biết chunk nào đổi
- Upload phần thay đổi
- Metadata cập nhật version
- Noti đến device khác để sync
9) Conflict Handling / Xử lý conflict
EN
Conflict happens when:
- two devices modify same file offline
- both upload different versions
Solutions:
- automatic merge for text
- generate “conflicted copy”
- last-writer-wins (weak choice)
VI
Xung đột xảy ra khi:
- 2 thiết bị sửa file offline
- upload 2 phiên bản khác nhau
Giải pháp:
- merge text
- tạo “conflicted copy”
- last-writer-wins (không khuyến khích)
10) Sharing & Permissions / Chia sẻ & quyền truy cập
EN
- share by link token
- RBAC: read, write, owner
- per-file access control enforced at metadata layer
VI
- share qua link token
- quyền: đọc / sửa / chủ sở hữu
- check quyền tại metadata layer
11) Notification Service
EN
Notifies:
- other devices of file updates
- permission changes
- sync conflicts
Push via:
- WebSocket
- SSE
- APNS/FCM
VI
Thông báo:
- file thay đổi
- thay đổi quyền
- conflict
Dùng WebSocket/SSE/push.
12) Consistency Models
EN
Metadata → strong consistency
File blobs → eventual consistency OK
VI
Metadata: phải strong
Blob file: eventual là OK
13) Storage Replication & Durability
EN
Use:
- triple replication
- erasure coding (Reed-Solomon)
- multi-region redundancy
VI
Dùng:
- replicate 3 bản
- erasure coding
- multi-region backup
14) Observability
EN
Track:
- sync latency
- chunk upload failure
- backlog in notification queue
- blob retrieval latency
VI
Theo dõi:
- latency sync
- upload chunk lỗi
- backlog queue
- latency đọc blob
15) Typical Failure Scenarios
EN
- metadata DB unavailable → freeze writes
- chunk corrupted → restore replica
- client upload interrupted → resume
- conflict loops → resolve via merge
VI
- metadata hỏng → dừng ghi
- chunk hỏng → lấy replica
- upload gián đoạn → resume
- conflict nhiều lần → merge logic
16) Advanced Features
EN
- block-level dedupe
- client-side encryption
- selective sync
- offline mode
- delta sync (rsync-like)
VI
- dedupe ở block level
- mã hoá client-side
- selective sync
- offline mode
- delta sync giống rsync
[…] Designing A Distributed File Storage System (Dropbox / Google Drive / S3-like) […]