Tuesday, July 31, 2018

Storing Files in Server. File System or Database?

When me and my teammates were building our senior project, we needed to store files in the server. The easiest option for us was to keep the files in server file system and add a link to each file in the database. After graduation, I came across a new concept in database management which is the BLOB. According to MySQL documentation, BLOB stands for Binary Large Object. It is a datatype that is used to store a variable amount of data. Many database administrators use it to store resources files like images or small videos. The reason that I had to learn about the blob is that one of the requirements of developing a web application is to store files in the database rather than using file system.

After a while, one question popped in my mind. Which of the two is better to use? Storing files in server ‘s file system or storing them in the database? As usual, Googling is the best way to find answers for many questions. After searching and reading, I found that many database administrators store files in the database. And some uses file system. This post will go through the two approaches and gives the advantages and the disadvantages of using one of the two.

Storing Files in File System, The Advantages

Speed

If one cares about the speed, file system is the option to go with. The speed of serving multiple files using file system is faster according to many. Also, the speed of creating backups of the files will be much faster than creating a backup of the files while they are stored in the database.

Portability

Using file system to store files will help in portability. This means that if the application will migrate to use different database management system, it will be easy to move the files without any compatibility issues since the database will have only file reference. By storing files in database, the database management system might have an additional layer that wrap the file or alter its structure.

Storing Files in File System, The Disadvantages:

Backup and Recovery

If you are using file system to store files, you will have to create two backups, one for the files in the file system and one for the database. Same thing applies to recovering data. In addition to that, many database management systems come with a utility to create backups automatically. While in file systems, there are no such tool in many cases.

Reference Structure

Usually, files in the server must be accessed using some Kind of a reference if they stored in file system (Usually a URL). If a file or a directory is moved to somewhere else, the structure of the reference will change for sure. In this case, the reference to many files must be updated in the database which might take some time.

Storing Files in Database, The Advantages:

Integrity

One of the biggest advantages is data integrity. This means that any data needed by the application can be accessed from only one place. Also, this means that the information that is related to each other will be in one place. For example, if a company needs to store the pictures of its employees, they will be in the same record of employee information.

Easy to Manage

Since files are in the database, most likely that the management will be performed through the application that will use the files.

Storing Files in Database, The Disadvantages:

Performance

One issue of storing files in database is performance. This issue can be seen when the application needs to fetch many resources from the database. Also, it can be noticed while creating a backup of the database.

No FTP Access

Some prefer to use file system over database storage for this reason. They prefer to mange files using FTP clients instead of using application logic to add, update or remove files.

Configuration

If anyone plan to store very large files in database, sure he might struggle with modifying many settings in the server and the database management system. Myself, I had many issues in building one web application. The files that I was trying to store are video files of size 20 to 30 megabytes in MySQL server using Apache Server with PHP. The first issue was PHP’s upload limit. The second was the global variable max_allowed_packet. I had to update the settings of both till I was able to upload files.

My Opinion

Simply, there is no best option. It depends on your needs. If you thrive for speed and you have very large files to store, go with storing files in file system. For small files (Around 1 to 5 Megabytes), It would be better to go with storing files in a database. There might be other factors that can affect the final decision. So, before deciding, do your homework then select the option that suits your needs.

References:

Most of the information in this page are collected from the following websites. The answers are provided by many people who had different situations in storing files in a server. You can go and read more if you like.