Blob Storage
How to store large files in an object store with metadata in a database, how uploads and downloads work, and what interviewers ask about them.
On this page
- Where blob storage sits
- Object, file and block storage
- Metadata in the database, bytes in the object store
- Uploads and downloads with pre-signed URLs
- Large files: multipart and resumable uploads
- Durability: replication and erasure coding
- Serving downloads through a CDN
- Storage tiers and lifecycle rules
- Keeping metadata and bytes consistent
- Content hashing and deduplication
- Keeping files private
- In the interview
- What to practise next
An object store is a service that keeps large, unstructured files, such as photos, videos, documents and backups, as objects under keys, and that scales to far more data than a database or a single disk can hold. The standard design keeps the file bytes in the object store and keeps only a small metadata row about each file, such as its owner, size and object key, in the database.
If the bytes go into database rows instead, the database grows by every file that users upload. Each replica copies every byte, backups take longer, and every transfer holds an application server connection open. Databases are built for small rows that are queried and updated, while files are large and are written once and read whole.
Where blob storage sits
An object store exposes a simple HTTP interface. You PUT an object under a key such as photos/7f3a9c/original.jpg, GET it back, or DELETE it. There are no real folders, only key prefixes, and an object is replaced as a whole, never edited in place.
Object, file and block storage
Block storage
Block storage is a raw disk volume attached to one machine, which puts a file system or a database on top of it. It has the lowest latency and supports any read or write, but it attaches to one machine at a time and you provision its capacity yourself. It is used for the disk under a database, not for user uploads.
File storage
File storage is a shared network file system where several servers see the same folders and can rename, append and partially rewrite files. It suits existing software that needs ordinary file operations on shared data, but it is harder to scale and costs more per gigabyte than an object store.
Object storage
Object storage gives up renames, appends and partial writes in exchange for very large scale, a low cost per gigabyte, high durability and access over plain HTTP, which lets a CDN sit in front of it. It is the default for uploads, media, backups and exports.
Metadata in the database, bytes in the object store
The database row describes the file and points to the object. Queries such as "all files owned by this user" run against the database, which has indexes, and only the content itself comes from the object store.
id BIGINT PRIMARY KEY
owner_id BIGINT
object_key TEXT -- generated, for example "files/7f3a9c/original", never the user's filename
size_bytes BIGINT
status TEXT -- pending | ready | deleted
created_at TIMESTAMPThe server generates the object key. If it used the name the user typed, two users who both upload IMG_0001.jpg would overwrite each other.
Uploads and downloads with pre-signed URLs
If clients upload through your API servers, the servers relay every byte, and slow mobile connections hold server connections open for minutes. A pre-signed URL avoids this. It is a link to one object key, signed with the API's credentials, valid for one action and for a short time, so the client can send the bytes straight to the object store, which verifies the signature and expiry itself.
1The app asks the API for an upload slot and sends only the file size and type.
2The API checks the user's quota and inserts a
pendingrow with a generated object key.3The API returns a URL that is signed for one
PUTto that key and expires in 15 minutes. No file bytes pass through the API.4The app uploads straight to the object store. The store checks the signature and stores the object durably before it answers.
5The store emits an "object created" event. A worker makes thumbnails and sets the row to
ready, and only then does the photo appear in feeds.6A viewer downloads the photo through the CDN. The first request goes to the object store, and later requests are answered by the CDN.
7If another upload drops halfway, no object is created and the row stays
pending, so nobody sees a broken file. A cleanup job removes the row later.
For private files, the API checks that the user may see the file and returns a short-lived signed GET URL, either to the object store or to the CDN.
Limiting what a client can upload
Most stores let the signature fix the object key, the content type and a maximum size, and the store rejects a request that breaks them. A worker should still check the real size and type before it sets the row to ready.
Large files: multipart and resumable uploads
A single PUT is a poor fit for a multi-gigabyte file. Stores limit the size of one request, and if the connection drops at 90 percent the client has to start again from zero.
Multipart upload
The client asks the store to start a multipart upload and receives an upload id. It splits the file into parts, uploads each part with its own request and pre-signed URL, in parallel if it wants, and finally sends a "complete" request that lists the parts. The store assembles them into one object, which does not exist until that call succeeds. A failed part is retried alone, so a failure costs one part instead of the whole file.
Parts of an upload that was never completed are billed as storage, and nothing in your database points to them. A lifecycle rule that aborts incomplete multipart uploads after a few days removes them.
Resumable uploads
An upload is resumable when the client can continue after the app is closed or the network changes. The client keeps the upload id, asks the store which parts it already has, and sends only the missing ones.
Durability: replication and erasure coding
Replication
With replication the store keeps several complete copies of each object, typically three, in different failure domains such as separate racks or data centers. This uses three times the raw space. Reads are fast because any copy can serve them, and repairing a lost copy only needs a plain copy from a survivor. It suits small or frequently read objects.
Erasure coding
Erasure coding splits an object into data fragments and adds parity fragments computed from them. With 10 data and 4 parity fragments, any 10 of the 14 rebuild the object, so it survives the loss of any 4 fragments while using 1.4 times the raw space. The costs are that a read may touch many disks and rebuilding one fragment requires reading 10 others. It suits large, rarely read objects, and many stores replicate small or hot data and erasure-code the rest.
Durability means not losing data, which is different from availability, being able to read it now. To survive the loss of a whole region, stores can also copy objects asynchronously to a second region.
Serving downloads through a CDN
Every read of a popular file would otherwise go to the same object store. A CDN keeps copies near users, so most reads never reach the store.
A CDN copy stays until its TTL runs out, so a changed or deleted object can still be served. The simplest fix is to give every version its own key, for example by including the content hash in the key. Files that only some users may read need signed CDN URLs that expire.
Storage tiers and lifecycle rules
Object stores offer tiers that trade the cost of storing against the cost and delay of reading. The hot tier is the most expensive to store and cheap to read. Colder tiers cost less per gigabyte but charge for retrieval, and the coldest archive tiers can take minutes to hours before data is readable.
A lifecycle rule moves or deletes objects automatically based on age or prefix. A typical set moves uploads to a colder tier after 30 days and deletes temporary exports after a week. Regularly read files do not belong in a cold tier, because retrieval fees cost more than the storage saves.
Keeping metadata and bytes consistent
The database and the object store are separate systems, and no transaction covers both. Interviewers ask what happens when one step succeeds and the other does not.
An upload that fails halfway
The row is created as pending before the upload starts and becomes ready only after the object is confirmed to exist. If the upload fails, users never see the file because only ready rows are shown. A cleanup job deletes old pending rows and aborts unfinished multipart uploads.
An upload that succeeded but was never reported
If the "object created" event is lost, the row stays pending although the object exists. The cleanup job checks each old pending row and, if the object is there, marks it ready.
Orphaned objects
An orphan is an object that no row points to, caused by a failed cleanup or a bug. It costs storage. A periodic job compares the store's inventory with the database and deletes unreferenced objects older than a safety margin, so that it skips uploads still in progress.
Deleting files
First mark the row deleted, so the file disappears from the product immediately. Then delete the object and expire its CDN copies in a background job that can retry, with the orphan check as the final backup. With versioning enabled, a delete only adds a marker, and old versions keep taking space until a lifecycle rule removes them.
Content hashing and deduplication
A content hash such as SHA-256 is a fingerprint of a file's bytes, and two files with the same hash are the same file. It is used for integrity, because the store can reject an upload whose bytes do not match the hash the client sent, and for deduplication, where identical files are stored once and many rows point to the same object.
To deduplicate, the client sends the hash and the API checks whether an object with that hash exists. If it does, no upload is needed and the API only creates a new row. A shared object may be deleted only when no row points to it, so the system needs a reference count. If "this file already exists" were visible across users, an attacker could test whether someone else uploaded a specific file, so deduplicate within one account.
Keeping files private
The bucket is private, so the object store never serves a file to an anonymous request. The application decides who may read a file, and when it approves a request it issues a pre-signed or signed CDN URL.
Anyone who holds such a URL can use it until it expires, and it cannot be revoked early, so the expiry should be short, usually minutes. The API issues a new URL each time an authorised user opens the file.
In the interview
Use a single PUT for files up to a few tens of megabytes and multipart above that, because multipart adds requests and cleanup work that small files do not need. Use replication for small or frequently read objects and erasure coding for large, cold ones.
Bring in blob storage when the design involves images, video, documents or any file larger than a few hundred kilobytes. Say that the bytes go in an object store, the metadata in the database, and that clients upload and download with pre-signed URLs.
Questions to expect:
- "How do you upload a 5 GB file?" Use a multipart upload with a pre-signed URL for each part. The client uploads parts in parallel, retries failed parts on their own and finishes with a complete request. The metadata row stays
pendinguntil the object exists. - "What happens when an upload fails halfway?" Nothing visible, because the row is
pendingand no complete object exists. The client can resume, and a cleanup job removes stale rows and aborts unfinished uploads. - "How do you keep files private?" The bucket is private. The API checks permission and returns a short-lived signed URL for that one object, and the store refuses requests without a valid signature.
- "How does the store avoid losing data?" It keeps redundancy across failure domains, as full replicas or erasure-coded fragments. Replication is simpler and faster to read, and erasure coding needs much less space but costs more to repair.
- "How do you delete a file?" Mark the row deleted first, so the file disappears from the product at once. A background job then deletes the object, its derived files such as thumbnails, and the CDN copies, and it retries if a step fails. A periodic orphan check catches anything the job missed.
Users can upload videos up to 5 GB. How does an upload work?
The API inserts a pending row, starts a multipart upload in the object store and returns a pre-signed URL for each part. The client uploads parts of about 100 MB in parallel, retries any that fail, and then calls complete. The bytes never pass through our servers.
The user closes the app at 60 percent. What happens?
On reopening, the app asks which parts are already uploaded and sends only the rest. If the user never returns, a cleanup job aborts the multipart upload after a few days so we do not pay for orphaned parts.
An archive of rarely read backups must survive the loss of several disks and use as little raw space as possible. Which technique fits?
A client uploads a 2 GB file with a single PUT and the connection drops at 90 percent. What change fixes this?
What to practise next
Read CDNs for how cached copies are placed and invalidated, then Design Object Storage and Design Video Streaming.