Do not use MongoDB

Apr 18, 2022

Introduction

There are two situations in which you might want to understand if you should use a noSQL database: you are an engineer, or you are a managing one or more engineers.

In both cases: Don’t use a noSQL Database. If you’re interested in knowing why, keep reading, it’s quite simple.

Patterns

During my time as a Sales Engineer @ Fivetran, I saw a pattern surface that was quite astonishing to see in big and small companies – by far more painful in big ones: Teams using noSQL databases, had major problems to scale.

Strange huh? Who would’ve thought that the super-flexible do-whatever-you-want infrastructure would give us scaling problems. Well if you are only thinking about the model itself, it’s very flexible. If you’re thinking on integrating with a web app, and retrieving JSON objects on the fly, it’s very useful. But put in perspective, it’s very hard and costly to scale an infrastructure that is based on a denormalized model.

Normalization

JSON Structures and noSQL database are useful for specific, small cases, where maintenance is going to be minimal, dimensions can be huge, and access needs to be very, very fast.

Given these structures are denormalized

  1. Test and validation are hard and costly
  2. Code built on them is error prone
  3. Developer experience (DX) is not the best (e.g. they lack typing)

SQL

SQL based databases on the other hand solve the 3 problems listed above.

Since they are based on a structure with constraints, there is a myriad of tools out there that can help you test and validate.

Chances of making a mistake are less, given you can have type validation, structure mappings, and syntax checkers for basically any code you build on top of a SQL database (e.g. Prisma)

Needless to say, the DX improves by several orders of magnitud.

Conclusion

noSQL is not the devil, but looks very much like it. When it comes to making a decission, you should default to use a SQL database, and only ever use noSQL for small use cases with very simple structures and lots of data (event based systems, caches, etc).

Bear in mind that the two big benefits of noSQL over SQL are the lack of structure, and fast access.

If you don’t need a flexible structure, and you only need fast (not lightning ⚡ speed), just go with a SQL. In the long run, you will be very, very grateful.