Azure3bt/File-Uploader

โ˜… 0Forks 0C#GitHub โ†—Compare

README

๐Ÿš€ High-Performance Chunked File Upload (16MB + Parallel Upload)

A fully optimized high-throughput file upload system designed for very large files (5GBโ€“20GB). This project implements a chunked + parallel upload architecture with a highly efficient .NET backend and a browser-optimized JS client.

The latest version uses:

  • 16MB chunk size (balanced optimal)
  • 4โ€“6 parallel workers
  • Direct append-merge on disk
  • Zero buffering in memory
  • Async file streams with 1MB buffer
  • Full resume support
  • Maximized throughput without CPU/Disk contention

๐Ÿ”ฅ Why This Architecture?

Feature Before After
Chunk Size 2MB 16MB (x8 larger; fewer roundtrips)
Upload Strategy Single-threaded Parallel (4โ€“6 concurrent workers)
Backend Merge Read each chunk into memory Zero-copy streaming merge
Disk IO Pattern Many small writes Large sequential writes โ†’ MAX throughput
Resume Partial Full resume with real status
Throughput Slow ~4xโ€“10x faster depending on device

๐Ÿงฉ Architecture Overview

1. Client (JavaScript)

The upload process is sliced into 16MB chunks and sent with parallel workers for maximum throughput while preventing CPU and disk overload.

Key Decisions

  • 16MB chunk size proved the best balance between speed and overhead.

  • Workers = min(cpu/2, 6) ensures:

    • zero CPU exhaustion
    • zero browser throttling
    • optimal multi-threaded uploads

Final Client Configuration

const CHUNK_SIZE = 16 * 1024 * 1024; // 16MB optimal
const MAX_WORKERS = Math.min(Math.floor(navigator.hardwareConcurrency / 2), 6);

Upload Flow

  1. Initiate upload โ†’ server returns uploadId + totalChunks
  2. Worker pool sends chunks in parallel
  3. Server stores each chunk
  4. User may pause/close browser โ†’ resume supported
  5. After all chunks arrive โ†’ server merges them into final file

2. Backend (.NET 8 / .NET 9)

Optimizations

โœ” Sequential disk writes (fastest pattern on all OSes) โœ” 1MB buffer for efficient streaming โœ” Async I/O only โœ” CPU-free merge phase (no recompression, no buffering) โœ” Chunk resume tracking โœ” Folder auto-cleanup

Simplified High-Performance Merge

for (int i = 0; i < totalChunks; i++)
{
    var partFile = Path.Combine(folder, $"{uploadId}.part{i}");
    await using var partStream = new FileStream(partFile, FileMode.Open, FileAccess.Read, FileShare.Read, 1_048_576, true);
    await partStream.CopyToAsync(finalStream, 1_048_576, ct);
}

โšก Performance Results

File Size Old System New System
1GB ~90โ€“120 sec 25โ€“40 sec
7GB ~20โ€“30 min 6โ€“10 min
12GB failed or unstable Fully stable

Performance improvement: 4ร— to 10ร— faster.


๐Ÿ›  Features

  • โœ” Upload files up to 20GB+
  • โœ” Full resume support
  • โœ” Parallel uploads
  • โœ” Backpressure to avoid overload
  • โœ” High-speed disk merge
  • โœ” No memory spikes
  • โœ” Clean architecture
  • โœ” Production-ready

๐Ÿ“ฆ API Endpoints

POST /api/uploads/initiate

Start upload, returns uploadId + chunk count.

PUT /api/uploads/{id}/chunk/{index}

Upload a chunk.

GET /api/uploads/{id}/status

Returns received chunks.

POST /api/uploads/{id}/complete

Triggers final merge.


๐Ÿงช Local Test

Drop a file >5GB and run:

dotnet run

Open browser โ†’ upload UI โ†’ observe real-time progress.


๐Ÿ”ฎ Next Steps (Optional Enhancements)

  • GPU-accelerated hashing
  • Brotli/Deflate per-chunk compression
  • Multipleโ€node distributed upload shard system
  • S3/GCS/Azure Blob backend adapters

๐Ÿ‘จโ€๐Ÿ’ป Author

Mohammad Nazari โ€” Backend .NET Developer High-performance systems, architecture, DDD & scalable infrastructure.

Contributors

Azure3bt0x-mhmdnzri

Issues