Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

What is considered the best practice for storing images in a database? How do twitter and Instagram store images?

Hi Guys,

I am building an app where users will be submitting a lot of photos. I would like to know what is the best way to store photos in the cloud? I have a mySQL database set up, should I store the photos directly in the database as a longblob? Or is it better to store the photos somewhere else and then keep only the URL for the photo in the database? Is one way faster than the other? Are there tradeoffs between the two options? How do the big boys at twitter and Instagram do it? I'm hoping this app will eventually have thousands of users so it most definitely needs to scale. Please let me know what you think. Thanks!

PS: Im using a LAMP stack on the backend, if that makes any difference.

1 Answer

Store the images somewhere, and store the images path, and meta data like alt tags, etc in the database. That way you can use the stored path to retrieve the image.

Of course these can eat server bandwidth fast, especially on smaller servers, which might have hard bandwidth caps, which is where looking at options like actually storing the images in an Amazon S3 bucket, and retrieving from Amazon's server instead of your own is ideal. You keep the images text details on your server, including it's url path to it's location in the S3 cloud server.

The big boys would do something like this, only at their scale there is probably a bit more too it, and they I'm sure have their own servers to store these assets at this point. Not that something like S3 couldn't be used on a big production site, but at some point there reaches a tipping point where it would be cheaper to hire your own sys admin to manage your own set of servers than to continue to pay to use someone else's servers. You are a long way from that, though, so plenty of room to grow.