# Db vs Filesystem for Binary contnet

**URL:** <https://www.ruby-forum.com/t/db-vs-filesystem-for-binary-contnet/70692>\
**Category:** Rails\
**Created:** [October 2, 2006, 2:05am UTC](https://www.ruby-forum.com/t/db-vs-filesystem-for-binary-contnet/70692 "2006-10-02T02:05:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [October 2, 2006, 2:05am UTC](https://www.ruby-forum.com/t/db-vs-filesystem-for-binary-contnet/70692/1 "2006-10-02T02:05:50Z")

</div>

Hi,

I’m debating between storing binary content (i.e. images) in the db  
(mysql) or the filesystem. I like the idea of the database because  
everything is in one place, but I’ve heard using the filesystem has  
better performance. Is storing binary content in the database a  
performance hit vs. the file system, or is it just an old wives  
tale? If I’m going to be writing queries against a table’s non-  
binary fields will I face performance hits because binary columns  
appear in the table?

Anyone have any thoughts?

Thanks in advance. Scott.

---

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [October 2, 2006, 2:44am UTC](https://www.ruby-forum.com/t/db-vs-filesystem-for-binary-contnet/70692/2 "2006-10-02T02:44:26Z")

</div>

Scott W. wrote:

> Hi,
> 
> I’m debating between storing binary content (i.e. images) in the db  
> (mysql) or the filesystem. I like the idea of the database because  
> everything is in one place, but I’ve heard using the filesystem has  
> better performance. Is storing binary content in the database a  
> performance hit vs. the file system, or is it just an old wives  
> tale? If I’m going to be writing queries against a table’s non-  
> binary fields will I face performance hits because binary columns  
> appear in the table?

Almost any curcumstance you will find the filesystem quicker for  
accessing binary files than a DB. Depending on how big your files are  
you can easily end up choking your DB.

I think this was a fad started by over ambitious DBAs that wanted every  
piece of content under their control.

---

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [October 2, 2006, 4:25am UTC](https://www.ruby-forum.com/t/db-vs-filesystem-for-binary-contnet/70692/3 "2006-10-02T04:25:55Z")

</div>

I hate managing backups of databases with lots of binary content.  
Resurrecting a damaged one is next to impossible. OTOH, everything  
really is  
in one place, which makes insert and delete more atomic. From a  
performance  
perspective, it will depend on how hard you expect your database to be  
hit.  
If the database is local, you shouldnÂ¹t suffer much performance. If the  
database is remote, the round-trip cost adds up in a hurry. If  
performance  
is your concern, measure a sample under your projected load on hardware  
similar to what you plan to deploy to.

Steve

---

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [October 2, 2006, 4:33am UTC](https://www.ruby-forum.com/t/db-vs-filesystem-for-binary-contnet/70692/4 "2006-10-02T04:33:03Z")

</div>

Yeah, dealing with dumps with a lot of binary data will lose its appeal  
very quickly.

Also, doing file operations – like imagemagick – on binary data just  
makes things so much more complicated.

Besides the weak “keeping everything in one place” argument, I can’t  
think of any pros of storing binary files in the db.

Joe

---

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [October 2, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/db-vs-filesystem-for-binary-contnet/70692/5 "2006-10-02T12:28:23Z")

</div>

unknown wrote:

> > Besides the weak “keeping everything in one place” argument, I can’t  
> > think of any pros of storing binary files in the db.
> 
> Well, an extension of that argument is that if your images are to be  
> dynamically resized on a regular basis or anything like that (so image  
> requests will have to be dynamic whether on the db or not), it makes  
> it so much easier to program.

HUH?!? Easier how? Dealing with image _files_ is stupid easy.

Joe

But I still think having images on the

> file system is well worth the extra time and effort in almost any  
> circumstance.
> 
> This is one of those subjects that comes up every 2-4 weeks.
> 
> -Nathan

---

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [October 2, 2006, 9:26am UTC](https://www.ruby-forum.com/t/db-vs-filesystem-for-binary-contnet/70692/6 "2006-10-02T09:26:21Z")

</div>

> Besides the weak “keeping everything in one place” argument, I can’t  
> think of any pros of storing binary files in the db.

Well, an extension of that argument is that if your images are to be  
dynamically resized on a regular basis or anything like that (so image  
requests will have to be dynamic whether on the db or not), it makes  
it so much easier to program. But I still think having images on the  
file system is well worth the extra time and effort in almost any  
circumstance.

This is one of those subjects that comes up every 2-4 weeks.

-Nathan

---

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [October 2, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/db-vs-filesystem-for-binary-contnet/70692/7 "2006-10-02T16:19:11Z")

</div>

I’ve done projects where I’ve stored images both ways. The first time, I  
stored images in the database. That’s an alright approach, because you  
can use caching to speed things up.

However, in a more recent project, I’m storing images in the file system  
along with any resized renditions of them. The thinking behind this is  
that I can serve image files statically without hitting Rails at all.  
More, since I’m using a very limited set of sizes, after a while, all  
the renditions I need will already be created statically. The tradeoff  
here is CPU time versus storage space. I think I’ll ALWAYS have more  
storage than CPU.
