AlgoViz

How Large Uploads Work

easy 38 min

A 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 hearSayBecause
Videos from phonesPieces, upload ID, server's offsetLong transfer on a network that breaks
"What if the reply is lost?"Same piece again, the server decidesRequest and reply loss look the same
"The user closed the app"Saved ID, fingerprint, ask for the offsetMemory is gone, the server's state is not
Very fast line, far awayNumbered parts in parallelOne connection may not fill it
Profile picture of 2 MBOne requestSending it again costs little
Costs grow without new filesExpiry and clean-up of sessionsUnfinished parts are stored and paid for

Word for word

  1. Interviewer

    The Wi-Fi drops at 72%. What does your client do?

  2. You

    It cannot know whether piece 73 was saved. So it waits a moment and sends piece 73 again with offset 288 MB.

  3. Interviewer

    And if the server had saved it?

  4. 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.

  5. Interviewer

    Why not just ask first?

  6. 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.