MongoDB and Redis both fall into the NoSQL category, but that’s where the similarities end. Disk-based MongoDB is optimized for operational simplicity, schema-free design, and very large data volumes. Compared to relational databases, MongoDB works really well if data requires complex processing but doesn’t fit a rigid schema. MongoDB scales on commodity hardware easily and helps developers adapt to changing needs faster and more cost effectively than relational databases.
Redis, while also schema-free, is an in-memory data structure store, frequently used as a database, cache, or message broker. Unlike other in-memory stores, Redis persists data. It is popular with developers because of its versatile, optimized data structures -- sets, sorted sets, hashes, lists, strings, bit arrays -- which deliver efficient in-database operations such as set comparisons, list pull-push operations, and range queries.
Purpose-built for performance, Redis allows developers to run most operations on these structures at the lowest possible complexity and highest possible speed. Because of their differences, MongoDB and Redis can be complementary for use cases such as mobile, IoT, APIs, massive Web apps, and other applications where data is generated too fast for it to be digested easily by a disk-based database alone.
MongoDB is great at handling any volume or variety of data being generated. However, extreme velocity poses a challenge to any disk-based database. When there are millions of data points coming at an application, using a disk-based database directly means you run the risk of losing data or creating excessively long queues to store data that's waiting to be processed. Thus, Redis is commonly used as a data ingestion buffer for MongoDB. With Redis running very efficiently in memory, it becomes simple to enable real-time analysis of fast-moving data by shifting some of the processing to Redis.
A growing number of use cases are emerging where MongoDB and Redis complement each other. The following are examples of real-world applications that combine MongoDB and Redis to store data at scale and process data incredibly fast.
Real-time analytics service. A Google Analytics-like service to large websites and Web-based applications provides insights into user behavior by collecting millions of event data points and aggregating and segmenting this data to glean intelligence. The primary data store is MongoDB, given its strengths in handling data with varying schemas. Redis is used for real-time analysis, calculations, and display of the high-volume event data that it collects.
Redis is also used for sessionizing data. Every event collected from an application or website belongs to a session that a user starts against Redis. Documents with tens or thousands of events create a stream of data that requires some amount of unraveling. When there are hundreds and thousands of users, event streams related to many users are interleaved. Updating each document with many small updates can be accomplished with disk-based MongoDB, but the expense of the operation is high and Redis’ hash data structure can make short work of the problem.
Hashes can be used to store event data by session. Keeping track of sessions that need to be timed out is also nontrivial when there are thousands of sessions, but Redis has built-in key expiration and timeout setting functionality that can be used to end sessions. Keyspace notifications allow users to subscribe to expired events and trigger an offload to MongoDB.
Real-time navigation service. Real-time navigation involves a huge amount of data that must constantly be updated with changing conditions. A navigation service, like Waze or Google Maps but with a focus on public transportation, combines MongoDB and Redis to support tens of millions of users. MongoDB stores static information such as destinations, routes, and vehicles, while real-time reporting from apps on mobile devices, user reports of current traffic conditions, and GPS transponder data are fed into Redis, so users can know the state of live traffic and plan their routes on the fly.
Massively multiplayer online game. A game catering to thousands of teams with hundreds of players each, all competing for resources and control, has unique real-time requirements. Here MongoDB stores information such as tournament profiles, resources, and resource profiles, while Redis manages game progress, user scores, and everything else that requires rapid updates and displays.
Redis sorted sets are useful in these scenarios. The
ZADD command adds users to the sorted set in the order of their score, and the
ZINCRBY command simply increments scores by a specified number.
Online dating application. While there are many thousands of dating applications, this particular one caters to immediately finding groups of people by geographical proximity. MongoDB stores user profiles, while Redis tracks the location of users in real time and instantly reconciles their preferences with those of others.
Redis commands like
GEOADD can add geospatial information such as latitude and longitude to a specified key. Further, the data is stored in a sorted set in a way that allows retrieval by proximity commands such as
GEORADIUS by member.
Internet of things application. An environmental monitoring application collects real-time information such as temperature, humidity, and CO2 levels from sensors dropped into heavily forested areas in order to detect forest fires and track other environmental problems. The sensor data, temperature maps, and other variables needed for real-time calculations and alerting are stored in Redis. Historical data is stored in MongoDB.
As the first-responder database, Redis not only provides efficient and high-performance data processing (1.5 million operations per second at submillisecond latency with a single AWS EC2 server), it is also a stable, reliable database that can be persisted to disk as needed. When several documents in MongoDB are being updated simultaneously or in very close succession, every update triggers a write to the database. Write operations, with disk-based databases, are expensive operations. If you have thousands or tens of thousands of these per second, you would have to maintain many shards, throw a ton of hardware at the problem, and still have to worry about write latency.
Redis offers throughput that is orders of magnitude higher than disk-based databases, and latency that is consistently submillisecond. By using Redis, you can manage the rapid updates in memory while consolidating writes to the disk-based database at a lower frequency. This approach reduces hardware needs and creates savings on operational costs.
We are increasingly living in a multiple database world, where MongoDB, one of the most popular NoSQL databases available, and Redis, one of the fastest-growing databases, work in tandem to support new applications that demand both high volume and high velocity. Having already made the move to NoSQL databases, MongoDB shops are finding that the functionality and speed of Redis complements MongoDB for real-time use cases.
Itamar Haber (@itamarhaber) is chief developer advocate at Redis Labs, the home of the Redis open source and provider of commercial Redis both on-premises and in the cloud. His varied experience includes software product development and management and leadership roles at Xeround, Etagon, Amicada, and M.N.S Ltd. Itamar holds a Master of Business Administration from the joint Kellogg-Recanati program by Northwestern and Tel-Aviv Universities, as well as a Bachelor of Science in Computer Science.
New Tech Forum provides a venue to explore and discuss emerging enterprise technology in unprecedented depth and breadth. The selection is subjective, based on our pick of the technologies we believe to be important and of greatest interest to InfoWorld readers. InfoWorld does not accept marketing collateral for publication and reserves the right to edit all contributed content. Send all inquiries to firstname.lastname@example.org.