Saturday 25 July 2015

MongoDB Basics

Introduction
In this blog post we will understand what is NoSQL databases and why we should use them for our application development.

NoSQL Database
A NoSQL (referred as "non SQL") database provides a mechanism for storage and retrieval of data in form of structure which is other than the tabular relations used in traditional relational databases.

Unlike a RDBMS(Relational database management system) , NoSQL database(Non relational database) structure stores data in form of key/value pair, document and graphs etc.

Why NoSQL Database over RDBMS
When we are working in a culture to use relational database to store our data in form of tables with relationship maintained among them, then why we should use a non relational database or what is the need to use NoSQL Dbs as backend providers for our applications. Here is the answer:
  • NoSQL databases solve scalability and performance issues that relational databases weren’t designed to address
  • NoSQL is especially relevant when data is unstructured, unpredictable or needs to be massively processed in real time. For example:  "sensor based applications".
  • As a database grows in size or the number of users multiplies, many RDBMS-based sites suffer serious performance issues.

MongoDB
There are several NoSQL database structures are available, but MongoDB is one of the leading NoSQL database among them.
MongoDB is a open-source document database that provides high performance, high availability, and easy/automatic scaling.

Document Database
A record in MongoDB is a document(object), which is a data structure composed of field and value pairs and map easily to programming language data types. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents which reduce need for joins.
{
product: "Maggi",
price: "78.0",
category: ["Food","Noodles"]
}

Key Features
There  are several key features provided by MongoDB which makes it best choice among other NoSQL Dbs for application development.

MongoDB supports almost all the languages and there are lists of drivers available for MongoDB and it open sourced at Github at following location:

There are several reason why people should choose MongoDB over other relations databases:

High Performance:
  • MongoDB stores data in JSON format so that performance is very fast.
  • It supports "Indexing" , which results in faster querying to database and can include keys from embedded documents and array.

Easy/Automatic Scalability:
  • It is easy to install, configure, maintain and use.
  • In RDBMS we need special database administrators to maintain data in database, while in MongoDB its very  to manage and with very few command it will provide all the functionalities.
  • MognoDB supports automatic sharding and you can use shard data at multiple locations.
  • It is possible to increase capacity without any downtime, which is very important on the web development when load can increase suddenly and bring down the website for maintenance can cost very high for business.

Flexibility:
  • We can store data in a way we want there is no restriction on database schema(unlike RDBMS). 
  • It works on any operating system so we can use it on any platform that our application requires.
  • It supports all languages.

Replication:
MongoDB also supports replication just like relational databases. It supports replication with replica sets. Each replica set contains two or more copies of the data. There are two types of replica set:

Primary Replica
Between two set of replicas, one replica act as Primary replica which performs read and write operation.

Secondary Replica
Other replica act as secondary replica set which create copy of data on top of Primary replica set.
Main goal of secondary replica set is, if primary replica set fails it start acting as primary and performs all operations of primary.
In such a way a replica set which is a group of MongoDB servers, maintain the same data set to provide redundancy and increasing data availability.

Power:
MongoDB provides lot of features of a traditional RDBMS such as :-
  • Secondary indexes,
  • Dynamic queries,
  • Sorting,
  • Rich updates, upserts (update if document exists, insert if it doesn’t), and easy aggregation.
With this we can easily relate functionalities which we use from RDBMS, with the flexibility and scaling capability that the non-relational model provides.

Use case scenarios
Now with the above explained details we have clear understanding of why we should opt for MongoDB as backend for web application development. Here are some example of applications where we use this:
  • Product Catalog
  • Inventory Management
  • Meta data/ asset management.
  • Storing comments
  • Archiving data (most famous implementation for mongoDB is : to archive old blog posts)
  • Sensor based data store
  • Online gaming
  • Logs Centralization (Asynchronous & speedy)
  • Network based queuing (distributed message-passing) implementations.

NoSQL DBMS types
There are several types of NoSQL databases are available:

Key / Value based:
Key / Value data stores are highly performant, easy to work with and they usually scale well.
  • Redis
  • Riak
  • Memcached / MemcacheDB
When to use
  • Caching
  • Queuing
  • To keep live information
  • For distributing information

Column based:
Column based data stores are extremely powerful and they can be reliably used to keep important data of very large sizes.
  • Cassandra
  • HBase
When to use
  • To keep unstructured, non-volatile information
  • To keep data stores which are highly scalable in nature.

Document based:
Document based data stores are excellent for keeping a lot of unrelated complex information that is highly variable in terms of structure from one another.
  • MongoDB
  • CouchDB
  • CouchBase
When to use
  • To keep nested information
  • To keep javascript friendly data stores.

Graph based:
Graph based data stores offer a very unique functionality that is unmatched with any other DBMSs.
  • Neo4j
  • OrientDB
When to use
  • Modelling and handling classifications
  • Handling complex relational information

Summary
In above article we learned about basics of NoSQL database management systems and why we should use them.
What are the key features provided by MongoDB which makes it best fit for web application development.
Please stay tuned for information about how to get start with or use MongoDB.





No comments:

Post a Comment