Persistence top

The early-bird price of the new Learn Spring Data JPA course is increasing by $35 on Friday:

>> GET ACCESS NOW

1. Overview

Often, we find it challenging to decide on a non-relational database as a primary data store for our applications.

In this article, we'll explore two popular non-relational databases, Redis and MongoDB.

First, we'll take a quick look at the features offered by Redis and MongoDB. Then, we'll discuss when to use Redis or MongoDB by comparing them against each other.

2. Redis

Redis is an in-memory data structure store that offers a rich set of features. It's useful as a cache, message broker, and queue.

2.1. Features

2.2. Installation

We can download the latest Redis server from the official website and install it:

$ wget http://download.redis.io/releases/redis-6.0.9.tar.gz
$ tar xzf redis-6.0.9.tar.gz
$ cd redis-6.0.9
$ make

3. MongoDB

MongoDB is a NoSQL document database that stores information in a JSON-like document structure. It's useful as a schemaless data store for rapidly changing applications, prototyping, and startups in a design and implementation phase.

3.1. Features

  • Offers an interactive command-line interface MongoDB Shell (mongosh) to perform administrative operations and query/update data
  • JSON based query structure with the support of joins
  • Supports various types of searches like geo-based search, graph search, and text search
  • Supports multi-document ACID transactions
  • Spring Data support
  • Available in community, enterprise, and cloud (MongoDB Atlas) editions
  • Various drivers for major technologies like C++, Java, Go, Python, Rust, and Scala
  • Provides GUI to explore and manipulate data through MongoDB Compass
  • Offers a visual representation of data using MongoDB Charts
  • MongoDB BI Connector provides connections to BI and analytics platforms

3.2. Installation

We can download the latest MongoDB server or, if using macOS, we can install the community edition directly using Homebrew:

brew tap mongodb/brew
brew install [email protected]

4. When to Use Redis?

4.1. Caching

Redis provides best-in-class caching performance by providing sub-millisecond response time on frequently requested items.

Furthermore, it allows setting expiration time on keys using commands like EXPIRE, EXPIREAT, and PEXPIRE.

At the same time, we can use the PERSIST command to remove the timeout and persist the key-value pair, making it ideal for caching.

4.2. Flexible Data Storage

Redis provides various data structures like string, list, set, and hash to decide how to store and organize our data. Hence, Redis gives us full freedom over the implementation of the database structures.

However, it may also require a long time to think through the DB design. Similarly, it can be challenging to build and maintain the inner structure of the schema using Redis.

4.3. Complex Data Storage

Similarly, with the combination of the list, set, and hash, we can implement complex data structures like queues, arrays, sorted sets, and graphs for our storage.

4.4. Chat, Queue, and Message Broker

Redis can publish and subscribe to messages using pub/sub message queues with pattern matching. Thus, Redis can support real-time chat and social-media feed applications.

Similarly, we can implement a lightweight queue using the list data structure. Furthermore, Redis's list supports atomic operations and offer blocking capabilities, making it suitable to implement a message broker.

4.5. Session Store

Redis provides an in-memory data store with persistence capabilities, making it a good candidate to store and manage sessions for web/mobile applications.

4.6. IoT and Embedded Systems

As per Redis's official documentation, newer versions starting from 4 and 5 support the ARM processor and the Raspberry Pi.

Also, it runs on Andriod, and efforts are in place to include Android as an officially supported platform.

So, Redis looks ideal for IoT and embedded systems, benefitted by its small memory footprint and low CPU requirements.

4.7. Real-Time Processing

Being a blazing fast in-memory data structure, we can use it for real-time processing applications.

For instance, Redis can efficiently serve applications that offer features like stock price alerts, leaderboards, and real-time analytics.

4.8. Geospatial Apps

Redis offers a purpose-built in-memory data structure Geo Set – built on sorted set – for managing geospatial indices. Also, it provides specific geo commands like GEOADD, GEOPOS, and GEORADIUS to add, read, and analyze geospatial data.

Therefore, we can build real-time geospatial applications with location-based features like drive time and drive distance using Redis.

5. When to Use MongoDB?

5.1. Dynamic Queries

MongoDB offers a powerful set of query tools. Also, it provides a wide range of flexible query schemes like geo-based search, graph search, and text search for efficient data retrieval.

At the same time, with the support of JSON-structured queries, MongoDB looks to be a better choice for scenarios where data search and analytics are daily activities.

5.2. Rapidly Changing Schema

MongoDB can be helpful in the design and early implementation phases, where we require quick changes to our schema. At the same time, it doesn't make assumptions on the underlying data, and it optimizes itself without needing a schema.

5.3. Prototyping and Hackathons

By following the JSON-like document structure, MongoDB allows for rapid prototyping, quick integrations with front-end channels, and hackathons.

At the same time, it can be useful for junior teams that don't want to deal with the complexities of an RDBMS.

5.4. Catalogs

By providing a dynamic schema that is self-describing, MongoDB makes it easier to add products, features, and recommendations for catalogs like e-commerce, asset management, and inventory.

We can also use expressive queries in MongoDB for features like advanced search and analytics by indexing a field or set of fields of the JSON-structured document.

5.5. Mobile Apps

MongoDB’s JSON document structure allows storing different types of data from various devices along with geospatial indexes.

Besides, horizontal scalability with native sharding allows easy scaling of a mobile app. Therefore, MongoDB can serve tons of users, process petabytes of data, and support hundreds of thousands of operations per second, making it a worthy choice for backing mobile apps.

5.6. Content-Rich Apps

It's not easy to incorporate various content in RDBMS for modern content-rich apps. On the other hand, MongoDB allows storing and serving rich content like text, audio, and video.

Also, we can easily store files larger than 16MB efficiently using MongoDB GridFS. It allows accessing a portion of large files without loading the entire file into memory.

Additionally, it automatically syncs our files and metadata across all servers. As a result, MongoDB looks to be a more suitable choice for supporting content-rich apps.

5.7. Gaming Apps

Similar to mobile and content-rich apps, gaming also requires massive scaling and dynamic data structures. Thus, MongoDB can be a promising choice for gaming apps.

5.8. Global Cloud Database Service

MongoDB Atlas is available across multiple cloud services like AWS, Google Cloud, and Azure. In addition, with built-in replication and failover mechanism, it offers a highly available distributed system. Therefore, we can quickly deploy and manage the database and use it as a global cloud database service.

6. Conclusion

In this article, we explored Redis and MongoDB as choices for a non-relational database.

First, we looked at the features offered by both databases. Then, we explored scenarios where one of them is better than the other.

We can certainly conclude Redis looks promising as a better solution for caching, message broker, and queue. At the same time, it can prove worthy in real-time processing, geospatial apps, and embedded systems.

On the other hand, MongoDB is a solid choice for storing JSON-like objects. As a result, MongoDB can be best suited for schema-less architecture for prototyping, modern-day content-rich, mobile, and gaming applications.

Persistence bottom

The early-bird price of the new Learn Spring Data JPA course is increasing by $35 on Friday:

>> GET ACCESS NOW
Comments are closed on this article!