Reputation: 8582
I know that in normal cases is faster to read/write from a file, but if I created a chat system: Would it be faster to write and read from a file or to insert/select data in a db and cahe results?
Upvotes: 11
Views: 9627
Reputation: 544
Do you really want a mechanical disk action every time someone types? Writing to disk is a horrible idea. Cache messages in memory. Clear the message once it is sent to all users in the room. The cache will stay small, most of the time empty. This is your best option if you don't need a history log.
But if you need a log....
If you write a large amount of data in 1 pass, I guarantee the file will smoke database insert performance. A bulk insert feature of the database may match the file, but it requires a file data source to begin with. You would need to queue up a lot of messages in memory, then periodically flush to the file.
For many small writes the gap will close and the database will pull ahead. Indexes will influence the insert speed. If thousands of users are inserting to a heavily indexed table you may have problems.
Do your own tests to prove what is faster. Simulate a realistic load, not a 1 user test.
Upvotes: 3
Reputation: 4352
Since I presume your system would write/read data continuously (as people type their messages), writing them to a file would take longer time because of the file handling procedure, i.e.
I would go with db.
Upvotes: 0
Reputation: 117681
Databases by far.
Databases are optimized for data storage which is constantly updated and changed as in your case. File storage is for long-term storage with few changes.
(even if files were faster I would still go with databases because it's easier to develop and maintain)
Upvotes: 0
Reputation: 6286
Database is faster. AND importantly for you, deals with concurrent access.
Upvotes: 8