· 5 min read

Understanding blob storage

When I was building RoomCraft AI, I needed somewhere to store all the AI-generated room images. Users upload a photo of their room, and the app generates 3 redesigned versions. That's a lot of images piling up fast.

I looked at Cloudinary, AWS S3 and Google Cloud Storage. Cloudinary seemed easy but got expensive at scale. S3 was the industry standard but egress fees add up quick when users keep viewing their generated designs. That's when I found Cloudflare R2.

R2 is S3-compatible (so all the same APIs work) and has zero egress fees. With S3, every time a user downloads an image, you pay. With R2, you don't. For an app like RoomCraft where users constantly view their generated designs, this saved me a ton.

The whole experience also taught me how much system design leans on blob storage, so these are my notes on it.

From my notes while learning about blob storage architecture

Big files don't belong in your database

Sometimes you need to store large files like videos, profile pictures or log files, and a traditional database isn't built for that. Storing large blobs (Binary Large Objects) in one is inefficient and expensive, so avoid it whenever you can and use a dedicated blob storage service like AWS S3, Azure Blob Storage or Cloudflare R2 instead.

Why I picked Cloudflare R2

The big one for RoomCraft was zero egress fees. S3 charges you every time someone downloads a file and R2 doesn't, which is huge for image-heavy apps. The API is S3-compatible, so it's a drop-in replacement and I didn't have to learn anything new. Images load fast globally because Cloudflare's edge network works as a built-in CDN. And the pricing is simple: you just pay for storage, so there are no surprise bills.

For side projects and startups watching their budget, R2 is a no-brainer. Blob storage in general is built to handle large amounts of unstructured data cheaply and with high availability.

Who uses blob storage

Pretty much everyone does. Netflix stores its entire video library in blob storage, Spotify keeps audio files there, and Slack stores every file you've ever uploaded. The pattern is the same: large files go in blob storage, and metadata (titles, timestamps, permissions) stays in a traditional database.

What blob storage gives you

Blob stores are very durable. Techniques like replication keep your data safe even if a data center fails. They also scale: services like S3 are virtually unlimited, and you can go from 1 GB to 1 PB without changing your architecture. Storing files here is a lot cheaper than storing them in a traditional database. Most platforms have built-in encryption and fine-grained access control through IAM policies. And the client can upload and download directly, bypassing your server, which saves you bandwidth and CPU.

When designing a system that uses blob storage, you shouldn't just "dump" everything there. Split the data: blob storage holds the actual large file, and your primary database holds the metadata (entity ID, name, description) plus a URL that points to the object in blob storage.

Upload and download flow

This is how the client, your app server, blob storage (S3) and the CDN talk to each other.

Click to expand

What the database stores

Each file gets an entity record in the database with these fields:

  • ID: unique identifier for the record.
  • Name: the display name of the file.
  • Desc: a brief description of the content.
  • BURL: the pointer/URL to the file in blob storage.

High-level workflow

The client uploads data and gets a URL back, then uses that URL to download the data directly. To speed things up, you can put a CDN (Content Delivery Network) in front to cache the data at "edge locations" closer to the user.

Uploads and downloads with presigned URLs

To keep the server from becoming a bottleneck, the client gets a presigned URL (a temporary, secure link) and talks to blob storage directly.

Uploading

  1. When the client wants to upload, it asks the server for a presigned URL.
  2. The server returns the URL and records the intent in the database.
  3. The client uploads the file straight to blob storage through the presigned URL.
  4. When the upload finishes, blob storage sends a notification to the server, which updates the database with the "actual URL".

Downloading

  1. The client requests a file and the server returns a presigned URL.
  2. The client uses that URL to download the file through a CDN, which proxies the request to the underlying blob storage.

Chunking and multipart uploads

With massive files, uploading the whole thing in one go is risky. If the connection drops at 99%, you have to start over.

Chunking fixes this by splitting the file into smaller pieces (e.g., 5MB each). Most S3-compatible platforms have a Multipart Upload API for it. If the user loses their internet connection, you only retry the chunks that weren't finished instead of the entire multi-gigabyte file. You can also upload several chunks at the same time, which speeds things up a lot and is essential for large video files.

The short version

Files go in blob storage and metadata goes in your database, with a URL pointing from one to the other. Don't use blob storage as a primary DB for structured data.


Used LLMs to correct grammar, typos etc

Comments

Loading comments
Leave a commentno sign in
0 / 1000