Ch.23: System Design: Resume a Failed File Upload

Outline

Transcript

0:00 Imagine a creator's 8-gigabyte video with its browser bar reading 95 percent. She sent the bytes for part 19, but her tab closes before a reply comes back. Must she upload the whole file again? The bar counted bytes leaving her laptop, not bytes storage accepted. She is checking out of a hotel; starting over could miss her deadline. Resume has to show what storage kept before she resends anything. The part can appear in storage after Resume calls it missing. Let's trace that on the map. Now follow 19's path: her browser asks our API for permission, sends numbered parts straight to storage, and later asks us to Finish.

0:39 Our session keeps her file fingerprint, fixed part plan, and upload ID. She reopens the tab. The common mistake is to treat the first storage list as a final answer. But that old request may still be running. What if it finishes after the list? We often trust the first inventory read after a reply goes missing; I think I'd have done that here too. But that read cannot settle an unfinished write. Our session has the plan and saved receipts; let's check storage again. And if 19 appears this time, did she send it again?

1:15 And the answer is no. Resume first sees no 19. Then the old request finishes, and the part appears without a new send. Oh, that's nasty. The first list said missing while the old send was still alive. What does she do now? Her Resume screen shows earlier parts green, 19 amber, 20 missing. Her plan is to pick the same cut, retry 19, send 20, then Finish. Accepted green slices stay untouched. But storage lists an ETag, the tag S3 returned for 19. Why is that part still amber? Because storage lists 19 but our service never saved its upload reply.

1:52 Amber means we still need a receipt before Finish; the accepted slices stay put. My first thought is to use the listed tag. The functional requirements say leave the green parts alone. Why pay hotel bandwidth to resend amber 19? It might work here. But S3 treats the list as verification, not a completion receipt. Finish needs a returned UploadPart tag, so the extra send buys one we can save. It doesn't freeze the slot; we still handle that race at Finish. On her hotel connection, that resend stings.

2:25 She picks the unchanged file and asks for fresh links. Our API crashes before it replies; no new part has left her laptop. Now she reloads. The API process died before issuing links, but the durable session still has her upload ID and earlier part receipts. We hold our breath after a crash until the screen comes back. Earlier parts are still green, 19 uncertain, 20 missing. Our non-functional requirements earned their keep. But our product designer asks, ‘She picked 8 gigabytes already. Same name, same size.

3:00 Why make her do it twice?’ But the shortcut can bite. A filename is not the file's identity. I want her account, one upload ID, fixed part ranges, and expected bytes bound to the session. What goes wrong if she uses the same session for a re-export? Imagine she fixes a title-card typo and exports a new cut under the same filename and byte count. If the browser sends those new parts into the original session, storage could assemble the wrong video: new opening, old ending. Ugh, and the thumbnail could still look right.

3:32 Her browser rereads the file and compares its SHA-256 fingerprint with the session before asking for links. That local read costs time; a revised cut needs a new session. With that same file selected, her old signed links have expired. A new request needs fresh permission. Would that expiry also have stopped 19's old request while it was in flight? No. S3 checks expiry when a request begins, so expiry cannot cancel a send already in flight. Future sends still need fresh permission. So the API confirms her session and signs new links for 19 and 20.

4:06 She can now send just those two slices; the earlier parts need no new transfer. With the new links, she resends 19 and sends 20. Does that second send for 19 put two copies into the video? No. S3 replaces the old part with the same number. The retry carries the original file's same slice; its signed link requires that slice's expected checksum. Ah, one slot, one piece. The browser posts each returned part number and ETag to our API; the session saves those upload receipts. But suppose another in-flight write replaces 19 after we save its retry tag?

4:42 Then Complete can reject the mismatched tag. We compare the saved receipt with storage, retry just that slot, and try Finish again. A successful Complete closes the upload ID; later writes cannot change the object. Now the parts have their returned tags. Are those tags enough to call the file Ready? No. Her laptop's SHA-256 confirmed the original cut, and S3 checked each part during upload. At Finish, S3 checks the assembled size and CRC32C, a whole-file transfer checksum. Only a match moves her to Processing.

5:12 And if Finish works but its reply to her tab disappears? Then her page stays Checking, not Upload Failed. The session entered Completing before the storage call. The service checks her session and private object key. It finds one object with the expected size and checksum. She sees Processing while a file-type check and malware scan run; only a passed check unlocks Ready. Ah, like 19: missing reply does not mean missing work. In a different attempt, suppose Complete has not succeeded and there is no object yet. What does she see?

5:45 Once that first Finish call settles without an object, she remains on Checking while the service retries with saved receipts. If recovery fails before expiry, it says Upload needs help, not Checking forever. We clean up that abandoned upload after old writes settle. Back on her successful path, S3 assembled one object; its private checks and scan pass. Now her page says Ready. She retried 19, sent 20 once, and made checkout without restarting 8 gigabytes. Support's runbook can name the exact part instead of sending her back to zero.

6:18 If her file is unchanged, Resume tells her what survived and what happens next. Thanks for listening to Learning Podcasts.