MongoDB, don't believe everything you read on the internet

Published on Jan 30, 2025 in Benchmarks  

You’ve probably already seen on the internet that it’s better to store JSON with MongoDB rather than in flat files for performance reasons? Well, that’s what I thought too.

A few years ago, when I was learning Golang, I wanted a simple JSON CRUD system (Create, Read, Update, Delete). I didn’t know MongoDB yet and I decided to build my own system with flat files, one file per document.

This is quite simple. Normally, the Linux file system is supposed to handle the concurrency itself. But for more security, I decided to use mutex for write operations on the same file.

A few years later, I learned MongoDB and rebuilt the same system. Since I’m a benchmark lover, I decided to compare the performance of the two systems.

I was really surprised by the result. Contrary to what everyone claims, my flat file-based system is twice as fast as MongoDB!

In the end, after a little thinking, it is rather logical. First, MongoDB uses 12-byte ObjectIDs and my ids are shorter. In addition, MongoDB uses the BSON format, which requires a conversion, while my system uses JSON directly.

But then, why use MongoDB?

Well, it’s not all about speed, and MongoDB offers other benefits.

With my flat file system, when I reach the limits of my system’s IOs, I get stuck. Even if I can gain performance with RAID 0, MongoDB allows better scaling options with its shard system.

Also, if using BSON slows down MongoDB for simple CRUD operations, this allows for better performance for aggregation queries. My system of flat files is really not made for that.

So, of course, there is an interest in using MongoDB. But it’s not about its performance for simple queries.

In conclusion, don’t believe everything you see on the internet, especially when it comes to performance. Particularly since what is true in certain circumstances is not necessarily true in your own use case. But also, many people share their beliefs without having any experience. So, do your own tests!