Where do you store images for a web app?

Our CMS vendor stores images in the database. Not just the metadata or location information, but the whole binary image. I’ve never done this personally thinking it was a waste of processing power to query for the image, then retrieve it from the database when the web server should be able to serve it directly much faster.

Am I wrong? Is it just as fast to pull it out of a database (SQL-Server in this case). Logic keeps telling me no, but I could be wrong.



Don’t miss anything, subscribe!

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

From the performance point-of-view, storing and retrieving it from file system should be faster, but if integrity is a must-have and the successful storage of the image is necessary for the successful completion of the transaction, then it is a better idea to save it in the database.

Even storing the image in the file system might lead to poor performance, for example, when all the images are stored in the same directory and the file system uses a linear algorithm to scan contents of a directory (a few suchlike file systems exist.)

In summary, I believe it is easier and safer to store the image in the database.

Thanks Behrang, interesting. I wouldn’t have thought it would be ‘easier’ to store it in the database. Upload and write out the file seems pretty easy to me :-)

Transactions aren’t a big concern with the images in our case. If the image didn’t upload one the first try then trying again isn’t a big issue.

I hadn’t thought about where the files are stored to be honest. Every CMS (and most blog) software that I’ve used in the past has stored them in a structured way such as year/month/day or something similar.

We store all of our static content on Akamai and store the URLs in the database. Keeping your images out of the database allows for more options like front-end caches (squid), etc. If you must store images in a database, put them in local, embedded databases (preferably OODBs for binary files) on the webservers. In general, follow the rule of thumb to keep data as close to the application that needs it.

We’ve tried both mechanisms (via Oracle & PostgreSQL) and storing the image on the file system was the hands down winner. I would only consider storing an image in the database if you had an actual business requirement for storing the image in that fashion, otherwise your putting undue load on your database.

Consider an average database table with 10-20 columns, a mix of number/text types. Selecting a row of information out of that table might involve retrieving 1-5Kb of information from the database. Now imagine you’ve tacked on a binary column for your images and each image might vary between 1-2000kb.

Consider how many select statements per second you can issue to your database, when you’re retrieving a 1000kb image per row. You’ve guessed correctly, not many because you’re probably going to hit a 100Mbit network limit.

Horses for courses, your mileage may vary yadda yadda but if you’re looking for any sort of moderate performance; I believe you’ll find that storing the images on the file system is going to win every time - web servers are optimised to serve files remember!

Al.

Hi there,

We had an application where we were storing/retrieving from the DB. Business reason mostly (for auditing purposes, etc).

If we were to do the same app (without business reasons), I guess filesystem would be a better choices many times over (backup, caching, etc).

BR,
~A

I have to store/retrieve images from my webapp. Java webapps are recyclable because (in my case) they are build from a WAR file. Which is deleted in each update. I have done before a storage based on file system. Is not bad, but you have to worry about backup other stuff.

What if you do a simple file system cache ?

This is a very simple solution to avoid load in the DB.
Imagine a simple blog app with images. In the initialization of the webapp you can write down to disk in a cache the most popular images (related to the most popular blog posts) or just on demand. Is very easy keep track of blog hits to decide which image will go to this cache. IMHO, I’m a genius. hahaha

Leave a comment

(required)

(required)