AlgoViz

How Large Uploads Work

easy 38 min

A large file goes up in small numbered pieces.

See it run

Follow 400 MB in 100 pieces. The Wi-Fi dies during piece 73. Watch the server's one number.

Operations

The road of the bytes

From your disk to storage, and why the bar can say 100% too early.

Step 1 of 6

Choose a file

Our systemreadTLSpacketsbodysaveDisk400 MB videoBrowser memoryone pieceSend bufferemptyLoad balancerUpload serverObject storage72 pieces

The page gets a File object: name, size, last change. The video stays on the disk.

Step 1: Choose a file

file = choose_file()                 # no bytes read yetblob = file.slice(288_000_000, 292_000_000)send(tls(read(blob)))                # into the send bufferreply = wait_for_reply()             # the only proof
Sent twice
nothing
Server keeps
pieces and a number
Wait for
the server's reply

A progress event counts bytes that were sent. Only a reply says that they were saved.

In the real world

The same how large uploads work doing real jobs. Pick one and step through it.

Noor sends the video on a train. A tunnel comes during piece 73.

Step 1 of 4

Before the tunnel

Pieces (a few of the 100)

  1. 1
  2. ...
  3. 72
  4. 73
  5. 74
  6. ...
  7. 100

Starts at, in MB

  1. 0
  2. ...
  3. 284
  4. 288
  5. 292
  6. ...
  7. 396
server has = 288 MB

72 pieces are stored. Piece 73 leaves Noor's browser.

Step 1: Before the tunnel

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Adding a repeated piece at the end

    critical

    After a lost reply the piece comes twice. Without the offset check the file grows by 4 MB in the middle and everything after it is shifted.

    Wrong
    def append(uid, data):    file.write(data)
    Right
    def append(uid, offset, data):    if offset == have: save(data)    return have
  • The client counts for itself

    critical

    The client cannot know what was stored. One wrong guess makes a hole or a double.

    Wrong
    offset += len(piece)
    Right
    offset = reply["offset"]
  • Moving the number before the bytes are safe

    critical

    A crash in between leaves a number that promises bytes which do not exist. The client never sends them again.

    Wrong
    set_offset(292)save(piece)
    Right
    save(piece)set_offset(292, only_if=288)
  • Saying done at 100% sent

    major

    The last bytes may be in a buffer or a proxy, and the file is not joined yet.

    Wrong
    if sent == total: show("Done")
    Right
    if finish().ok: show("Done")
  • Saving the upload ID at the end

    major

    If the tab closes at 73%, the ID is lost, and 292 MB on the server can never be continued.

    Wrong
    upload_all(file)save_note(uid)
    Right
    save_note(uid)upload_all(file)

Interview tips

  • How to explain it

    • Cut, number, send, count.
    • The server keeps one number.
    • Silence: the same piece again.
    • The server's answer says where to go on.
  • Memory trick

    • Don't guess. Send it again, let the server decide.
    • Bookmark and book: saved ID and the same file.
    • Sent is not saved.
  • Always say

    • Store, count, reply.
    • Compare and set on the offset.
    • Checksum per piece and for the whole.
    • Atomic finish and an expiry.
  • Numbers to know

    • Piece 73 of 4 MB: 288 to 292 MB.
    • Dropbox: 150 MiB a request, 7 days a session.
    • S3: 5 MiB minimum part, 10,000 parts.
    • Google style: multiples of 256 KiB.