Categories
blob database image storage theory

Storing Images in DB – Yea or Nay?

415
votes

So I’m using an app that stores images heavily in the DB. What’s your outlook on this? I’m more of a type to store the location in the filesystem, than store it directly in the DB.

What do you think are the pros/cons?

1

140
votes

As with most issues, it’s not as simple as it sounds. There are cases where it would make sense to store the images in the database.

  • You are storing images that are
    changing dynamically, say invoices and you wanted
    to get an invoice as it was on 1 Jan
    2007?
  • The government wants you to maintain 6 years of history
  • Images stored in the database do not require a different backup strategy. Images stored on filesystem do
  • It is easier to control access to the images if they are in a database. Idle admins can access any folder on disk. It takes a really determined admin to go snooping in a database to extract the images

On the other hand there are problems associated

  • Require additional code to extract
    and stream the images
  • Latency may be
    slower than direct file access
  • Heavier load on the database server

6

  • 2

    Not having a seperate backup strategy can be a big deal when you are writing applications that are installed on premise (like SharePoint). When you create a SharePoint backup everything is in the DB which makes it very easy.

    Oct 2, 2008 at 23:40

  • 44

    Security by obscurity is not really an access control strategy!

    – Jon Cage

    Oct 9, 2008 at 10:46

  • 5

    I don’t think he’s advocating security by obscurity – he’s saying that putting images in the DB adds another layer of security. (I think… @Conrad, don’t want to put words in your mouth)

    – AJ.

    Oct 7, 2010 at 9:06

  • I chose storing images in the database because of the single backup advantage (or more generally speaking, having all data in one place), but the problems you mention are true as well, which is why I cache the images on the filesystem. It’s the best of both worlds, and I’m surprised none of the top answers here mention it.

    May 1, 2011 at 21:04

  • Are you, by chance, using the ImageResizing.Net library to handle your SQL->disk image caching? It’s the most advanced, scalable, and robust disk cache you can get…

    Aug 15, 2011 at 21:13

99
votes

File store. Facebook engineers had a great talk about it. One take away was to know the practical limit of files in a directory.

Needle in a Haystack: Efficient Storage of Billions of Photos

1

  • ext3’s dir_index helps a lot.

    May 5, 2011 at 8:41