Should web application store images in Database or File system?
We aggregate and tag open source projects. We have collections of more than one million projects. Check out the projects section.
As a web developer storing images in the file system would be the easiest and it is the best way. If the images are stored in the database then below are its disadvantages
- On request, the specified file has to be retrieved from database and temp file should be created, which would be referenced in the generated html. This is a time consuming task and it will delay the page load.
- In database, the image data would be stored as BLOB or IMAGE. If a particular record doesnot have any image then also it will consume certain space. To avoid this images should be stored in separate table and it should be accessed via foreign key.
- In shared hosting environment, where the database, disk space, web servers and other tools would be provided by the service provider, the database space would be very much limited and it is good option to utilize the file system. In case virtual private hosting this may not be an issue.
There are also some advantages to store the images in Database.
- If multiple servers are required for load balancing then if the images are available in database then it would be easy to take backup or replicate the data.
- Databases are helpful to keep multiple versions of the images.
- It provides some security as against storing all the image files in the file system.
What is the best solution? You need to pick based on your requirement. If it is a single server then I would recommend to store images in file system. If you scale then store the images in database but cache it in file system. For first time the images will be loaded from database and after that the images are loaded from file system. This would be difficult as to ensure both database and cache are in sync.
Please post your feedback.
To find embedded technology information about MCU, IoT, AI etc Check out embedkari.com.