ealeng

Episode 22 of 55 · Search and files

Large uploads: multipart and presigned URLs, and what a cut connection costs

A 2 GiB file, a connection cut at 20 random points: one upload of the whole file sent 21.14 GiB, resending 19.14. The same file in 64 MiB parts, with the same 20 cuts, resent 0.64 GiB and finished in 19 s instead of 144. Then the presigned URL: a link the server signs for one method, one file name and a short expiry, which the store checks and refuses when any of the three changes.

Lab 11 · Search, Geo and Object Storage8 min read
Video transcript8:24

0:00Cold open

0:01

I uploaded a two-gigabyte file over a connection that dropped twenty times: drops I made on purpose.

0:08

When it finally got through, I had sent twenty-one gigabytes. The whole file, more than ten times over.

0:16

The same file, with the same twenty drops, sent another way: two point six gigabytes.

0:24

So what’s the difference? And how does a browser upload a file like this without your storage keys?

0:32Intro

0:33

Hands-on system design, part twenty-two.

0:37

Today: uploading a big file over a bad connection, and letting a browser upload it without your keys.

0:44Meet Lucía

0:46

I’m Lucía, a platform engineer. Users upload big files to my service: videos, backups.

0:53

The files go in an object store: a place for big files, called objects. Each is stored under a file name, its key, and read and written over HTTP. Here it’s SeaweedFS, which speaks the same API as Amazon S3. It runs locally on my laptop, not in the cloud.

1:13

The database keeps only a small row about each file: its file name, its size, a checksum, a short fingerprint of the bytes, and how it was uploaded.

1:24

Think of mailing a long manuscript to a publisher. The object store is the publisher’s archive, and the database row is the index card for it. One upload of the whole file is one huge parcel.

1:3901 · What you need

1:40

To run it yourself: Python with a few packages, and Docker for the object store, Postgres, and a few other services.

1:49

The quick tests run in a couple of seconds, without Docker.

1:54

The upload test writes a two-gigabyte file to your disk. No cloud account, and nothing to pay for. Sizes here are binary gigabytes: one is about one point oh seven billion bytes, a little more than a regular gigabyte.

2:1102 · Cutting the connection

2:13

First, how I cut the connection. Not with a real bad network: the upload code itself stops sending at a chosen byte, and that request fails.

2:23

Twenty cut points, picked at random across the file. Both ways of uploading hit the same twenty byte positions.

2:32

One upload of the whole file means twenty tries that get cut, then one that gets through. And every try starts again from byte zero.

2:4203 · One big upload

2:44

One upload of the whole file: a two-gigabyte file, twenty cuts. How much did it send in total?

2:52

Twenty-one point one four gigabytes, in twenty-one requests. Nineteen point one four of it was sent again: nearly nine point six times the file.

3:04

Why? Each cut throws away everything sent so far: each red bar here. A random cut lands halfway through on average, at one gigabyte, so twenty cuts should waste about twenty gigabytes. These landed a bit early, point nine six gigabytes into the file on average: nineteen point one four.

3:24

In the manuscript picture: the parcel is lost on the way, so you send the whole manuscript again. It took a hundred and forty-four seconds.

3:3604 · In parts

3:37

The other way is a multipart upload. The file goes up in thirty-two numbered parts of sixty-four megabytes. Binary megabytes again: about sixty-seven million bytes each.

3:49

Each part gets its own signed upload URL, more on those in a minute, and its own PUT: the HTTP request that uploads a file. The single upload used one URL like that too.

4:04

If a part is cut, only that part is sent again.

4:08

At the end, one more request asks the store to join the parts into one object.

4:15

In the manuscript picture: chapters in numbered envelopes. A lost envelope means sending that chapter again, and the publisher binds them at the end.

4:2605 · In parts, measured

4:28

Same file, same twenty cuts. In parts, it sent two point six four gigabytes, in fifty-two part uploads: thirty-two parts, plus twenty sent again. And one request to start, and one to join.

4:42

It sent point six four gigabytes again: thirty-two percent of the file. Why? Now a cut wastes only what was already sent of the one part in flight. On average that’s half a part, thirty-two megabytes. Twenty cuts make six hundred and forty megabytes, about point six three gigabytes. These cuts landed a bit past the middle of their part, fifty-one percent on average, so: point six four.

5:06

The twenty cuts hit sixteen different parts. Four parts were cut twice, and sent three times.

5:14

It took nineteen seconds, against a hundred and forty-four: seven and a half times faster. Most likely because it sent eight times fewer bytes, but that’s one run, on a busy laptop. And here the parts went up one at a time; they could also go up in parallel.

5:32

Both stored files were downloaded again and checked: the same SHA-256 checksum as the original. Each got its row in the database.

5:4306 · A presigned URL

5:45

Last: who’s allowed to upload. You don’t want to give a browser your storage keys. And you don’t want two gigabytes passing through your own servers.

5:55

So the server signs a URL with its secret key, the private half of its storage keys: this method, this file name, until this time. The signature is a code only that secret key can produce. That’s a presigned URL. Here: PUT, the file avatar dot png, valid for sixty seconds.

6:16

A browser, here a script, uploads straight to the store with that URL, and never sees the key that signed it. In the manuscript picture: a prepaid return label for one shelf of the archive, with a use-by date.

6:3207 · What it allows

6:33

This is the shape of such a URL. It names the file and the expiry, sixty seconds, in plain text. The signature over them, and the credential, which names the key that signed it, are masked. I tried four requests. Two hundred means OK; four-oh-three means forbidden.

6:52

PUT to the signed file name: two hundred, OK.

6:57

The same signature on another file name: four-oh-three, forbidden. A GET, the request that downloads a file, with the PUT signature: forbidden too.

7:08

And a second URL, valid for one second, sent two seconds later: forbidden.

7:15

The signature covers the method, the file name and the expiry. So you can hand the URL to a browser: one object, for a short time. Each of the three other tries was refused.

7:29Back to Lucía

7:31

So, three lessons.

7:33

Big files go in an object store. The database keeps a small row about each one.

7:40

Upload big files in parts. A cut then costs part of one part, not everything sent so far.

7:48

And let clients upload with presigned URLs: one method, one file name, a short time.

7:56

Next time: much of what came before, IDs, caching, replicas, load balancing, in one service. A URL shortener, built, loaded to its target, and checked against the design written before measuring.

8:10

For today: send it in parts.Lucía: Send it in parts.

8:14Summary

8:15

Keep big files in an object store, upload them in parts, and sign each upload.

Key takeaways

  • ✓A single PUT of a 2 GiB file, with the connection cut at 20 random points, sent 21.14 GiB in 21 requests: each cut threw away everything sent so far, so 19.14 GiB was sent again, 9.6 times the file.
  • ✓Split into 32 parts of 64 MiB, the same file with the same 20 cuts sent 2.64 GiB and resent only 0.64 GiB, because a cut loses only what was sent of the one part in flight. It finished in 19 s, against 144 s for the single PUT, in one run on a busy laptop.
  • ✓A presigned URL is signed with the server's secret key for one method, one file name and an expiry time. The store accepted the PUT of the signed file name (200) and refused the same signature for another file name, for a GET, and after it expired (each 403).
  • ✓The database keeps a small metadata row per file, not the bytes: the lab's files table got one row per upload, and both stored objects downloaded back with the exact SHA-256 checksum of the original.
  • ✓The store checks signatures only when it is configured to. Without an identity in its settings, SeaweedFS accepts any request, so the presigned-URL checks mean something only with that file set up.

A 2 GiB file went over a connection cut 20 times, on purpose, and one upload of the whole file sent 21.14 GiB. The same file in 64 MiB parts, with the same 20 cuts, resent 0.64 GiB. This chapter takes apart the difference, then the other half of big uploads: a presigned URL, which lets a browser upload straight to storage without ever holding the storage keys.

The examples come from Lab 11 (Search, Geo and Object Storage) of the system design labs, its upload script 04_uploads.py, Exercises 1 and 2 (the lab numbers its exercises inside each script), and its recorded results in data/results/04_uploads.json. The store is SeaweedFS, an open-source object store that speaks Amazon's S3 API (the standard HTTP interface for object stores) and runs in Docker on one laptop, not in the cloud. The setup is the same Apple M4 Max with Docker Desktop (16 virtual CPUs) as the earlier chapters. Sizes are binary: 1 GiB is 1,024 × 1,024 × 1,024 bytes, a little more than a gigabyte, and 1 MiB is 1,024 × 1,024 bytes. The lab's run took place on a busy machine (its load average was 19.9 at the end), so read the shapes, not the last digit.

Why blobs don't go through the app server

Big files belong in an object store: a place that keeps each file, called an object, under a name, its key, and reads and writes it over HTTP. Two HTTP verbs matter here: PUT writes an object, and GET reads one. The database keeps only a small row about each object: its key, its size, a SHA-256 checksum (a fingerprint of the bytes, so you can tell whether a copy is the same file), its number of parts, and how it was uploaded. The lab's files table holds exactly that.

The reason is cost. A database that stores the bytes gets bloated backups and replication, and every download ties up a database connection and memory. An object store is built for large blobs and scales on its own. The app server stays out of the data path: it decides who may upload and what the file is called, and the bytes flow to the store.

One PUT of the whole file

The lab makes the connection cuts itself. The upload code stops sending at a chosen byte and the request fails, so the cut is simulated by the client, not a real bad network. The 20 cut points are random byte offsets into the file, drawn from a fixed seed (a starting value for the random numbers, so the run repeats): numpy's default_rng(1). Both ways of uploading meet the same 20 offsets.

A single PUT has no way to resume. Each cut ends the request, and the next try starts again from byte zero. So 20 cuts mean 20 failed tries and one that gets through: 21 requests in all.

Upload Requests GiB sent GiB resent Resent ÷ file Time
single PUT 21 21.14 19.14 957% 144 s

The arithmetic is simple. A cut at byte x resends the x bytes sent before it. The 20 cut points average 0.96 GiB into the file, not the half-file, 1 GiB, a random cut would average. Their sum is 19.14 GiB, which the run measured exactly. The lab's README predicts "20 × 1 GiB = 20 GiB" and measures 19.14, the same thing rounded.

In parts

A multipart upload splits the file into numbered parts. The lab uses 32 parts of 64 MiB, each about 67 million bytes. Each part gets its own presigned URL (below) and its own PUT. A request at the start creates the upload, and a request at the end asks the store to join the parts into one object.

If a part is cut, only that part is sent again. The lab's results:

Upload Requests GiB sent GiB resent Resent ÷ file Time
single PUT 21 21.14 19.14 957% 144 s
multipart, 64 MiB parts 52 2.64 0.64 32% 19 s

The 52 requests are 32 parts plus the 20 resends; the create and join requests are not counted. A cut costs what was sent of the part in flight, which on average is half a part, 32 MiB. Twenty cuts make 640 MiB, which is 0.625 GiB. The cuts landed a bit past the middle of their parts (51% on average), so the measured 0.64 GiB is close to that prediction. The 20 cuts hit 16 different parts; four of them were cut twice and sent three times.

The run took 19 s against 144 s, about 7.5 times faster. The likely cause is that the multipart run sent eight times fewer bytes (2.64 GiB against 21.14), but the lab didn't separate that from other effects. It is one run on a busy laptop, and the parts went up one at a time, not in parallel.

Both stored objects were downloaded back and matched the original's SHA-256, and each upload got its row in the files table.

Try it · Bytes sent for a 2 GiB file, cut one at a timeLab 11 · 04_uploads.py · simulation on the lab's 20 cut points

Cuts so far

20 of 20

Cut 20 lands 0.06 GiB into the file. A single PUT loses the 0.06 GiB sent so far. In parts, it lands 0.055 GiB into part 1, which is the only part resent.

One PUT of the whole file21.14 GiB sent
Multipart, 32 parts of 64 MiB2.64 GiB sent
the file, sent once sent again after a cut
Single PUT resent
19.14 GiB
Multipart resent
0.64 GiB

Resent as a share of the file: single 957%, multipart 32%. Recorded after 20 cuts: single 21.14 GiB sent (19.14 resent) in 144 s; multipart 2.64 GiB sent (0.64 resent) in 19 s.

Part size is the trade-off. Smaller parts waste less per cut but mean more requests. S3 allows parts of at least 5 MiB (except the last one) and at most 10,000 parts per upload, so a 2 GiB file could use parts from 5 MiB up. The lab tried one size, 64 MiB.

Presigned URLs

A browser should not hold your storage keys, and two gigabytes should not pass through your servers. So the server signs a URL with its secret key: this method, this file name, until this time. The signature is a code only that secret key can produce, and the client uses the URL as is. A presigned URL like that is how each part above was uploaded.

The lab's second exercise tests what the signature allows. It signed a PUT for a file named avatar.png, valid for 60 seconds, then tried four requests:

Request HTTP status
PUT to the signed file name 200 (OK)
PUT to another file name with the same signature 403 (forbidden)
GET (download) with the PUT signature 403
PUT after the URL expired (a separate URL, valid 1 s, sent 2 s later) 403

The signature covers the method, the file name and the expiry. So you can hand the URL to a browser, and it can upload that one object, for a short time, and nothing else. The lab's README adds the other half: a presigned URL means nothing if the store doesn't check signatures. SeaweedFS without a configured identity accepts any request. The lab's s3/s3.json, the store's settings file, adds one, and only then do these 403s appear.

A PUT URL can't limit the size of the upload or the kind of content, since the user chooses the bytes. The lab's README suggests a presigned POST (a form-style upload that the server signs with a policy, such as a size range and a content type) for that, and checking the content after the upload.

The workflow the lab ran

  1. Make a 2 GiB file with a known SHA-256 (a fixed-seed random file, so the run repeats).
  2. Upload it as one PUT, cut 20 times, then once through. Record the bytes sent and the time.
  3. Upload it as 32 parts of 64 MiB, each cut resent as its part, then join the parts. Record the same numbers.
  4. Download both objects, compare their SHA-256 with the original, and insert one files row for each.
  5. Run the presigned checks in the table above.

Caveats

  • One run, on a busy laptop. The 144 s and 19 s are single runs, and the 7.5× is one ratio of two of them. The bytes sent are the likely reason for the gap, but that was not isolated.
  • The cuts are simulated. The client stops sending on purpose. Real drops come with timeouts and half-finished packets, which this test does not produce.
  • Parts went up one at a time. The lab's README asks what 8 parallel parts would do to the bytes resent when a cut hits 8 in flight. That wasn't measured.
  • A cut resends the whole part. Resuming in the middle of a part would resend less. The lab didn't test that.
  • Not tested: the presigned POST policy, abandoned multipart uploads (their parts cost storage until the upload is completed or aborted, so a lifecycle rule should clean them up), and the real expiry of a presigned URL for a large upload. The big uploads used a one-hour URL; the checks used 60 seconds and 1 second.
  • SeaweedFS is not AWS S3. It speaks the same API, which is what the code relies on, but a managed store adds its own limits and behavior.

Check your understanding

Try each question before opening its answer.

Why did a single PUT of 2 GiB, cut 20 times, send 21.14 GiB?

A single PUT can't resume. Each cut threw away everything sent so far, and the next try started from byte zero. So each cut resent the bytes before it: the 20 cut points, averaging 0.96 GiB into the file, sum to 19.14 GiB resent, on top of the one full upload that got through.

What does a multipart upload lose to a cut, and why is that much less?

Only what was sent of the one part in flight. Each part is its own request, so a cut loses the bytes of that part sent before the cut, on average half a part, 32 MiB. Twenty cuts cost 0.64 GiB, or 32% of the file, against 957% for the single PUT.

What can the holder of a presigned PUT URL do with it, and what can't they do?

They can PUT the one file name the server signed, within the expiry, which the lab's store accepted (200). The same signature for another file name, a GET, and a PUT after expiry were each refused (403). The signature covers the method, the file name and the expiry, so the URL can't do anything else.

Why is the database holding only a row per file, and what does the row prove?

The bytes stay in the object store, which is built for large blobs, so the database isn't bloated or tied up by downloads. The row keeps the metadata: key, size, the SHA-256 checksum, parts and method. The lab's check downloaded both stored objects and compared their SHA-256 with the original; both matched.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.A single PUT of the 2 GiB file was cut 20 times and sent 21.14 GiB. Why was 19.14 GiB of it sent again?

  2. 2.The same 20 cuts hit a multipart upload of 64 MiB parts. Why did it resend only 0.64 GiB?

  3. 3.A presigned URL was made for a PUT of avatar.png, valid 60 seconds. Which request does the store accept?

Next up: Chapter 23, a URL shortener: built, loaded to its target, and checked against the design written before measuring. It puts the earlier chapters' IDs, caching, replicas and load balancing into one service.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.