Sub topic 1
Start from Requirements: File Sizes, Types, Latency, and Trust
Prerequisites
This chapter assumes you know HTTP methods, headers, and status codes. It also assumes you understand a basic web architecture with a client, an API server, and a database, and that you have a general idea of what object storage such as Amazon S3 is. You do not need to have built an upload feature before.
Why requirements come first
Most upload bugs are design bugs. A team picks a multipart form endpoint because it is easy, then finds out that users upload 2 GB videos, that mobile connections drop, or that someone uploaded a script disguised as a photo. The fix is to write down four things before choosing any technology.
The four requirements
File sizes
Ask for the typical size and the maximum size. A 200 KB avatar and a 4 GB video need different designs. Large files are hard to send in a single request, because one dropped connection restarts the whole transfer. They also tie up server memory, bandwidth, and connections if they pass through your API.
File types
List the types you accept, such as JPEG, PNG, WebP, or MP4. Use an allowlist, which is a list of permitted values, not a blocklist of forbidden ones. The type decides what processing you need later, for example image resizing or video transcoding.
Latency
Latency is the time a user waits. Separate three delays:
- Upload time: how long until the bytes arrive.
- Ready time: how long until the file is processed and usable.
- Delivery time: how long a viewer waits when loading the file.
An avatar may need to show up within a second. A video can take minutes to process if you show a "processing" state.
Trust
Treat every uploaded file as untrusted. The filename, the declared Content-Type header, and the file extension are all values the client controls, so they can lie. Trust also covers who may upload, who may view, and what happens if a file is malicious.
From requirements to design choices
| Requirement | Question to ask | Design consequence |
|---|---|---|
| Size | What is the maximum? | Small files can go through your API. Large files need direct upload and chunking. |
| Type | Which types are allowed? | Validate against an allowlist and check content, not only the extension. |
| Latency | What must be instant? | Do slow work asynchronously and show a pending state. |
| Trust | Who uploads and who views? | Validate, scan, and serve from a separate domain with access control. |
A worked example
Suppose you build a photo-sharing app with these requirements:
- Profile photos up to 5 MiB, JPEG or PNG only.
- Video clips up to 2 GB over mobile networks.
- Photos must appear within two seconds. Videos may take several minutes to become playable.
- Anyone can sign up and upload. Some clips are private.
These lead to different choices. Photos are small, so a simple upload with strict validation works. Videos need resumable uploads, background transcoding, and signed access for private clips. The rest of this tutorial covers each of these pieces.
Exercise
A team accepts uploads up to 500 MB through their API server and sees memory spikes and dropped uploads on mobile. Which two requirements did they most likely underestimate?
Show answer
File size and network reliability (latency). Large files passing through the API server consume memory and connections, and a single dropped connection forces a full restart. Direct-to-storage and resumable uploads address both.