# How do you handle file uploads and serve them fast?

By Vishal Kumar · Published 2026-10-18 · Updated 2026-11-13
Source: https://fungyaan.com/p/design-file-uploads-cdn
Tags: system-design, uploads, s3, cdn, interview

**The verdict, which the flow below will earn:** your API should handle metadata and permissions, not bytes.
- **Upload directly to object storage** (S3, GCS, Azure Blob, R2, MinIO) using a **pre-signed URL** your API issues: valid for minutes, for one key, with a fixed content type and size (a pre-signed POST policy if you need a size range).
- **Process asynchronously** after upload: virus scan, validate the real file type, strip metadata, make thumbnails or transcode video.
- **Serve through a CDN** for public files; issue **short-lived signed URLs** for private ones.
- **Large files** use multipart (chunked) uploads that can resume after a dropped connection.

The case usually begins with a symptom that seems unrelated to files: API servers pinned at full memory, requests timing out, all because a few users are uploading large videos through the same process that serves everyone else. The obvious suspect is "we need bigger servers". The real fix is to stop carrying the bytes at all. I've always liked the pre-signed URL trick for exactly that reason. Your API hands out a tightly scoped permission slip, then steps aside while the client and storage deal with the heavy bytes.

> **Case file**
>
> | | |
> |---|---|
> | **The case** | API servers pinned at full memory while users upload large videos |
> | **Suspects** | "We need bigger servers" |
> | **Culprit** | File bytes passing through the app servers |
> | **The fix** | Pre-signed direct uploads, async processing, a CDN, signed URLs for private files |

## Through the app, or direct?

| | Through your app servers | Direct to object storage (pre-signed) |
|---|---|---|
| **App server load** | Every byte passes through: memory, bandwidth, long requests | Only a small "give me an upload URL" call |
| **Timeouts and size limits** | Tied to your load balancer and app limits | Storage handles multi-GB files and resumable uploads |
| **Validation** | Before storing | After storing (quarantine until checked) |
| **Complexity** | Simple for small files | Slightly more: issue URL, confirm completion |
| **Good for** | Small files (avatars, icons) when you want to check them first | Anything large or frequent |

The blogging platform I run takes the first route for blog icons: they're at most 256 KB, so the app reads them, checks the real image type and dimensions, refuses SVG (it can carry script), and stores them in the database. For anything larger, I'd go direct.

## Following one file, from request to CDN

Here's the whole journey of one file, from the client's first request to a CDN serving it.

```mermaid
sequenceDiagram
  participant C as Client
  participant API as Your API
  participant S3 as Object storage
  participant W as Processing worker
  participant CDN as CDN
  C->>API: POST /uploads {name, size, type}
  API->>API: check permission, quota, allowed type and size
  API-->>C: upload_id, pre-signed URL (5 min, exact key, size limit via a POST policy)
  C->>S3: upload the file to the pre-signed URL
  S3-->>W: "object created" event (quarantine bucket)
  W->>W: virus scan, sniff real type, strip EXIF, make variants
  W->>S3: write approved file and variants (public or private bucket)
  W->>API: mark upload ready (or rejected)
  C->>API: GET /files/123
  API-->>C: CDN URL (public) or signed URL (private, minutes)
  C->>CDN: GET image
```

Notice how each component does only the job it's good at; that division of labour is the elegant heart of the design. The clues worth underlining:
- **The API picks the object key** (for example `uploads/{tenant}/{uuid}`), never the client. User file names go into metadata, not paths.
- **Uploads land in a quarantine location** that nothing serves. Only the worker copies approved files to where they're served from.
- **Check the content, not the extension or the declared type.** Extensions and declared types are the classic false lead: they're whatever the uploader says they are. Read the first bytes to detect the real format, and decode images to check dimensions.
- **Confirm completion:** the upload record stays "pending" until the worker approves it; a cleanup job deletes pending uploads that never completed.

## Case 1: profile photos and product images

- Generate a few **fixed variants** (thumbnail, medium, large) in WebP or AVIF, plus the original format as a fallback.
- Strip **EXIF metadata**: photos from phones can carry the location where they were taken.
- Serve through a **CDN with long cache lifetimes** and versioned URLs (`…/avatar-v3.webp`), so a new photo is a new URL rather than a cache purge. You never have to invalidate a cache if you never reuse a name, which is one of my favourite small tricks in the whole field.

## Case 2: invoices, contracts and other private documents

- Keep them in a **private bucket**; the CDN or storage only serves them with a **signed URL valid for minutes**, issued after a permission check.
- Use `Content-Disposition: attachment` for documents users download, so a crafted HTML or SVG file can't run in your domain.
- Serve user content from a **separate domain** (`files.example-cdn.com`), never your main app's domain, so any script that slips through can't read your cookies (the [CSRF, CORS and SameSite post](/p/csrf-cors-samesite) explains why origins matter).
- **Encrypt at rest** (default in major object stores) and log access for audits.

## Case 3: large video

- **Multipart upload**: the client uploads 10-100 MB parts in parallel and can resume after a failure; storage assembles them.
- **Transcode** into several bitrates and package for adaptive streaming (HLS or DASH); the player switches quality as bandwidth changes.
- The CDN serves the small segment files; your origin rarely sees traffic.

## Following the money: limits and costs

- **Quotas** per user and tenant, checked when issuing upload URLs.
- **Lifecycle rules** move old files to cheaper storage classes and delete abandoned multipart uploads.
- **Egress** is often the biggest cost; a CDN with a high cache hit rate reduces it, and so does serving modern image formats.

> **Remember this**
> - Your API handles metadata and permissions, not bytes.
> - Quarantine every upload and check the real content, not the extension.
> - Never reuse a file name, and you never have to purge a cache.

## What to do on Monday

1. **Pre-signed direct uploads** for anything but small, checked-on-arrival files.
2. **Quarantine, scan and verify the real type** before anything is served.
3. **Separate domain and `Content-Disposition`** for user content.
4. **CDN with versioned URLs** for public files; **short-lived signed URLs** for private ones.
5. **Multipart uploads and transcoding** for video; lifecycle rules for cost.

What I enjoy about this design is how little your own servers end up doing, and how much safer and faster everything gets as a result. Sometimes the best thing an API can do is get out of the way, carefully. For why serving from a separate origin matters so much, the [CSRF, CORS and SameSite post](/p/csrf-cors-samesite) is the next case to read.
