kurast
kurast

Reputation: 1615

Saving images: files or blobs?

When you save your images (supose you have lots of them) do you store then as blobs in your Database, or as files? Why?

Duplicate of: Storing Images in DB - Yea or Nay?

Upvotes: 27

Views: 67230

Answers (12)

ubi sage
ubi sage

Reputation: 93

Blob and file system are old now Why not go for S3 bucket to store images as in production we are using them,and storing the path of them in db

Upvotes: 0

raman bhadauria
raman bhadauria

Reputation: 155

I would suggest to go for File systems. First, let's discuss why not Blob? So to answer that, we need to think what advantages DB provides us over File system?

  1. Mutability: We can modify the data once stored. Not Applicable in case of images. Images are just a series of 1s and 0s. Whenever we changes an image, it wouldn't be a matter of few 1s and 0s altered and hence, modifying the same image content doesn't make sense. It's better to delete the old one, and store new.
  2. Indexing: We can create indexes for faster searching. But it doesn't apply on images as images are just 1s and 0s and we can't index that.

Then why File systems?

  1. Faster access: If we are storing images in Blob inside our DB, then a query to fetch the complete record (select *) will result in a very poor performance of the query as a lots and lots of data will be going to and from the DB. Instead if we just store the URL of images in DB and store images in a distributed file system (DFS), it will be much faster.
  2. Size limit: If DBs are storing images, a lot and lot of images then it might face performance issues and also, reach its memory limit (few DBs do have it).

Upvotes: 2

Kirti Chaturvedi
Kirti Chaturvedi

Reputation: 1325

Using file System is better as the basic feature you would be provided with while storing images as a blob would be 1. mutability which is not needed for an image as we won't be changing the binary data of images, we will be removing images as whole only 2. Indexed searching :which is not needed for image as the content of images can't be indexed and indexed searching searches the content of the BLOB.

Using file system is beneficial here because 1. its cheaper 2. Using CDN for fast access

hence one way forward could be to store the images as a file and provide its path in database

Upvotes: 1

chaos
chaos

Reputation: 124355

If I'm running on one web server and will only ever be running on one web server, I store them as files. If I'm running across multiple webheads, I put the reference instance of the image in a database BLOB and cache it as a file on the webheads.

Upvotes: 2

Sfynx
Sfynx

Reputation: 365

The performance hit of a database server is a moot issue. If you need the performance benefits of a file system, you simply cache it there on the first request. Subsequent requests can then be served directly from the file system by a direct link (which, in case of a web app, you could rewrite the HTML with before flushing the output buffer).

This provides the best of both worlds:

  • The authoritative store is the database, keeping transactional and referential integrity
  • You can deploy all user data by simply deploying the database
  • Emptying this cache (e.g. by adding a web server) would only cause a temporary performance hit while it is refilled automatically.

There is no need to constantly hammer the database for things that won't change all the time, but the important thing is that the user data is all there and not scattered around different places, making multi-server operation and deployment a total mess. I'm always advocating the "database as the user data store, unless" approach, because it is better architecturally, and not necessarily slower with effective caching.

Having said that, a good reason to use the file system as the authoritative store would be when you really need to use external independent tools for accessing it, e.g. SFTP and whatnot.

Upvotes: 6

APC
APC

Reputation: 146349

The question is, does your application handle BLOBS or other files like other application data? Do your users upload images alongside other data? If so, then you ought to store the BLOBs in the database. It makes it easier to back up the database and, in the event of a problem, to recover to a transactionally consistent state.

But if you mean images which are part of the application infratstructure rather than user data then probably the answer is, No.

Upvotes: 2

giorgian
giorgian

Reputation: 3825

I've tried to use the db (SQL Server and MySQL) to store medium (< 5mb) files, and what I got was tons of trouble.

1) Some DBs (SQL Server Express) have size limits;

2) Some DBs (MySQL) become mortally slow;

3) When you have to display a list of object, if you inadvertedly do SELECT * FROM table, tons of data will try to go up and down from the db, resulting in a deadly slow response or memory fail;

4) Some frontends (ruby ActiveRecord) have very big troubles handling blobs.

Just use files. Don't store them all in the same directory, use some technique to put them on several dirs (for instance, you could use last two chars of a GUID or last two digits of an int id) and then store the path on db.

Upvotes: 12

The Disintegrator
The Disintegrator

Reputation: 4187

By saving you mean to use them to show in a webpage or something like that? If it's the case, the better option will be to use files, if you use a database it will be constantly hammered with the request for photos. And it's a situation that doesn't scale too well.

Upvotes: 2

Mikey
Mikey

Reputation: 2837

Given that you might want to save an image along with a name, brief description, created date, created by, etc., you might find it better to save in a database. That way, everything is together. If you saved this same info and stored the image as a file, you would have to retrieve the whole "image object" from two places...and down the road, you might find yourself having syncing issues (some images not being found). Hopefully this makes sense.

Upvotes: 3

Chet
Chet

Reputation:

Blobs can be heavy on the db/scripts, why not just store paths. The only reason we've ever used blobs is if it needs to be merge replicated or super tight security for assets (as in cant pull image unless logged in or something)

Upvotes: 1

Sean
Sean

Reputation: 62532

It depends on the size of the image.

Microsoft Research has an interesting document on the subject

Upvotes: 15

Brandon Wood
Brandon Wood

Reputation: 5337

I usually go with storing them as files, and store the path in the database. To me, it's a much easier and more natural approach than pushing them into the database as blobs.

One argument for storing them in the database: much easier to do full backups, but that depends on your needs. If you need to be able to easily take full snapshots of your database (including the images), then storing them as blobs in the database is probably the way to go. Otherwise you have to pair your database backup with a file backup, and somehow try to associate the two, so that if you have to do a restore, you know which pair to restore.

Upvotes: 30

Related Questions