Friday, February 14, 2014

PostgreSQL or MongoDB?

In recent weeks has been revived the battle between MongoDB and PostgreSQL users in what is the best DB, and above all in the address that "MongoDB ends up being a bad choice and that it is best to never use it" and developers using MongoDB defend it saying that "PostgreSQL with a huge demand the performance is not optimal and MongoDB is made to have a highly performance and to be highly scalable, but what has happened is that you have not known how well use it".

And is that we, human beings, often see us involved in this situation that describes the history of india "Blind Men and an Elephant", here is the version of the poet John Godfrey Saxe, due to the inability of we have to know the totality of reality.

But who is right?

The answer to this question is all and none (I hope you have read the illustration above), for someone a personal subjective experience may be true, but this experience can enter into contradiction with the experiences of someone else or the whole truth at a broader level.

Ok, but leaving aside the phenomenological part of the subject, which then is best MongoDB or PostgreSQL?

This is not about which is better, is which fits best to the needs. You have to choose the correct tool, lets see this with more detail:

PostgreSQL is based on relational data storage model, being its main feature to maintain the integrity and consistency of the data. Using a normalized scheme minimizes the redesign of structure when you extend the data base and avoid anomalies during modifications in the database. The developer don´t worry about the integrity of the information since PostgreSQL is responsible for that but this have an impact on performance.

MongoDB is based on the non-relational storage model, the information is stored in documents and it is not necessary to follow a scheme. Its purpose is to provide great scalability and performance while maintaining most of the functionality of a RDBMS.

In Mongo there is no joins becuase is the operation that more impact has on the performance, for this you embed data, the information gets into the same document. If you need to have several collections of data the join must be implemented and it is the duty of the developer update all collections when there is a change.

There are no constraints or transaction, only atomic operations, so you get a great freedom to adjust the database to the application but is your responsibility maintain the integrity and consistency of the data. With this properties Mongo is really good to make ad-hoc querys and get a high performance.

If I need the features of both, can I use together?

Yes! you can use a master/slave replication, PostgreSQL as database master responsible for the integrity of the data and receive write operations and MongoDB as a slave so most of read operations are carried out there. To implement this configuration you can use tools like Bucardo replication.

Remember to choose the configuration database that best fits the needs of your application.