Thursday 31 December 2015

Why you should NOT use MongoDB for your app.

Document databases like MongoDB are fantastic at storing JSON into a 'collection'. You can store any JSON Document you like and categorize them into any collection you want. JSON that is stored into MongoDB is called Binary JSON or BSON and they, like any JSON is schema-less. So unlike traditional SQL databases, You can store anything you like in any collection you like - Well, that freedom combined with MongoDB's ability to horizontally scale is just what excites developers to use it. That sounds awesome! Oh, but hold on...
Why not use it if it's "awesome"?
Database which you pick for your app depends on the type of the app you're building. You don't choose and marry the database, Your app does that instead. So here are few examples :
If you're building an app that has data which conceptually looks like "documents" then MongoDB is a better choice. For example, If you're building the blog engine Where each author has many blogs and each blog has many comments, and if you think your blog engine should scale to accommodate TONS of blogs then yes, MongoDB is a better choice.
MongoDB has no relations between documents and collections (You can have a Database Reference, but they're not enough).
Think of document as an actual "document", You have the piece of data which is not linked to any other data and there's no way you can do a join as effortlessly as you would do in SQL.
Well, If there are no relations and you can't join tables - Why on earth do people use it? Because its extremely scalable and has a low read/write latency when compared to SQL. For apps which has almost no relational data and needs scalability are the perfect fit of MongoDB. Most developers also use MongoDB to store related data and do a join manually in their code, which might work in certain scenarios which has a one level join / less relations between data but not for all.
Which database should I use?
There are tons of types of databases out there, and each database is for a specific set of requirements you have for your app. Few of them are :
Document Databases (For ex: MongoDB): Document databases are used to store JSON documents in collections and query with relevant fields. You can use this database to build apps which dont have too many relations between documents. Good example of those kind of apps are - Blog Engines / If you want to store a product catalogue.
Graph Databases (For ex: Neo4j): Graph databases are used for storing relations between entities with nodes being entities and edges being relationships. For example: If you're building a social network and if Person A follows Person B. Then Person A and Person B can be nodes and "follows" can be the edge between them. Graphs are excellent in doing joins which are even 100's of levels deep.
Cache (For ex: Redis): Cache are used when you need to access to your data super-fast. For example, if you're building an ecommerce application. You have product categories which kind of loads on every page load. Instead of hitting the database for every read operation (for every page load) which is expensive, you can store it in cache which is crazy fast for reads / writes. Cache like Redis should be a front-face of your database for frequently queried data. You can store it in the cache and not hit the database all the time.
Search Databases (For ex: ElasticSearch): If you want to do a full text search on your data (for ex: Products in an e-commerce app) then you need a search database like ElasticSearch which can help you perform search over huge volumes of data and give you a set of kick-ass features like facets.
Row Store (For ex: Cassandra): Cassandra is used for storing time-series data like analytics / logs / huge volumes of sensor data. If you have a type of use-case that does a ton of writes and less reads and is non-relational data then Cassandra is the db to take a look at.
Use two or more databases?
Combine databases
If you're building an app today, then there might be a need for using two or more databases at the same time. So, for example - If your app does search you might have to implement ElasticSearch, for non-relational data-storage - MongoDB works the best, and if you're building an IoT which has sensors pumping out a ton of data why not shoot it into Cassandra. Implementing multiple databases to build one app is called "Polyglot Persistence", I've written a blog about pros and cons of using multiple databases. Please do check that out and let me know if you find that interesting.

No comments:

Post a Comment

What should you required to learn machine learning

  To learn machine learning, you will need to acquire a combination of technical skills and domain knowledge. Here are some of the things yo...