How Large Uploads Work
easy 38 minA 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
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
- ...
- 72
- 73
- 74
- ...
- 100
Starts at, in MB
- 0
- ...
- 284
- 288
- 292
- ...
- 396
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
criticalAfter 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.
Wrongdef append(uid, data): file.write(data)Rightdef append(uid, offset, data): if offset == have: save(data) return haveThe client counts for itself
criticalThe client cannot know what was stored. One wrong guess makes a hole or a double.
Wrongoffset += len(piece)Rightoffset = reply["offset"]Moving the number before the bytes are safe
criticalA crash in between leaves a number that promises bytes which do not exist. The client never sends them again.
Wrongset_offset(292)save(piece)Rightsave(piece)set_offset(292, only_if=288)Saying done at 100% sent
majorThe last bytes may be in a buffer or a proxy, and the file is not joined yet.
Wrongif sent == total: show("Done")Rightif finish().ok: show("Done")Saving the upload ID at the end
majorIf the tab closes at 73%, the ID is lost, and 292 MB on the server can never be continued.
Wrongupload_all(file)save_note(uid)Rightsave_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.