Database versus files for Images
Update 2 January 2021: This was written in 2009 when virtual private servers were the rage, so I've updated it slightly to deal with our new cloud overlords.
Where should uploaded assets go?
A habit I once had was to store the uploaded content (including images) in a database. At the time the thinking was I want to manage:
- Application code
- Application data
Keeping all the data in a database made sense. This meant if I had to move a web app I just push code somewhere else and copy the database. Very simple.
Most frameworks, correctly assume you’ll upload files to some sort of file storage. I never fully understood this until I thought about how I’d speed things up when the time comes to speed things up. Django forces you to think that way.
File storage can be the file-system, but for building robust applications, this data belongs in an object store like Amazon's S3.
Almost from the start Django encourages you to:
- Upload any binary content to the filesystem with pointers in a database
- Have a separate server, or even machine serve static content.
Furthermore in a cached environment or even an environment that utilizes a CDN, putting static content in one spot, including user-generated content, was a big win.
I ported an app from symfony to Django, and I had been serving images via the database. Immediately when I switched to the filesystem I saw a huge benefit. Not just a drop in database connections, but there was an overall “zippiness” in the site.