Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are flat file databases any good? [closed]

Informed options needed about the merits of flat file database. I'm considering using a flat file database scheme to manage data for a custom blog. It would be deployed on Linux OS variant and written in Java.

What are the possible negatives or positives regarding performance for reading and writing of both articles and comments?

Would article retrieval crap out because of it being a flat file rather than a RDBMS if it were to get slash-doted? (Wishful thinking)

I'm not against using a RDBMS, just asking the community their opinion on the viability of such a software architecture scheme.

Follow Up: In the case of this question I would see “Flat file == file system–based” For example each blog entry and its accompanying metadata would be in a single file. Making for many files organized by date structure of the file folders (blogs\testblog2\2008\12\01) == 12/01/2008

like image 981
Robert Nickens Avatar asked Dec 02 '08 02:12

Robert Nickens


People also ask

What is the problem with a flat-file database?

Disadvantages of flat-file database Flat file database is harder to update. Harder to change data format. It is poor database in terms of complex queries. It increased Redundancy and inconsistency.

Does flat file model still exist?

Although they provide relatively rudimentary means of storing, manipulating and accessing data, flat files are still widely used for a number of contemporary applications.

What is the best reason to use a flat file instead of a database?

They Require Fewer Hardware and Software Components. There's a good reason many operating systems, such as Windows, Macintosh, and Linux, use flat file databases. Though the databases can become bogged down with several thousand records, they are usually more efficient than other options.

Why are flat files not as good as relational databases?

Flat-file databases are simple and are essentially “free” but limit data access to manual processes and/or structured programs. Relational databases are generally more complex with varying costs but provide advanced capabilities and more efficient access options.


2 Answers

Flat file databases have their place and are quite workable for the right domain.

Mail servers and NNTP servers of the past really pushed the limits of how far you can really take these things (which is actually quite far -- files systems can have millions of files and directories).

Flat file DBs two biggest weaknesses are indexing and atomic updates, but if the domain is suitable these may not be an issue.

But you can, for example, with proper locking, do an "atomic" index update using basic file system commands, at least on Unix.

A simple case is having the indexing process running through the data to create the new index file under a temporary name. Then, when you are done, you simply rename (either the system call rename(2) or the shell mv command) the old file over the new file. Rename and mv are atomic operations on a Unix system (i.e. it either works or it doesn't and there's never a missing "in between state").

Same with creating new entries. Basically write the file fully to a temp file, then rename or mv it in to its final place. Then you never have an "intermediate" file in the "DB". Otherwise, you might have a race condition (such as a process reading a file that is still being written, and may get to the end before the writing process is complete -- ugly race condition).

If your primary indexing works well with directory names, then that works just fine. You can use a hashing scheme, for example, to create directories and subdirectories to locate new files.

Finding a file using the file name and directory structure is very fast as most filesystems today index their directories.

If you're putting a million files in a directory, there may well be tuning issues you'll want to look in to, but out of that box most will handle 10's of thousands easily. Just remember that if you need to SCAN the directory, there's going to be a lot of files to scan. Partitioning via directories helps prevent that.

But that all depends on your indexing and searching techniques.

Effectively, a stock off the shelf web server serving up static content is a large, flat file database, and the model works pretty good.

Finally, of course, you have the plethora of free Unix file system level tools at your disposal, but all them have issues with zillions of files (forking grep 1000000 times to find something in a file will have performance tradeoffs -- the overhead simply adds up).

If all of your files are on the same file system, then hard links also give you options (since they, too, are atomic) in terms of putting the same file in different places (basically for indexing).

For example, you could have a "today" directory, a "yesterday" directory, a "java" directory, and the actual message directory.

So, a post could be linked in the "today" directory, the "java" directory (because the post is tagged with "java", say), and in its final place (say /articles/2008/12/01/my_java_post.txt). Then, at midnight, you run two processes. The first one takes all files in the "today" directory, checks their create date to make sure they're not "today" (since the process can take several seconds and a new file might sneak in), and renames those files to "yesterday". Next, you do the same thing for the "yesterday" directory, only here you simply delete them if they're out of date.

Meanwhile, the file is still in the "java" and the ".../12/01" directory. Since you're using a Unix file system, and hard links, the "file" only exists once, these are all just pointers to the file. None of them are "the" file, they're all the same.

You can see that while each individual file move is atomic, the bulk is not. For example, while the "today" script is running, the "yesterday" directory can well contain files from both "yesterday" and "the day before" because the "yesterday" script had not yet run.

In a transactional DB, you would do that all at once.

But, simply, it is a tried and true method. Unix, in particular, works VERY well with that idiom, and the modern file systems can support it quite well as well.

like image 152
Will Hartung Avatar answered Oct 05 '22 23:10

Will Hartung


(answer copied and modified from here)

I would advise against using a flat file for anything besides read-only access, because then you'd have to deal with concurrency issues like making sure only one process is writing to the file at once. Instead, I recommend SQLite, a fully functional SQL database that's stored in a file. SQLite already has built-in concurrency, so you don't have to worry about things like file locking, and it's really fast for reads.

If, however, you are doing lots of database changes, it's best to do them all at once inside a transaction. This will only write the changes to the file once, as opposed to every time an change query is issued. This dramatically increases the speed of doing multiple changes.

When a change query is issued, whether it's inside a tranasction or not, the whole database is locked until that query finishes. This means that extremely large transactions could adversely affect the performance of other processes because they must wait for the transaction to finish before they can access the database. In practice, I haven't found this to be that noticeable, but it's always good practice to try to minimize the number of database modifying queries you issue, and it's certainly faster then trying to use a flat file.

like image 26
Kyle Cronin Avatar answered Oct 05 '22 23:10

Kyle Cronin