Use Case: Redis vs MongoDB

Table of Contents

Introduction

Both Redis and MongoDB are popular, high-performance "NoSQL" alternatives to a traditional relational database like MySQL. These use cases explore the difference between them. Learn the advantages and disadvantages of each, and find more information on which one will best suit your project.

Related articles:

Use Case: Overview

Redis is a key-value store, which stores data in a flexible structure similar to a dictionary or hash.

Redis stores data in memory, which makes it extremely fast, as data never has to be retrieved from disk. This makes Redis an excellent choice for so-called "hot data," which needs to be accessed on the fly, but doesn't need to be permanently stored.

MongoDB is slightly more similar to a traditional relational database. MongoDB stores data in schema-free, JSON-like documents which can be accessed with MongoDB's SQL-like query language.

Scenario 1: Session Caching

In this scenario, the user needs to set up a system to store session caching for website visitors. This will hold on to the visitor's session data (including which pages the visitor has seen, and what items they have viewed) for 24 hours.

Redis is the best choice for this user. Because Redis does not write data to the physical disk, it is an excellent solution for "hot data" which needs to be kept for a short amount of time, but not permanently stored.

Redis's fast response times and lightweight data structure also make it an excellent choice for storing session data.

Scenario 2: Query Flexibility

In this scenario, the user wants to store customer data, and plans to query that data in a variety of ways.

MongoDB is the best choice for this project. MongoDB's flexible data schema means that the user can store a variety of data about a customer without having to worry about creating new table fields.

In addition, MongoDB offers a powerful set of querying tools which allow the user to query the data in many different ways, similar to traditional SQL "join" statements.

Scenario 3: Scaling

For this scenario, the user is planning for a lot of growth in the future, and needs to be able to scale up their implementation.

MongoDB is the clear choice when it comes to scaling. MongoDB supports more scaling methods than Redis, partly because Redis's growth is limited by the amount of RAM on the physical machine. Although it is certainly possible to scale up a Redis installation, this process is simpler and more stable with MongoDB.

Scenario 4: Performance

The user in this scenario needs the fastest possible performance from their database solution, and is willing to compromise on other points to achieve it.

Because it stores all of its data in memory, the performance speed of Redis is unmatched. Although MongoDB is quite fast, the benchmarks for Redis are clearly faster.

Tags: Redis