How Large Uploads Work
easy 38 minA large file goes up in small numbered pieces.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
| You hear | Say | Because |
|---|---|---|
| Videos from phones | Pieces, upload ID, server's offset | Long transfer on a network that breaks |
| "What if the reply is lost?" | Same piece again, the server decides | Request and reply loss look the same |
| "The user closed the app" | Saved ID, fingerprint, ask for the offset | Memory is gone, the server's state is not |
| Very fast line, far away | Numbered parts in parallel | One connection may not fill it |
| Profile picture of 2 MB | One request | Sending it again costs little |
| Costs grow without new files | Expiry and clean-up of sessions | Unfinished parts are stored and paid for |
Word for word
Interviewer
The Wi-Fi drops at 72%. What does your client do?
You
It cannot know whether piece 73 was saved. So it waits a moment and sends piece 73 again with offset 288 MB.
Interviewer
And if the server had saved it?
You
Then its number is 292, the piece ends at 292, so it is already there. It writes nothing and answers 292. The client sends piece 74.
Interviewer
Why not just ask first?
You
That is also right, and it saves at most 4 MB. After a closed tab I always ask, because I have no number at all. After a short break, sending again saves a round trip if the piece was lost.
How does a 400 MB upload survive a broken connection?
BreaksOne request, retry from zero
- How
- POST the file. On an error, POST it again.
- Limit
- A break at 72% sends 288 MB again. A 2 GB file on a weak line may never finish. A retry after a lost reply may make two files.
- Choose when
- Small files.
WorksPieces, and the client remembers
- How
- Cut into pieces. The client stores the number of the last piece it sent and continues after it.
- Limit
- The client's memory is a guess. After a lost request there is a hole, and after a cleared browser there is no memory.
- Choose when
- Never by itself. Use it as a hint for the interface.
StrongPieces, and the server decides
- How
- Upload ID, offset and checksum with every piece. Save only at the expected offset, answer with the number, compare and set, atomic finish, expiry.
- Limit
- Server state, clean-up and a little more client code.
- Choose when
- Large files, phones, and wherever sending again hurts.