Where to store images
The other day I thought about the issue of storing images. There are two alternatives: in the file system and in the database. This is me, by the way, about images for web projects.
I read the literature. As it turned out, but was not particularly surprising, storing files as files has a number of advantages:
Storing files in a database has one definite plus - encryption. But are such precautions often needed for images? And it’s quite difficult to synchronize the file system with the database: deleted the record, you must delete the file. In the database, this is also solved in one turn by cascading deletion of the corresponding record with the BLOB.
In a number of issues, none of the approaches has obvious advantages. For example, backup can be done both for the database and for the file system.
The verdict is this. This is not to say that one of the approaches is more correct, because correctness is determined by the task. A simpler way is the standard way to store images as files. But for some tasks, databases are more suitable.
I read the literature. As it turned out, but was not particularly surprising, storing files as files has a number of advantages:
- Speed. Perhaps one of the most important advantages. The fact is that when storing in the database we need to perform the following actions to display the image: read the stream from the database, create a temporary file, write the stream to it. From the file system, it is enough to count it.
- Universality. Regular images can be used for many applications: send via ftp, add to email, show in browser. Binary data from the database must first be converted.
- Ease of implementation. Uploading a file to the server and saving the link to it in the table is easier than implementing the logic of saving binary data into the same table. And simplicity is the speed of development and change. Time is money.
- And if the base suddenly covers up (imagine that there were no backups), then at least the images will not be lost.
Storing files in a database has one definite plus - encryption. But are such precautions often needed for images? And it’s quite difficult to synchronize the file system with the database: deleted the record, you must delete the file. In the database, this is also solved in one turn by cascading deletion of the corresponding record with the BLOB.
In a number of issues, none of the approaches has obvious advantages. For example, backup can be done both for the database and for the file system.
The verdict is this. This is not to say that one of the approaches is more correct, because correctness is determined by the task. A simpler way is the standard way to store images as files. But for some tasks, databases are more suitable.