Systemsfor Aravinda
0 / 78 core
Week 3 · Storage

Object storage

1.5 hours · core path

Read first: Partitioning and sharding

In one sitting

Object storage keeps immutable blobs and a metadata service that maps a key to where the bytes are. Overwrite is a new blob plus a pointer swing, not an edit in the middle of a file. Video bytes live there, and the database holds the small row that points at them.

A ten-minute video is hundreds of megabytes you almost never edit in place. The title, the owner, and the pointer are a row. Object storage exists so those two shapes do not share a page, a lock, or a backup strategy.

The metadata service is a map from a key to the place the bytes live. That map is ordinary keyed data, and you split it the way you split any other hot map. Partitioning and sharding is that split. The bytes themselves sit on a data plane that is cheap because it does not understand your schema. Readers who want a query go to a database. Readers who want the blob go to the key.

Core concepts

Two planes

The metadata service stores small records: the key, where the bytes are, a checksum, a size. A point read of metadata is a lookup. You partition it by key. A hot key is still a hot key. More partitions do not cool one object everyone wants, any more than they cool one database row.

The data plane stores the blobs. It is built to hold a lot of bytes and to stream them. It is the wrong place to ask for “every video this account uploaded last Tuesday” unless the metadata service can answer that from its own index. Listing is a metadata operation. It is not a query planner, and it is not free just because the bytes were cheap to keep.

A small object and a huge object share an API and not a budget. A same-datacenter round trip is 0.5 ms on the teaching list. Fetching a million tiny objects one by one, each paying that round trip before a single payload byte is interesting, is 500 seconds. One GET of a few hundred megabytes is limited by how fast the bytes move, and the metadata lookup is noise next to that transfer. Same store, opposite designs. Put the tiny structured fields in a database row. Put the blob you will fetch once in the object store.

Immutable blobs, and a pointer that swings

Objects are immutable. You do not open one, seek to the middle, and overwrite ten bytes. A change is a new blob. Overwrite of a key is usually a new object plus a pointer swing: the metadata entry for that key moves from the old blob to the new one. Readers see the old complete blob or the new complete blob. They do not see a torn mix of the two, because neither blob was edited in place.

The swing is atomic for one key. It is not a transaction across two keys. If the thumbnail and the video must change together, object storage will not hold that invariant for you. A database row that points at both, updated in one transaction, will. Transactions and isolation is the contract on that row. The blobs are just bytes the row names.

The safe order is boring and easy to get backward. Write the new blob and its checksum first. Then swing the pointer in the database, in a transaction that also stores the new checksum, and that fails if a newer version of the row already won. After that swing is durable, delete the old blob. Delete first, and a crash loses the only copy. Swing the row before the blob exists, and readers follow a key to nothing. Two writers who both upload and then both update the row will lose one update unless the row update checks a version. The blobs stay intact either way. The lost update is a metadata race, and the row is where you close it. The loser leaves an unreferenced blob. A sweeper can drop unreferenced blobs after a grace period long enough that a pointer still in flight is not deleted out from under a commit.

Not a filesystem

A POSIX filesystem gives you open, seek, a partial write, rename, and locks. Object storage gives you put, get, delete, and list. Treating it as a home directory is how designs grow bugs that look like corruption and are actually API mismatches.

You cannot patch the middle of a video. A “small edit” is a read of the whole blob, a local change, and a new put. For a few hundred megabytes that is the wrong tool. Keep the editable fields in the database. If you need a new encoding, write a new object and swing the pointer when the encode finishes.

You do not get a multi-key rename that commits or aborts as one action. Two swings are two updates. A crash between them leaves one key on the new blob and the other on the old. If that combination breaks the product, the pointer that matters is a single database row, not a pair of object keys you hoped would move together.

You do not get advisory file locks that keep two workers from processing the same blob. If two workers must not encode the same upload, the lock or the lease lives in a database or a coordinator. The object will not enforce it. Both workers can read the same bytes. Immutability makes that safe for the bytes and useless for the job claim.

Listing a prefix is not SELECT. A page that builds itself by listing a million keys and fetching each one will spend the 500 seconds above, and it will fall over when the prefix grows. Store the association you want to query as rows. Store the payload as one object per thing the user actually downloads.

Multipart upload and checksums

One PUT of a huge object is fragile. The blob becomes readable when the put completes. A dropped connection after most of the bytes have been sent leaves you nothing you can resume, because an incomplete put is not an object. You start over.

Multipart upload exists for that reason. You split the object into parts, upload the parts, and finish with a complete call. The metadata service assembles the part list into one object. A failed part is retried. The parts that already landed stay landed. If you never complete, the parts occupy the data plane and the key does not point at them. You cannot read them as the object. You abort the upload to release the space. “Cheap bytes” still cost you if you leak parts forever.

Checksums belong on the bytes. The client checksums the body it sent. That checksum is stored with the pointer, and a reader recomputes it over the bytes it got back. A mismatch means truncation, a bad disk, or a complete call that stitched the wrong part. A checksum of the metadata around the blob, and not of the blob, will happily bless a body that does not match. When the pointer swings, the checksum swings with it in the same database update. Pair a new blob with the previous checksum and a good upload looks corrupt. Pair an old blob with the new checksum and a bad one looks fine.

Complete is itself a metadata write. Check each part before you publish the key. Publishing a key whose parts do not verify is an object you have to swing away from, which is a second outage you already paid to avoid.

Video bytes versus database rows

Viewing history, account state, and permissions are rows. They are updated, they are joined, and they have invariants. The encoded title is immutable bytes. How Netflix streams splits those on purpose in the published design. Appliances close to the viewer store encoded media and serve it over HTTP. They do not store viewing history, DRM state, or member data. Those stay in the control plane.

In a system you draw, the same split is a database row plus an object. The row holds the owner, the title, the object key, and the checksum. The object holds the bytes. A CDN caches the bytes at the edge. The cache key is the URL, and media bytes are a good cache citizen where dynamic HTML is not. The origin should not be on the path of every play. The database should not be on the path of the bytes at all. A player that reads the video out of a database blob column drags a huge payload through the engine you needed for small transactional rows.

Do not invert the split to save a component. A database that stores the video inline will back up, replicate, and cache hundreds of megabytes to change a title string. An object store that stores the member row will not give you the transaction on “one account, one email.” Each store is bad at the other’s job.

A worked example

One title, one encoded rung. Assumptions, all labeled:

  • The rung is 5 megabits per second. An interview assumption for one compressed encoding, not a measurement of any provider.
  • The title is 10 minutes.
  • The database row that points at it is under 1 KB. An assumption for a key, a checksum, an owner, and a few fields.
  • The site serves 1,000,000 plays a day. A planning assumption.
  • One percent of plays miss the edge cache and read the origin. Also an assumption. A real miss rate is something you measure.
  • A million tiny objects, fetched one by one from the same region, is the contrast case. Each pays a 0.5 ms datacenter round trip.

A day has 86,400 seconds. The shortcut is about 100,000 seconds.

size of the title
  10 * 60 = 600 s
  600 * 5 megabits/s = 3,000 megabits
  3,000 / 8 = 375 MB

row versus blob, row assumed under 1 KB
  375 MB / 1 KB = 375,000 times smaller

plays
  1,000,000 / 86,400 = 11.6 plays/s
  shortcut: 1,000,000 / 100,000 = 10 plays/s

every play reads the origin
  11.6 * 375 MB/s = 4,350 MB/s
  1,000,000 * 375 MB = 375 TB a day

assumption, 1 percent miss the edge
  0.116 * 375 MB/s = 43.5 MB/s from origin
  0.01 * 375 TB = 3.75 TB a day from origin

metadata for those plays, 1 KB each
  11.6 * 1 KB/s = 11.6 KB/s

one million tiny objects, one round trip each
  1,000,000 * 0.5 ms = 500 s

multipart, a 4,000 MB upload in 100 MB parts
  4,000 / 100 = 40 parts
  a failed last part retries 100 MB, not 4,000 MB

The row is hundreds of thousands of times smaller than the blob. About 11.6 plays a second asking the database for that row is 11.6 KB a second. The same plays pulling the blob from the origin are 4,350 MB a second, and a million of them in a day are 375 TB, if nothing sits at the edge. One percent misses still leave 43.5 MB a second and 3.75 TB a day on the origin. That is why the bytes are cached near the viewer and why they are not a column in the transactional database. The database is bored. The data plane, uncached, is not.

The upload path is the other half of the same object. A single PUT of 4,000 MB that dies on the last 100 MB starts over, because the incomplete put never became a readable object. Forty parts of 100 MB mean you retry the part that failed. You still checksum the parts and the finished object. You still do not swing the database row until the complete succeeds. A row that points at an upload you never completed is a play button that fetches nothing.

Two encoders finishing at once are a pointer race, not a blob race. Each put a new immutable object. Each tries to swing the row. A transaction that updates the row only if the version is the one the encoder started from lets one swing commit. The other encoder retries, sees the winner, and deletes its spare blob or leaves it for the sweeper. Without the version check, the slower update wins and you can publish the worse encode last. The bytes of both encodes were fine. The bug was two writers on one pointer.

flowchart TD
  put[Put a new immutable blob]
  sum[Checksum stored with the pointer]
  swing[Swing the database row]
  serve[Readers follow the new pointer]
  later[Delete the old blob after the swing]
  put --> sum
  sum --> swing
  swing --> serve
  swing --> later

Read the arrows in that order. Reverse the last two and a crash deletes the blob a reader still needs. Skip the row and you have bytes nobody can name.

Tradeoffs

Choice What you gain What you spend
Blob in object storage, fields in a database row Big immutable bytes stay off the transactional engine Two systems, and the row must swing only after the blob is durable
Video bytes inside a database column One store, one transaction, one backup story The engine replicates and backs up hundreds of megabytes to edit a title
One PUT for a large object A single request and a simple client A failure near the end restarts the whole upload
Multipart upload You retry a part, and a finished object is still immutable Incomplete uploads leak parts until you abort them
Pointer lives only as the object key Overwrite is one metadata swing Two keys do not move atomically, and lost update needs a version somewhere
Pointer lives in a database row The swing can be a real transaction with a version check The play path reads the database before it knows which blob to fetch
CDN in front of the blob Origin traffic drops toward your miss rate A purge is eventual, so a replaced video can play the old bytes for a while

A purge being eventual is the CDN fact that bites replacement. You swung the pointer, and an edge that cached the old URL keeps serving it until the purge lands or the TTL ends. If the new encode must replace the old one immediately, the new blob needs a new URL and the row must hand that URL out. Cache the immutable URL. Do not cache a URL that means “whatever the latest encode is” and then expect a purge to be instant.

Pitfalls

  • Opening the object store as if it were a filesystem. The symptom is a worker that seeks, writes ten bytes in the middle, and finds that the API has no such operation, or a design that depended on a rename of many keys as one commit.
  • Swinging the database pointer before the blob is complete. The symptom is a play that fetches a key the data plane does not have.
  • Deleting the old blob before the new pointer is durable. The symptom is a crash that leaves the row aimed at bytes you already removed.
  • Two writers updating the pointer with no version check. The symptom is the slower encode published last, and a lost update you cannot see in the blobs because both blobs are intact.
  • No checksum on the bytes. The symptom is a truncated multipart complete that you serve for days, because the metadata size looked plausible.
  • Never aborting failed multipart uploads. The symptom is a data plane full of parts no key points at, which you still store.
  • A page that lists a prefix and fetches a thousand small objects. The symptom is half a second to tens of seconds of round trips for what should have been one indexed query and one blob.
  • Putting member state on the media cache. The symptom is an edge box that now holds data it was never supposed to, and a personalization bug you cannot fix by purging a video.

In the interview

In the first five minutes, split the bytes from the row. The object store holds the immutable blob. The database holds the owner, the key, and the checksum. Give the number: a 10-minute title at 5 megabits per second is 375 MB, and a million full pulls a day is 375 TB if the origin serves every play. The bottleneck is origin bandwidth, not the metadata lookup, which is why a CDN sits on the bytes. The honest tradeoff is two systems and a pointer you must swing in order, against stuffing the video through the database.

Skip multipart until the object is large enough that restarting a PUT hurts. Then say parts, retries, and a complete call that does not publish a bad checksum. Skip a vendor’s product name. The question that usually follows is how overwrite works, or how a 4 GB upload survives a dead connection. Overwrite is a new blob, then a pointer swing, then a delete of the old blob. The upload survives as parts you can retry, not as a partial object readers can see.

Check yourself

Why is replacing an object a new blob plus a pointer swing, rather than an edit in the middle of the old bytes?

The blobs are immutable, so nothing rewrites the middle of an existing object. The new bytes are a complete new blob. The metadata pointer for the key then moves from the old blob to the new one. A reader sees one finished object or the other, not a mix. The swing is the atomic step, and it is atomic for that one key.

A 4,000 MB upload in 100 MB parts fails during the last part. What did multipart already save, and what is still not readable?

Thirty-nine parts are already stored, so you retry about 100 MB instead of starting 4,000 MB over. A single PUT would have left no resumable object, because the blob appears when the put finishes. The key is still not readable: until the complete call publishes the part list, those parts are not the object. If you walk away without an abort, the parts sit on the data plane unnamed.

You need to change ten bytes in the middle of a 375 MB video. Why is the object store the wrong place to do that edit?

There is no partial overwrite. The edit is a read of all 375 MB, a local patch, and a new put of 375 MB, plus a pointer swing. Ten bytes of structured data belong in the database row, where an update rewrites a small record. A new encoding is a new blob you publish when it is finished, not a seek.

What do you put in the database row, and what do you put in the blob, for a video the user can play?

The row holds the owner, the title, the object key, and the checksum of the bytes. The blob holds the encoded video and nothing you need to query or to protect with an invariant about the account. The player reads the row to learn the key, then reads the bytes from the object store or from a cache in front of it. Viewing history is another row, not a file on the media cache.

Based on: Designing Data-Intensive Applications, Kleppmann and Riccomini; Netflix Open Connect.