MongoDB and Redis are modern NoSQL databases. Even though they both fall under the same umbrella term—NoSQL—they have conceptually different storage models. MongoDB stores data on disk whereas Redis is an in-memory store.
In this article, we'll explore the differences between the two databases and their use cases.
MongoDB vs. Redis Key Differences
Below is a table comparing MongoDB Atlas and Redis across several key features.
What is MongoDB?
MongoDB is the leading document database chosen by architects and developers for a wide variety of use cases. The key characteristics of MongoDB, built into it from day one, are:
Distributed architecture that allows systems to scale out across multiple nodes.
Document data model that maps naturally to the objects in your code.
Focus on great developer experience with official support for over a dozen programming languages.
MongoDB stores data on the disk in Binary JSON (BSON) records called documents. The documents have a flexible schema, meaning that the structure of the stored data can change over time. You can add new fields or remove existing ones by modifying the document.
Similar or related documents are grouped in collections. Documents in a single collection can have a different set of fields. Flexible schema doesn't mean chaotic data, though - you can specify validation rules on your collections based on your application requirements.
MongoDB Atlas is MongoDB's multi-cloud database platform. It allows you to distribute and move your data across all major cloud providers - Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP).
What is Redis?
Redis, shortened from Remote Dictionary Server, is an in-memory key-value store. Most of its advantages and disadvantages stem from this very definition.
What is an in-memory store?
In-memory means that data is stored on the host's RAM (random access memory) and not on disk. Reading and writing from RAM is much faster than performing disk operations. This allows in-memory stores, such as Redis, to perform millions of requests per second.
The in-memory storage model comes with disadvantages. RAM is a lot more expensive than disk storage and therefore not cost-effective for large datasets. Simply put, your dataset is limited by the memory allocated for the Redis process.
What is a key-value store?
Key-value stores treat the data as a single collection. Each record in the collection is a pair: a key and a value associated with it. Keys must be unique as they are used to retrieve values.
The data types and allowed structures for keys and values are implementation-specific. For keys, Redis allows you to use binary-safe strings up to 512 MB. For values, you can choose from a wider range of data structures. Among the supported types are strings, lists, sets, maps, and streams.
A disadvantage of using a key-value store, such as Redis, is that there is no query language—only a basic set of commands. Additionally, Redis doesn't natively support secondary indexes. This limits your data access flexibility. If you need an access path to the data different from the primary key, you have to build and maintain your own indexes, storing them in the already limited memory.
Redis use cases
Based on its configuration, Redis can be put to different uses: a cache, session management system, and message broker are among the most common. Due to in-memory storage model shortcomings, it's rare to use Redis as a system of record.
Redis is often used in combination with an on-disk database such as MongoDB. While the on-disk database is the primary storage solution, Redis may be used as a caching layer, or for performing real-time analytics.
If you're already using MongoDB, you can achieve similar results without adding Redis to your system. MongoDB's storage engine, WiredTiger, has an internal cache that may be enough to accommodate your application's working set. In addition, MongoDB Enterprise Advanced offers an in-memory storage engine.
MongoDB vs. Redis: Data Storage
The most obvious difference between MongoDB and Redis is their conceptually different data storage model. Redis is an in-memory data store, while MongoDB's default storage engine, WiredTiger, stores data on disk.
Does Redis persist data?
Despite being an in-memory store, Redis persists data on disk to ensure data durability. If the Redis process crashes and restarts, it can restore the data from the disk. All data is accessed through RAM. When auto-tiering is enabled, frequently used values (“hot data”) are promoted from flash to RAM, while less active values (“warm data”) are stored in flash and swapped back when RAM space is needed.
Redis offers different persistence options that can be adjusted to meet your application requirements. More frequent writes to the disk mean higher durability of the data, but also degraded performance.
Does MongoDB support in-memory storage?
MongoDB supports an in-memory storage engine as part of MongoDB Enterprise Advanced. The in-memory storage engine combines the predictable latency benefits of the in-memory storage model with the rich query capabilities of MongoDB.
The in-memory storage engine doesn't write data to persistent storage. To ensure data durability, you can deploy replica sets that use a combination of the in-memory storage engine and the default persistent storage engine. In case of a crash and restart, the nodes using the in-memory storage engine can sync from the nodes using persistent storage.
Does MongoDB Atlas support in-memory storage?
The in-memory storage engine is not available for MongoDB Atlas. However, you can use NVMe SSD drives that offer more predictable latency than regular hard disk storage.
Another option is to increase the available RAM on your deployed cluster. WiredTiger, MongoDB's storage engine, will use part of the memory for caching. For more information about in-memory use in MongoDB Atlas, check out the dedicated section in the in-memory databases article.
MongoDB vs. Redis: Scalability
MongoDB has a horizontal scale-out architecture built-in from day one. Horizontal scaling is achieved with sharding, which allows you to distribute data across multiple nodes. Data is partitioned across nodes based on the configured shard key. If you decide to refine your shard key, Live Resharding allows you to redistribute your data without downtime. Introduced in MongoDB 7.0, shard key advisor commands generate metrics that will help you refine your shard keys. Redis Cluster is Redis's solution for scaling out by sharding data across multiple nodes.
Sharding strategies
Redis Cluster supports only hashed sharding. MongoDB offers different shard key strategies: ranged sharding, hashed sharding, and zoned sharding. You can choose the strategy that best suits the needs of your system. With Global Clusters in MongoDB Atlas you can set up sharded cluster zones to support geographically distributed applications.
Consistency and backups
Redis Cluster doesn't guarantee strong consistency, meaning that the cluster may lose data in case of a failure. MongoDB offers one of the strongest data safety and consistency guarantees among databases.
Redis Cluster doesn't offer cross-shard backups. Each shard must be backed up individually. MongoDB Atlas allows you to perform consistent backups with point-in-time recovery.
Language support
Redis is supported by a limited number of officially supported libraries. Currently, there are actively maintained libraries for Node.js, Python, Java, C, C#, and Go, plus five community-developed recommended libraries. MongoDB is supported by more than a dozen language-specific official drivers, plus dozens more developed by the community.
It’s worthwhile to note that the Redis community ecosystem includes numerous additional drivers.
Summary
MongoDB is a general-purpose persistent database used as a primary storage solution by businesses in various industries. Redis is an in-memory data store that can be configured to work in different ways depending on your system's needs.
You can achieve in-memory data store performance with your MongoDB database by:
configuring your cluster's RAM to accommodate the working set.
using NVMe SSD drives.
using the in-memory storage engine.