Media and Uploads

Topics Covered

Upload Flows That Survive Real Phones

Why the naive upload endpoint fails

Direct to object storage with signed URLs

Resumable uploads for big files and bad networks

Decoupling the post from the upload

Validating what arrives

Quotas and limits

Putting it in the interview

Almost every product architecture prompt has media hiding in it. A photo sharing app is obviously about images, but so is a marketplace (listing photos), a messaging app (attachments and voice notes), a review site (photos of the dish), and an event ticketing site (venue maps and artist images). Interviewers like media because it forces you to think about the client, the network, storage, background work and delivery all at once, and because the obvious first design, a POST endpoint that accepts the file, breaks in ways that are easy to probe.

This lesson owns the media path end to end: how bytes get from a phone into storage, what happens to them afterwards, what people see while that happens, and how the finished media reaches viewers. Earlier lessons covered the API surface (l03) and the data model (l04) in general terms; here you apply both to the one kind of data that is too big to treat like a JSON field.

Why the naive upload endpoint fails

Picture the simplest version. The client sends POST /posts with a multipart body containing a 12 MB photo and a caption. The API server receives the whole file, writes it to object storage, inserts a row, and returns. It works on your laptop on office wifi. On a real phone it fails in four ways.

First, the bytes pass through your application servers, which are sized for small JSON requests. A few hundred concurrent uploads of large videos tie up connections, memory and bandwidth on machines that should be serving feeds. Second, the request is long, so it meets every timeout on the path: the load balancer idle timeout, the server request timeout, and the mobile operating system that suspends your app when the user switches to another one. Third, if the connection drops at 90 percent, the client starts again from zero. Fourth, the caption and the file are bundled, so a slow upload makes the whole post feel slow and you cannot let the user write the caption while the bytes travel.

None of these are exotic. They are the normal conditions of a phone on a train, and an interviewer who asks what happens when the user walks into a lift is testing whether you have seen them.

Direct to object storage with signed URLs

The standard fix is to separate the control path from the data path. The client first asks your API for permission to upload. The API checks who the user is, whether they are allowed to upload, what kind of file it is and how big it claims to be, then creates a media record in a pending state and returns a signed URL. That URL grants permission to write one object, at one key, for a few minutes, and nothing else. The client then sends the bytes straight to object storage using that URL. Your servers never see them.

The API hands out a short lived signed URL; the bytes go straight to object storage and never touch the app servers.
http
1POST /v1/media/uploads
2Content-Type: application/json
3
4{"kind": "photo", "content_type": "image/heic", "size_bytes": 12400000}
5
6201 Created
7{"media_id": "m_81", "upload_url": "https://store.example/raw/m_81?sig=...", "expires_in": 600}

When the object lands, storage emits an event (most object stores can notify a queue or a function on object creation), or the client calls a small confirm endpoint, or both. Using both is common: the client confirm gives you a fast path, and the storage event catches the case where the client died right after the upload finished. Whichever arrives first moves the media record from pending to uploaded and starts processing.

What the interviewer is checking here is not whether you know a particular cloud provider's API. It is whether you separate the cheap, authenticated control request from the expensive, dumb byte transfer, and whether you know that the signed URL is the authorization for that transfer. Mention that the URL is scoped to a single key, short lived, and limited in size and content type where the store supports it, so a leaked URL cannot be used to dump arbitrary files into your bucket.

Resumable uploads for big files and bad networks

For photos of a few megabytes, a single PUT with a retry is usually fine. For video, or for markets where many users are on slow and patchy networks, you want resumable uploads. The file is split into chunks, often between 5 and 16 MB. The client opens an upload session, sends chunks with their offsets, and the server records which ranges have arrived. When the connection drops, the client asks the session for its current offset and continues from there.

A resumable upload sends numbered chunks, so a dropped connection costs one chunk instead of the whole file.

Object stores offer this as multipart upload: you initiate, upload numbered parts in any order and in parallel, then complete with the list of parts. There are also open protocols for resumable HTTP uploads. In the interview you only need the shape: a session id, chunk offsets, a way to ask what has arrived, and a final complete call that is idempotent. Mention two housekeeping details that show experience. Incomplete multipart uploads still cost storage, so you set a lifecycle rule that aborts sessions older than a day or so. And upload sessions should survive the app being killed, which means the client stores the session id and offset on disk, and on iOS and Android you hand the transfer to the platform's background transfer service so it continues when the app is not in the foreground.

Decoupling the post from the upload

Once bytes travel separately, you can make the product feel faster. The client starts the upload the moment the user picks a photo, while they are still cropping, choosing a filter and typing a caption. By the time they tap Share, much of the file has often already arrived. The create post call then carries only the caption, the media ids and the audience:

json
{"media_ids": ["m_81", "m_82"], "caption": "first swim of the year", "audience": "followers", "client_request_id": "c7f2"}

The client_request_id makes the create idempotent, so a retry after a timeout does not create two posts. The server validates that every media id belongs to this user and is in an acceptable state. Whether a post can be created while its media is still processing is a product decision you should state out loud; most products allow it and show the post to its author immediately while holding it back from others until the media is ready, which section three covers.

Validating what arrives

The client claimed a content type and a size. Do not trust either. When processing starts, the worker reads the real file header to determine the actual format, rejects anything that is not an allowed image or video type, and checks the real size and dimensions against your limits. Decompression bombs (a tiny compressed file that expands to an enormous image) are a real attack, so decoders run with pixel limits and memory limits, ideally in an isolated sandbox. Files that fail validation move the media record to a rejected state with a reason the client can show, such as unsupported format or file too large.

Quotas and limits

Uploads cost you storage, processing and bandwidth forever, so products put limits on them. Common ones are a maximum file size per item (say 30 MB for photos and a few GB for long videos), a maximum duration for video, a maximum number of items per post, and per user rate limits on upload sessions per hour. Storage quotas per account matter for products like cloud drives and less for social apps, where the business pays for storage as a cost of engagement.

Enforce limits at the control request where you can, because rejecting before the upload saves the user's data plan. The upload API refuses a declared size over the limit, and the signed URL encodes a maximum length where the store supports it, so the client cannot lie about size and then send more. Count quota usage from the media records, not from the storage bill, and decrement it when media is deleted. When a user hits a limit, the product should tell them plainly what the limit is and what they can do, which is a requirements question worth raising in step one.

Putting it in the interview

In a real interview, the upload path deserves about three to five minutes unless the interviewer pushes deeper. Draw the client, the upload API, the object store and a processing queue. Say the four sentences that matter: bytes go directly to storage with a short lived signed URL; large files use resumable chunked uploads; the post is created separately and idempotently, referencing media ids; and the server validates the real file, not the client's claim. Then offer to go deeper on processing or delivery depending on what the product cares about. That offer is a navigation signal, and it leaves the interviewer free to steer.

Say control path and data path

When you draw uploads, label two arrows: a small authenticated request to your API that returns a signed URL, and a large transfer straight to object storage. Naming the split in one sentence shows the interviewer you know why the naive POST fails.