Storing Large Media Files: The "Reference-Only" Strategy
In this lesson, you will learn why storing large binary files directly inside your database can cripple performance and how to implement a reference-based storage strategy instead.
The Problem with Blobs
While your database (like PostgreSQL or MySQL) can store binary data using BLOB or BYTEA types, it is almost always a bad idea for large media files (images, videos, PDFs). Databases are designed to manage structured data, indexing, and complex relations. When you stuff massive binary objects into table rows, you cause several issues:
- Memory Bloat: Queries become slow because the database must load massive chunks of binary data into RAM just to fetch a few text fields.
- I/O Contention: You saturate your database disk I/O, slowing down every other operation in your system.
- Backup Complexity: Backing up your database becomes a nightmare when a few gigabytes of photos turn into a multi-terabyte backup file.
The Strategy: External Object Storage
Instead of saving the actual bytes in your database, you should treat your database as an index and your file system (or cloud storage like AWS S3) as the blob store.
When a user uploads a file, follow this flow:
- Save the file: Write the binary data to a dedicated file server or cloud bucket.
- Generate a reference: Create a unique URL or file path (e.g.,
/uploads/user-123/profile.jpg). - Save to DB: Store only that string reference in your database alongside your model data.
// Example of storing a reference, not the blob
async function saveUserAvatar(userId, fileBuffer) {
const filePath = `/storage/avatars/${userId}.jpg`;
// 1. Save binary to persistent file storage
await fs.promises.writeFile(filePath, fileBuffer);
// 2. Save only the path string to the DB
await db.users.update({
where: { id: userId },
data: { avatarUrl: filePath }
});
}
By decoupling the binary data, your database remains lean and performant, allowing it to handle complex queries without being weighed down by heavy media files. You can scale your file storage independently of your database, making your architecture more resilient.
Key Takeaway: Never store raw binary media files inside your database rows; store only the reference path or URL, and keep the actual files in specialized external object storage.