Redis and MongoDB are both very popular choices as NoSQL databases. They share some important properties such as speed and having data organized in ways that are friendly to developers, but under what circumstances does Redis outperform MongoDB? It really depends on the type and nature of the data you will be storing, and how it will be used in your applications.
Where MongoDB is Advantageous
If you are storing and querying very large datasets—most notably if those datasets will be growing on a regular basis—then MongoDB is the winner. The reason for this is that MongoDB excels at scaling ever-growing datasets, while also making this process easy on database administrators and developers.
In the realm of development, MongoDB provides the BSON data format, which is much like the JSON format with which numerous developers have quite a bit of previous experience. This provides a hierarchical structure of the data within documents, each of which can have subdocuments. This works well for developers trained in object-oriented languages already.
An example of a MongoDB query. Source: MongoDB.
Where Redis Excels
Will your database have a more predictable size? If the size of your database will basically stay the same over time, Redis can optimize this feature to provide you with a great deal of lookup speed. By placing the database in memory, Redis can execute high-performance caching and locate the required data extremely quickly.
Another way in which Redis can optimize for speed is its organization as a key-value store. If you have a known key, the lookup of the data is very quick. For example, applications that process data in real time, such as a stock price tracking app, will run very quickly on Redis since the lookups of the prices for the specific keys will be optimized.
For developers, there is also the option to use more complex data structures like scalars, sets, hashes, and lists. These are very similar to the similarly-named structures in many programming languages, which can make the learning curve less steep for developers already familiar with those structures.
An example of a Redis list. Source: Redis.
When Redis is Faster
In summary, Redis will be the better choice for performance if you meet most of these conditions:
- You will have a fairly consistent and knowable database size.
- Speed will be extremely important, and you will be able to use a known key for data retrieval.
- You want to store data for real-time display or real-time analytics.
With this in mind, you should be able to determine when and how Redis will be faster than MongoDB for the applications you are implementing!