Library

Volume 1 · System Design Scenarios

Notebook
Sign in

Section 60

Large-Scale File Processing (5-Million-Row CSV with Async + Progress Notification)

519 words · about 2 min

Problem Statement

A user uploads a 5-million-row CSV in the UI, edits some data, and clicks Submit. The naive synchronous flow blocks on a multi-minute API call and shows a frozen spinner. Redesign for responsiveness.

The Architect’s Solution

1. Valet Key Pattern (direct-to-storage upload): The UI never routes a 5-million-row file through the API server. The backend issues a short-lived SAS token (Azure) / pre-signed URL (AWS) with PUT-only permission, valid ~10 minutes. The UI uploads the CSV directly to Blob Storage/S3, bypassing the API entirely. When the user edits data in the grid, only the small JSON diff payload is sent to the API — not the whole file.

2. Asynchronous processing (fire-and-forget with a job handle): On Submit, the API drops a message on a message broker (Service Bus/SQS) containing the blob URL and the diff payload, then immediately returns HTTP 202 Accepted with a JobId. The UI stops the spinner and shows a “Processing…” state — the request/response cycle is now sub-second regardless of file size.

3. Background processing with bulk-optimized DB writes: A background worker (Azure Function / AKS pod / Lambda) picks up the message, streams the CSV from blob storage (never loading all 5M rows into memory at once — use a streaming CSV reader), applies the diff, and writes to the database using SqlBulkCopy / EFCore.BulkExtensions / COPY in batches of ~50,000 rows — never row-by-row ORM inserts, which would take hours instead of minutes at this scale. Non-clustered indexes are disabled before the bulk insert and rebuilt afterward for maximum write throughput.

4. Real-time progress via WebSockets: While the worker batch-inserts, it publishes progress events (e.g., “1.2M / 5M rows processed”) that are pushed to the UI over SignalR/WebSockets, turning the frozen spinner into a live progress bar the user can trust.

UI --SAS token request--> API --issues short-lived PUT-only SAS--> UI
UI --uploads 5GB CSV directly--> Blob Storage        (bypasses API)
UI --submits small diff JSON--> API --202 Accepted + JobId--> UI (spinner stops)
API --message (blob URL + diff)--> Service Bus
Service Bus --> Background Worker
Worker: stream CSV -> apply diff -> SqlBulkCopy in 50K-row batches
Worker --progress events--> SignalR Hub --WebSocket push--> UI (progress bar)
Worker --job complete--> Cosmos DB (job status) + notification to user

Why Each Choice Matters

  • Why not chunked multipart upload through the API? Even chunked, routing 5GB through the API tier consumes API compute/memory/bandwidth for no benefit — direct-to-storage removes that tier from the critical path entirely.
  • Why 202 Accepted, not a long-lived synchronous HTTP call? Browsers, proxies, and load balancers commonly time out synchronous requests well under the multi-minute processing time; the async job pattern is the only approach that’s reliable at this scale.
  • Why batches of ~50,000 rows, not one giant bulk insert? Balances transaction log growth, lock duration, and the ability to checkpoint/resume on partial failure without redoing the entire 5 million rows.
  • Why disable and rebuild indexes? Every index update on every row insert multiplies write cost; for a one-time bulk load, it’s dramatically faster to insert unindexed data and rebuild the index once at the end.