Chris Muench
Chris Muench

Reputation: 18318

Storing image in database vs file system (is this a valid use case?)

I have an application where every user gets there own database and runs from the same file system folder. (the database is determined by sub domain)

Storing in the filesystem could lead to conflict. I'd imagine the images upload would be small. (I would scale them down before storing)

Is it ok in this case to store in database?

(I know this has been asked a lot)

I also want to make my application easy to install and creating a writable folder is hard for some people)

Upvotes: 1

Views: 311

Answers (3)

Dustin
Dustin

Reputation: 764

Pasting my answer from a similar post: I have implemented both solutions (file system and database-persisted images) in previous projects. In my opinion, you should store images in your database. Here's why:

  1. File system storage is more complicated when your app servers are clustered. You have to have shared storage. Even if your current environment is not clustered, this makes it more difficult to scale up when you need to
  2. You should be using a CDN for your static content anyways, and set your app up as the origin. This means that your app will only be hit once for a given image, then it will be cached on the CDN. CloudFront is dirt cheap and simple to set up...there's no reason not to use it. Save your bandwidth for your dynamic content.
  3. It's much quicker (and thus cheaper) to develop database persisted images
  4. You get referential integrity with database persisted images. If you're storing images on the file system, you will inevitably have orphan files with no matching database records, or you'll have database records with broken file links. This WILL happen...it's just a matter of time. You'll have to write something to clean these up.

Anyways, my two cents.

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234795

To take the contrary view from Nathanial -- I find it easier to use the data base to store opaque data like images. When you back up the data base, you automatically get a backup of the images. Also, you can retrieve, update, or delete the image along with all the other data in integrated SQL queries; keeping the files separately means writing much more complex code that has to go out to the file system to maintain data integrity every time you issue certain SQL queries. Locking can be a big problem, and transaction processing (especially rollback) even bigger.

Upvotes: 2

Nathanial Woolls
Nathanial Woolls

Reputation: 5291

Seems like you've already sort of talked yourself into it, but in my experience it's better to store files in a filesystem and data in a database. Use GUID's for the file names if you are worried about a conflict.

Upvotes: 1

Related Questions