skip to content

When a wedding-photography app's 2 GB video upload drops at 80 %, why resume from the server's reported offset rather than its own byte count?

level: middleimportance: must knowfreq 53%

answer

  1. sent is not the same as stored
  2. ask first, then send the rest
  3. the server may hold less than you sent
  4. same bytes, or the upload fails
  5. a mismatch is an error, not a merge

basics

~20 s

Bytes the phone sent are not bytes the server stored: some were buffered or in flight when the link died. The client asks the server how much it holds, then sends from that offset, reading the same unchanged file.

solid answer

~50 s

The app's counter records what it handed to the network stack, but when the signal drops some of those bytes were still in buffers or in flight and never reached the server, which may also have dropped a partial piece. So on resume the client first asks the upload for its current offset, then sends from exactly that byte to the end. A server offset lower than the app's count is normal and accepted; one higher than anything the app sent means the upload is inconsistent and is cancelled and restarted. Resuming also needs the **same bytes**: if the clip was compressed for upload, the compressed file stays on disk until the server commits, because re-compressing need not reproduce it byte for byte. A send at the wrong offset is refused with `409 Conflict`, and the client asks again rather than guessing.

code

pseudocode · 19 lines
pseudocode
loop:
  status = ask_offset(entry.upload_url)              # offset query, sends no file bytes
  if status.complete:
    mark_committed(entry); stop
  if status.offset > entry.furthest_byte_sent:
    cancel_upload(entry); start_new_upload(entry); stop
  file = open_pinned_file(entry)
  if file.size != entry.bytes or file.modified_at != entry.modified_at:
    cancel_upload(entry); start_new_upload(entry); stop
  result = send(entry.upload_url, read_from(file, status.offset), offset = status.offset)
  if result.complete:
    mark_committed(entry); stop
  if result.conflict:                                 # wrong offset: ask again, never guess
    continue
  if result.url_expired and not entry.url_refreshed:  # re-sign once, never retry a stale url
    entry.upload_url = fetch_fresh_url(entry); entry.url_refreshed = true; continue
  if result.no_response or result.server_error:
    wait(backoff_with_jitter(entry.attempts)); continue
  mark_failed(entry, result.reason); stop

go deeper

for a junior

Recall the rule: ask the server how many bytes it holds, then send the rest of the file from there.

for a middle

Explain why sent and stored differ — buffers, dropped connections, partial pieces — and walk the loop: ask for the offset, check it, send, ask again after any interruption.

for a senior

Show the edge cases: an offset beyond what was sent, a conflict on append, a re-encoded file that no longer matches, and an upload the server already completed.

for a principal

Set the rule every media feature follows: resumable above a size threshold, source bytes pinned until commit, and the server's offset as the only truth about progress.

## Sent is not stored A photographer's 2 GB ceremony clip is 80 % uploaded when the venue's signal drops. The app has a counter that says about 1.6 GB went out. That number records what the app **handed to the network stack**, not what the **server has stored**: - bytes waiting in the phone's send buffers, or in flight across the radio, were lost with the connection; - an intermediary between phone and server may have accepted bytes it never forwarded; - the server may persist incoming data in pieces and drop a partial piece when the request breaks off. So the server's count is usually **equal to or lower than** the app's. A client that resumes from its own counter leaves a hole in the file — or, with a strict server, has every resumed request refused. The two resumable-upload designs worth knowing, an open protocol and a specification still being standardised as an IETF draft, agree on the cure: before sending anything, the client **asks the upload for its current offset** and continues from exactly that byte. ## The resume loop 1. **Ask.** Send a lightweight request (`HEAD` in both designs) to the upload's address. The response carries the offset and, in the draft, whether the upload is already complete. 2. **Check the answer.** - complete → mark the entry `committed`; the earlier final response was simply lost; - an offset at or below what the app has sent → seek the file to that byte; - an offset beyond anything the app ever sent → the upload is inconsistent; cancel it and start a new one from zero. 3. **Send the rest** from that offset, declaring the offset in the request so the server can check it. 4. **On any interruption**, go back to step 1. Never compute the next offset from "what I think went through". If a request declares the wrong offset, the server refuses it with **409 Conflict** rather than splicing bytes in the wrong place, and the client's reaction is to ask again, not to guess. A mismatch is an error to resolve, never a merge. ## The bytes must be the same bytes Resuming at byte N assumes that byte N onward is **identical** to what the first attempt was sending. Three client habits break that: - **Re-compressing on relaunch.** An encoder run twice on the same clip need not produce byte-identical output, so the resumed tail would belong to a different file than the stored head. Keep the compressed output on disk, pinned by the queue entry, until the server commits it. - **Editing the source.** If the photographer trims the clip mid-upload, the source no longer matches; detect it with the size and modification time recorded at enqueue and start a new upload. - **Reading from a temporary location** the OS may clear. Copy into the app's own storage first. The draft is blunt about the consequence: if the client cannot reproduce the same data from the offset, the upload has failed and must not be continued. The disk cost is real — a 2 GB original plus a compressed copy — and belongs in the storage budget the app shows. ## What the client stores to make this work | Stored with the queue entry | Used for | |---|---| | the upload's address | asking for the offset and appending | | the file reference, with its size, modification time and hash | proving the bytes have not changed | | the furthest byte the app has ever sent | spotting a server offset that is impossibly high | | the last offset the server reported | progress on screen only — never the resume point | | when the address stops being valid | refreshing it before use instead of failing on it | ## Where this sits beside the server's design How the server cuts an upload into parts, how large they are, which checksums prove the assembled object and when its metadata becomes committed are the server-side ingest subject. The client's half is narrower and easier to get wrong: **trust only the server's count, keep the exact bytes, and loop on ask-then-send**. A worked figure shows why it matters. Resuming the 2 GB clip at 80 % sends about 400 MB more; restarting sends 2 GB again — five times the data over a venue's weak signal, and five times the exposure to the next drop. On a 2 Mbit/s uplink (an assumption for illustration), counting 2 GB as 2,000 MB, that is roughly 27 minutes against 2 hours 13 minutes.

  • The server reports an offset larger than any byte the app ever sent; what should the client do?
    Treat the upload as broken. The server claims data the client never produced, so whatever it holds cannot be trusted. Stop, cancel that upload, and start a new one from zero. Skipping ahead to match would publish a corrupt clip that nobody notices until the couple play it.
  • Why might the app keep a compressed copy of the clip on disk until the upload finishes?
    Resuming from offset N works only if byte N onward is identical to what was sent before. Re-running the compressor after a relaunch need not produce the same bytes, so the server would join two different encodings. Keep the compressed output pinned by the queue entry until the server commits, and count that space in the app's storage budget.
  • Why ask for the offset after every interruption, not once per app launch?
    Every interruption makes the server's count uncertain again: a timeout, a network switch or a server error can each leave a different number of bytes stored. Asking costs one small request that sends no file bytes; guessing wrongly costs a refused request or a hole in the file.

Dictating a long letter over a crackling phone line: when the line comes back you ask the listener to read back the last word they wrote down, not the last word you remember saying.

  • The app's own count of bytes sent is an accurate resume point.
  • When the offsets disagree, resume from the larger of the two.
  • A 2 GB clip that drops at 80 % must restart from zero to avoid corruption.
  • Re-compressing the clip on relaunch yields the same bytes, so resuming is safe.
  • A conflict on resume means the file is corrupt and must be discarded.