Durable Uploads Service Design
Overview
This design outlines a scalable, fault-tolerant uploads service for large files (e.g., videos, datasets) with:
- Resumability: Chunked uploads with progress tracking; resume from any interruption.
- Checksums: End-to-end integrity via per-chunk and whole-file hashes (SHA-256).
- Durability: 99.999999999% (11 9's) via replicated object storage (e.g., AWS S3, Google Cloud Storage, or MinIO).
- Lifecycle: Automated policies for retention, tiering, and deletion.
Key Goals:
- Handle 1TB+ files.
- Support millions of concurrent uploads.
- Zero data loss on failures.
- Minimal client-side state.
Tech Stack (cloud-agnostic):
- API Gateway: REST/HTTP (e.g., AWS API Gateway, Kong).
- Backend: Go/Python (e.g., FastAPI) for orchestration.
- Metadata Store: DynamoDB/Cassandra (for upload sessions, low-latency).
- Object Storage: S3-compatible (chunks stored as prefixes).
- Queue: SQS/Kafka for async processing.
- Checksums: BLAKE3 or SHA-256 (fast, secure).
- Auth: JWT/OAuth + presigned URLs for direct-to-storage uploads.
Architecture (Text Diagram)
code
Client ──(HTTPS)──> API Gateway ──> Upload Orchestrator (Serverless: Lambda/ECF)
│
├──> Metadata DB (Upload Sessions: ID, chunks bitmap, metadata)
│
└──> Queue ──> Workers (Verify/Assemble)
│
└──> Object Storage (S3)
├── Temp: uploads/{uploadId}/{chunkIdx}
├── Final: files/{fileId}
└── Archive/Cold: lifecycle tiers
Lifecycle Manager (Cron/Event-Driven) ──> Storage Policies
Monitoring: Prometheus/Grafana + CloudWatch
Data Models
Upload Session (Metadata DB)
json
{
"uploadId": "uuid-v4",
"fileId": "derived-from-hash-or-uuid",
"userId": "auth-user",
"status": "INIT|IN_PROGRESS|COMPLETED|FAILED|EXPIRED",
"totalChunks": 1000, // fileSize / chunkSize (default 5MB-100MB)
"completedChunks": [bitmap or Set<chunkIdx>], // Bitarray for efficiency (e.g., roaring bitmap)
"fileMetadata": {
"name": "example.mp4",
"size": 1048576000, // bytes
"contentType": "video/mp4",
"checksum": "sha256:abc123...", // Client-provided whole-file hash
"chunkSize": 5242880 // 5MB
},
"createdAt": "timestamp",
"expiresAt": "timestamp + 7d" // Auto-cleanup
}
Chunk Object (S3 Key): uploads/{uploadId}/{chunkIdx:04d}.chunk
- Metadata:
chunk-checksum, total-checksum (for tree-hash verification).
API Endpoints
All endpoints require auth (Bearer JWT). Use presigned URLs for chunk uploads to offload traffic.
| Method | Endpoint | Description | Response |
|---|
POST | /uploads | Initiate: Provide file metadata + total checksum. | {uploadId, totalChunks, presignUrls?} |
GET | /uploads/{uploadId} | Query status: chunks completed, resume point. | Session JSON |
POST | /uploads/{uploadId}/{chunkIdx} (or presigned PUT to S3) | Upload chunk + chunk checksum in header. | 202 Accepted or 409 Conflict (duplicate) |
POST | /uploads/{uploadId}/complete | Finalize: Server verifies total checksum. | {fileId, downloadUrl} |
DELETE | /uploads/{uploadId} | Abort: Cleanup chunks. | 204 |
GET | /files/{fileId} | Download final file (signed URL). | Redirect/Signed URL |
Upload Flow
-
Initiate:
- Client computes:
- Whole-file SHA-256 (streamed).
- Per-chunk SHA-256.
- POST
/uploads → Get uploadId, expected totalChunks.
- (Optional) Query existing
uploadId for resume.
-
Chunk Upload (Resumable):
- Client uploads chunks in parallel (e.g., 5-10 concurrent).
- For each: PUT presigned URL with
x-chunk-checksum: sha256:....
- Server:
- Atomic upsert to bitmap in DB.
- Verify chunk checksum on ingest (S3 Event → Lambda verifies).
- If mismatch:
400 Bad Checksum.
- Idempotent: Skip if already present.
-
Resume:
- On reconnect: GET
/uploads/{uploadId} → Get bitmap of completed chunks.
- Client skips completed, retries missing.
-
Complete:
- Client: POST
/complete with proof (e.g., Merkle tree root).
- Server:
- Check bitmap == totalChunks.
- Async worker: Multipart copy/assemble to
files/{fileId} (S3-native).
- Verify whole-file checksum (download + hash or tree aggregation).
- Update status → Emit event (e.g., to webhook).
- Delete temp chunks.
-
Error Handling:
- Network fail: Retry chunk with exponential backoff.
- Server fail: Client resumes via query.
- Expiry: DB TTL → S3 lifecycle deletes orphans.
Checksum Verification
- Per-Chunk: Client → Header. Server verifies on PUT (prevents corruption).
- Whole-File:
- Client provides upfront.
- Server recomputes post-assembly (or uses S3 checksum algorithms like CRC32/E2E).
- Advanced: Merkle Tree – chunk hashes → tree hash → root matches client total.
- Libraries:
crypto (Node), hashlib (Python), blake3 (fast).
Durability Boost:
- S3: Automatic replication (multi-AZ/region).
- Chunk min-size: 5MB → Efficient replication.
- Quorum writes to DB.
Lifecycle Management
Event-driven (S3 Events + Cloud Scheduler):
| Stage | Trigger | Action | Storage Tier |
|---|
| Temp (upload) | On chunk | Store as-is | Standard (hot) |
| Active (post-complete) | complete event | Assemble → Final key | Standard/IA |
| Warm (>30d inactive) | Age-based | Copy to IA | Infrequent Access |
| Cold (>90d) | Age-based | Copy to Glacier | Archive |
| Delete (>1y or policy) | User/tag | Permanent delete | N/A |
- Implementation: S3 Lifecycle Policies (rules by prefix/tag:
retention:1y).
- Metadata: Tag objects with
uploadDate, userId, retentionPolicy.
- API:
/files/{fileId}/lifecycle (GET/POST policies).
- Compliance: GDPR/HIPAA – audit logs, hold flags.
Scalability & Ops
- Throughput: 10k uploads/sec → Sharded by
uploadId (DB partition key).
- Costs: Presigned → Direct to S3 (no proxy bandwidth).
- Monitoring:
- Metrics: Upload speed, resume rate, checksum fails.
- Alerts: >1% checksum mismatch.
- Edge Cases:
- 0-byte files: Special handling.
- Power-loss mid-chunk: Client re-hashes partial? (Use Tus.io protocol inspiration).
- Open-Source Inspo: Tus (resumable), S3 Multipart.
This design is production-ready; prototype in ~2 weeks with AWS CDK/Terraform. Questions?