A Quick Comparison Between Redis And MongoDB

Posted By : Satish Joshi | 20-Jun-2018

Redis is a memory resident database known for its speed and high performance, while MongoDB is a Document-Oriented Database known for its flexibility and scalability. 

 

Who's Currently Behind The Databases?                                                                                                                                                                                                                      

Redis : Redis was initially released in 2009 and is currently run by RedisLabs.                                                                                                                                                  

MongoDB : MongoDB is currently managed by MongoDB, Inc which was originally started in 2007 by 10gen and later changed its name to MongoDB Inc. 

 

 

Database & Query Structure                                                                                                                                                                                                                                           

Redis :-                                                                                                                                                                                                                                                                                                  

In its simplest form, Redis uses the key-value pair to store data of a key and an associated value.                                                                                                                                  

Its structure is very different from relational databases that have tables featuring rows and columns.                                                                                                                             

It holds its database entirely in the memory, using the disk space for persistence.                                                                                                                                                            

A simple key-value pair can be represented with this format and values can contain more complex data types :                                                                                                            

name: "Redis"

id: 1                                                                                                                                                                                                                                                                                     

To retrieve the name or id we would need to use the GET command :                                                                                                                                                                              

> GET name

"Redis"                                                                                                                                                                                                                                                                                                           

Redis also offers more complex data structures like strings, lists, sets, sorted sets, hashes and more.                                                                                                                              

Its commands are used for managing keys in Redis with the following syntax :                                                                                                                                                               

COMMAND KEY_NAME                                                                                                                                                                                                                        

Examples: SET name "Redis", GET name, DEL name, KEYS* (Retrieves all the existing keys and values) etc.

 
 
MongoDB :-
 

MongoDB is a document-oriented database which uses JSON like documents to store schema-free data I;e; without a predefined structure.

 

It works on the concept of collection and document.

     

A collection is a group of MongoDB documents whereas Document is a set of key-value pairs. Documents have dynamic schema I;e; documents in the same collection do not need to have the same set of fields or structure.                                                                                                                                                                    

A document in Mongo can be represented in JSON format:

                                                        

{

"_id": ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer),

"name": "redis",

}

 

_id is 12 bytes hexadecimal number which is unique for every document in a collection.

                                         

Its commands are used for storing Key-value pair in JSON format with the following syntax :

                

> DB.COLLECTION_NAME.QUERY_TYPE(document)                                                                                                                                                                                                                        

Examples : db.collection_name.insert({"name":"redis,tags:['mongoDB','NoSQL']}), db.collection_name.drop(), db.collection_name.find() etc.

 

MongoDB supports many data types like String, Integer, Boolean, Object, Null, Symbol, Date, Arrays, Timestamp, Binary data and many more.

                                                                                                                                                                                                                                                                                           
Which Database Is Right For Your Application? 
 

REDIS :-                                                                                                                                                                                                                                                                             

  • Redis uses key-value storage model with having additional data types.

  • It offers master-slave or one-way replication I;e; A master controls one or more other processes known as slaves.

  • Instead of connecting to the master for reading data, clients will connect to one of the slaves to read their data.

  • For High-performance database Redis is faster than MongoDB.

  • A great choice if you have well-planned design and need fast access.

  • It is possible to use sorted sets to create these indexes. 

  • It is not scalable as MongoDB does.

  • It is developed in C language and uses Lua for server-side scripting.                                                                                                                                                                     

MongoDB :-                                                                                                                                                                                                                                                                          

  • MongoDB stores the data in the form of document and they can be modified.

  • It offers Single Master replication i.e; one member is primary (master) and any replicated databases have a secondary role.

  • Here By default clients reads from the primary however they can specify a read preference to send read operations to secondaries. If the primary is unavailable, an eligible secondary will become a new primary.

  • For Flexibility, high availability and Unstructured design MongoDB is probably a better choice but not for if speed is your biggest requirement.

  • A great choice if you have unstructured or structured data without a clear definition of the data structure.

  • Because of its flexible database design, it scales better than redis.

  • Indexes are preferred for MongoDB. If you don’t have an index, every document must be searched within the collection, which can slow read times.

  • It is written in C++ language and it uses Javascript for server-side scripting.                                                                                                                                                                                                                                                                                                                                                                                                              

CONCLUSION

 

It totally depends upon your application and the motives regarding building your app. Both the databases are perfect to build a highly scalable and performance-oriented system depending on their storage and uses.

If your priority is speed and limited storage then Redis is a perfect choice but when u need very large data sets and they are growing on a regular basis, and for Unstructured data and flexibility then MongoDB is a better choice.

     

About Author

Author Image
Satish Joshi

Satish has experience on web development believes in hard work and dedication. He is friendly and an honest person.

Request for Proposal

Name is required

Comment is required

Sending message..