Whenever a developer wants to store images to be used by the application many ideas exist, one of them is storing images in the database, so why you MUST NOT go for that approach?
Databases are mostly optimized for fast data storing and retrieval, such thing makes the database expensive in terms of allowed space you get and resources consumption, so you need to be aware about what you are storing in your database.
Storing images in database usually is done by converting the image into base64 or to binary then inserting into the database.
then when a user asks for the image a query is made to the database to retrieve the required image then convert it back to the image to be served to the user.
Storing binary or base64 in the database to size increase (which is expensive as mentioned before) so when you want backup your database you will have large sized backup files and more time consumption for backup and restore process.
Beside increasing the database size, the previous 2 processes add more complexity to the code and also add more time for conversion and resource consumption.
Other thing to mention here is when if you are serving images for a web application this adds more time for user to download the image on the client to allow viewing, most browsers make a good use of caching to enhance the performance in case of storing images in the database it's hard for browsers to cache the images which lead to performance issues.
so to summary it up, problems are :
1. Performance
2. Increased Database Size
3. Backup and Recovery
4. Caching
Then what is the best approach to store your images?
in general it's better to store images itself on file storage system with your app (on server for example in web applications)
and store metadata about it in the database, this makes your database much lighter and fix all mentioned issues above.
there are some cloud services providing file storage solution can be useful like AWS S3 bucket or Cloudinary which gives you a lot of options related to image storing, retrieval and processing.
Top comments (1)
Totally Agree