6

We have a RoR 3.2 site on Heroku using their production tier PostgreSQL database.

Our site users occasionally upload images of 0.5 - 2MB in size. For different reasons we want to store those images in the database, rather than as files on S3 or similar.

If our site was image heavy, i.e. a Facebook clone, surely storing images in the db would not be a good thing.

Anyway, it is not, so here are some of the reasons for wanting to store a users images in the database:

  • Control over everything: S3 downtime and third party gems cannot break image handling/access to images.
  • Easy delete: If a user cancels his account, we can easily delete all associated images with that account through our rails models (relation dependencies).
  • Easier user data export: When a user wants to leave our site and take his data with him, we can more easily export his images rather than having to export from S3
  • Cost: Not a weighty argument, but since we're already paying for the database it will be cheaper than using S3 which costs (a small amount of) money.
  • Backups: Not a weighty argument, but a backup of our db would include everything associated with our site (except the RoR code, of course).

What is the easiest way to store images in the Heroku PostgreSQL database? Example code or link to a walk-through would be most welcome.

Thanks :)

rassom
  • 2,896
  • 5
  • 34
  • 45
  • It might help the answer to describe what different reasons you have for not using S3 - that would typically best practice in this example. – Jack P. Aug 25 '12 at 17:57

2 Answers2

9

I encourage you to not use your database to store images of that size.

S3 can be backed up very effectively, and is extremely hardened. From a data loss perspective, it's effectively safer then a development postgres database plan with Heroku.

The cost aspect is also not really best practice in this example. Although you're right they only enforce a row limit, I think it would be frowned upon to store large images (0.5 - 2mb) in the development database plan.

Finally, the speed of a transfer to S3 from EC2 (where Heroku is hosted) is very fast due to Amazon's internal networking.

Knowing that, I'd urge you to rethink your desire to store images of that size in a database. See this question for a more in depth analysis on the pros and cons.

There's also a dev center article about using S3 and Heroku.

Community
  • 1
  • 1
Jack P.
  • 727
  • 1
  • 7
  • 11
  • Thanks for your comments Jack. I did write that we're on a *production* tier database and not a development one, but I guess it wouldn't change your arguments much :) My most important argument is actually the not relying on yet another third party. The fewer points of failure, the better. When users upload an image to our site it IS essential and without the image(s) that user wouldn't be able to use the site much, so I'd rather have an all or nothing "policy", meaning that if S3 is down, our site is not accesible. Therefore, going for a db-store solution on Heroku makes best sense to me. – rassom Aug 26 '12 at 07:17
  • I think one of the main reasons for not using db and S3 instead is simply scalability. databases are usually expensive to use as bulk storage especially when the number of 1mb grows. – Kostia Sep 04 '12 at 18:53
  • As for the point of failure argument, it is only valid if you assume that your infrastructure is more reliable/can handle loads better than S3 (doubtful). Then using S3 becomes a asset, since it provides MORE instead of LESS stability. – Kostia Sep 04 '12 at 18:56
  • Thanks Kostia. I assume the Herokou infrastructure is as good as S3 (don't they actually use S3?). Also, there is 1TB of space on their db plans, so it is actually cheaper to use them (free until that - very high - limit) vs. S3 (although pretty cheap). Anyway, thanks for your views :) – rassom Sep 05 '12 at 10:16
4

As mentioned by others the better option would be storing images in S3 or something.

Still Check out following link for storing image in Heroku PostgreSQL database

Upload images in database

Community
  • 1
  • 1
Nidhi
  • 340
  • 2
  • 10