Redis vs MongoDB – Redis vs MongoDB

最后修改: 2020年 12月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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.

在本文中,我们将探讨两个流行的非关系型数据库:RedisMongoDB

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.

首先,我们将快速浏览一下Redis和MongoDB提供的功能。然后,我们将通过相互比较来讨论何时使用Redis或MongoDB。

2. Redis

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.

Redis是一个内存数据结构存储,它提供了一系列丰富的功能。它可以作为缓存、消息代理和队列使用。

2.1. Features

2.1.特点

2.2. Installation

2.2.安装

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

我们可以从官方网站下载最新的Redis服务器,并安装它。

$ 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

3.蒙哥马利数据库

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.

MongoDB是一个NoSQL文档数据库,以类似JSON的文档结构存储信息。它作为一种无主题的数据存储,对于快速变化的应用程序、原型设计以及处于设计和实施阶段的初创公司来说非常有用

3.1. Features

3.1.特点

  • 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

3.2.安装

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

我们可以下载最新的 MongoDB 服务器,如果使用 macOS,我们可以使用Homebrew直接安装社区版。

brew tap mongodb/brew
brew install mongodb-community@4.4

4. When to Use Redis?

4.什么时候使用Redis?

4.1. Caching

4.1.缓存

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

Redis提供一流的缓存性能,对频繁请求的项目提供亚毫秒级的响应时间

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

此外,它允许使用EXPIREEXPIREATPEXPIRE等命令对密钥设置过期时间。

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.

同时,我们可以使用PERSIST命令来移除超时并持久化键值对,使其成为理想的缓存。

4.2. Flexible Data Storage

4.2.灵活的数据存储

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.

Redis提供各种数据结构,如字符串、列表、集合和哈希,以决定如何存储和组织我们的数据。因此,Redis让我们对数据库结构的实现有充分的自由。

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.

然而,它也可能需要很长的时间来思考数据库的设计。同样地,使用Redis构建和维护模式的内部结构也是一种挑战。

4.3. Complex Data Storage

4.3.复杂的数据存储

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

4.4.聊天、队列和消息代理

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.

Redis可以使用模式匹配的pub/sub消息队列发布和订阅消息。因此,Redis可以支持实时聊天和社交媒体反馈应用。

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.

同样地,我们可以使用列表数据结构实现轻量级队列。此外,Redis的列表支持原子操作并提供阻塞功能,使其适合于实现一个消息代理。

4.5. Session Store

4.5.会销店

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

Redis提供了一个具有持久性功能的内存数据存储,使其成为存储和管理网络/移动应用程序会话的良好候选者。

4.6. IoT and Embedded Systems

4.6.物联网和嵌入式系统

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

根据Redis的官方文档,从4和5开始的新版本支持ARM处理器和Raspberry Pi

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

另外,它在Andriod上运行,并且正在努力将Android纳入官方支持的平台。

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

因此,Redis看起来是物联网和嵌入式系统的理想选择,受益于其小的内存占用和低的CPU要求。

4.7. Real-Time Processing

4.7.实时处理

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.

例如,Redis可以有效地服务于提供股票价格提醒、排行榜和实时分析等功能的应用程序。

4.8. Geospatial Apps

4.8.地理空间应用程序

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.

Redis提供了一个特制的内存数据结构Geo Set–建立在排序集上–用于管理地理空间指数。此外,它还提供了特定的地理命令,如GEOADDGEOPOSGEORADIUS来添加、读取和分析地理空间数据。

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

因此,我们可以使用Redis构建具有基于位置特征的实时地理空间应用程序,如驾驶时间和驾驶距离

5. When to Use MongoDB?

5.什么时候使用MongoDB?

5.1. Dynamic Queries

5.1.动态查询

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.

MongoDB提供了一套强大的查询工具。同时,它还提供了广泛的灵活的查询方案,如基于地理的搜索、图形搜索和文本搜索,以实现高效的数据检索。

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.

同时,由于支持JSON结构的查询,MongoDB看起来是一个更好的选择,因为数据搜索和分析是日常活动的场景。

5.2. Rapidly Changing Schema

5.2.快速变化的模式

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.

在设计和早期实施阶段,MongoDB可以提供帮助,因为我们需要快速改变我们的模式。同时,它不对底层数据进行假设,而且不需要模式就能进行自我优化。

5.3. Prototyping and Hackathons

5.3.原型设计和黑客马拉松

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

通过遵循类似JSON的文档结构,MongoDB允许快速的原型设计、与前端渠道的快速集成以及黑客活动

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

同时,对于不想处理RDBMS复杂问题的初级团队来说,它也很有用。

5.4. Catalogs

5.4.目录

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.

通过提供自我描述的动态模式,MongoDB使其更容易为电子商务、资产管理和库存等目录添加产品、功能和推荐

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.

我们还可以在MongoDB中使用表达式查询,通过对JSON结构的文档的一个字段或一组字段进行索引,实现高级搜索和分析等功能。

5.5. Mobile Apps

5.5.移动应用程序

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

MongoDB的JSON文档结构允许存储来自不同设备的不同类型的数据以及地理空间索引。

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.

此外,通过原生分片的水平可扩展性,可以轻松扩展移动应用程序。因此,MongoDB可以为数以吨计的用户提供服务,处理PB级的数据,并支持每秒数十万次的操作,使其成为支持移动应用程序的一个值得信赖的选择。

5.6. Content-Rich Apps

5.6.内容丰富的应用程序

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.

对于现代内容丰富的应用程序来说,在RDBMS中纳入各种内容并不容易。另一方面,MongoDB允许存储和提供文本、音频和视频等丰富内容

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.

此外,我们可以使用MongoDB GridFS轻松地高效存储大于16MB的文件。它允许访问大文件的一部分,而无需将整个文件加载到内存中。

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.

此外,它还能在所有服务器上自动同步我们的文件和元数据。因此,MongoDB看起来是一个更适合支持内容丰富的应用程序的选择。

5.7. Gaming Apps

5.7.游戏应用

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.

与移动和内容丰富的应用程序类似,游戏也需要大规模的扩展和动态数据结构。因此,MongoDB可以成为游戏应用程序的一个有希望的选择。

5.8. Global Cloud Database Service

5.8.全球云数据库服务

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.

MongoDB Atlas可用于多种云服务,如AWS、Google Cloud和Azure。此外,通过内置的复制和故障转移机制,它提供了一个高度可用的分布式系统。因此,我们可以快速部署和管理数据库,并将其作为一个全球云数据库服务。

6. Conclusion

6.结语

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

在这篇文章中,我们探讨了Redis和MongoDB作为非关系型数据库的选择。

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.

我们当然可以得出结论:Redis看起来很有希望成为缓存、消息代理和队列的一个更好的解决方案。同时,它可以证明在实时处理、地理空间应用程序和嵌入式系统中的价值。

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.

另一方面,MongoDB是存储类似JSON对象的可靠选择。因此,MongoDB可以最适合用于原型设计、现代内容丰富、移动和游戏应用程序的无模式架构。