mongodb 带有Redis的MongoDB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10696463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB with redis
提问by tonyl7126
Can anyone give example use cases of when you would benefit from using Redis and MongoDB in conjunction with each other?
任何人都可以举例说明何时可以从相互结合使用 Redis 和 MongoDB 中受益?
回答by Didier Spezia
Redis and MongoDB can be used together with good results. A company well-known for running MongoDB and Redis (along with MySQL and Sphinx) is Craiglist. See this presentationfrom Jeremy Zawodny.
Redis 和 MongoDB 可以一起使用,效果很好。一家以运行 MongoDB 和 Redis(以及 MySQL 和 Sphinx)而闻名的公司是 Craiglist。请参阅Jeremy Zawodny 的此演示文稿。
MongoDB is interesting for persistent, document oriented, data indexed in various ways. Redis is more interesting for volatile data, or latency sensitive semi-persistent data.
MongoDB 对持久的、面向文档的、以各种方式索引的数据很有趣。Redis 对于易失性数据或延迟敏感的半持久性数据更有趣。
Here are a few examples of concrete usage of Redis on top of MongoDB.
下面举几个Redis在MongoDB之上的具体用法的例子。
Pre-2.2 MongoDB does not have yet an expiration mechanism. Capped collections cannot really be used to implement a real TTL. Redis has a TTL-based expiration mechanism, making it convenient to store volatile data. For instance, user sessions are commonly stored in Redis, while user data will be stored and indexed in MongoDB. Note that MongoDB 2.2 has introduced a low accuracy expiration mechanism at the collection level (to be used for purging data for instance).
Redis provides a convenient set datatype and its associated operations (union, intersection, difference on multiple sets, etc ...). It is quite easy to implement a basic faceted search or tagging engine on top of this feature, which is an interesting addition to MongoDB more traditional indexing capabilities.
Redis supports efficient blocking pop operations on lists. This can be used to implement an ad-hoc distributed queuing system. It is more flexible than MongoDB tailable cursors IMO, since a backend application can listen to several queues with a timeout, transfer items to another queue atomically, etc ... If the application requires some queuing, it makes sense to store the queue in Redis, and keep the persistent functional data in MongoDB.
Redis also offers a pub/sub mechanism. In a distributed application, an event propagation system may be useful. This is again an excellent use case for Redis, while the persistent data are kept in MongoDB.
Pre-2.2 MongoDB 还没有过期机制。上限集合不能真正用于实现真正的 TTL。Redis 具有基于 TTL 的过期机制,可以方便地存储易失性数据。例如,用户会话通常存储在 Redis 中,而用户数据将存储和索引在 MongoDB 中。请注意,MongoDB 2.2 在集合级别引入了低准确度过期机制(例如用于清除数据)。
Redis 提供了一种方便的集合数据类型及其相关操作(联合、交集、多个集合上的差异等......)。在此功能之上实现基本的分面搜索或标记引擎非常容易,这是对 MongoDB 更传统的索引功能的有趣补充。
Redis 支持对列表进行高效的阻塞弹出操作。这可用于实现临时分布式排队系统。它比 MongoDB 可尾游标 IMO 更灵活,因为后端应用程序可以在超时的情况下侦听多个队列,以原子方式将项目传输到另一个队列等......如果应用程序需要一些排队,则将队列存储在 Redis 中是有意义的,并将持久的功能数据保存在 MongoDB 中。
Redis 还提供了发布/订阅机制。在分布式应用程序中,事件传播系统可能很有用。这又是 Redis 的一个很好的用例,而持久数据保存在 MongoDB 中。
Because it is much easier to design a data model with MongoDB than with Redis (Redis is more low-level), it is interesting to benefit from the flexibility of MongoDB for main persistent data, and from the extra features provided by Redis (low latency, item expiration, queues, pub/sub, atomic blocks, etc ...). It is indeed a good combination.
因为用 MongoDB 设计数据模型比用 Redis 容易得多(Redis 更底层),所以有趣的是从 MongoDB 对主要持久数据的灵活性以及 Redis 提供的额外功能(低延迟)中受益、项目过期、队列、发布/订阅、原子块等...)。这确实是一个很好的组合。
Please note you should never run a Redis and MongoDB server on the same machine. MongoDB memory is designed to be swapped out, Redis is not. If MongoDB triggers some swapping activity, the performance of Redis will be catastrophic. They should be isolated on different nodes.
请注意,永远不要在同一台机器上运行 Redis 和 MongoDB 服务器。MongoDB 内存旨在换出,而 Redis 则不是。如果 MongoDB 触发一些交换活动,Redis 的性能将是灾难性的。它们应该在不同的节点上隔离。
回答by Brian P O'Rourke
Obviously there are farmore differences than this, but for an extremely high overview:
显然,有远远更多的差异比这个,但极高的概述:
For use-cases:
对于用例:
- Redis is often used as a caching layer or shared whiteboard for distributed computation.
- MongoDB is often used as a swap-out replacement for traditional SQL databases.
- Redis 常被用作分布式计算的缓存层或共享白板。
- MongoDB 通常用作传统 SQL 数据库的换出替代品。
Technically:
技术上:
- Redis is an in-memory db with disk persistence (the whole db needs to fit in RAM).
- MongoDB is a disk-backed db which only needs enough RAM for the indexes.
- Redis 是一个具有磁盘持久性的内存数据库(整个数据库需要适合 RAM)。
- MongoDB 是一个磁盘支持的数据库,它只需要足够的 RAM 来存储索引。
There is some overlap, but it is extremely common to use both. Here's why:
有一些重叠,但同时使用两者是非常常见的。原因如下:
- MongoDB can store more data cheaper.
- Redis is faster for the entire dataset.
- MongoDB's culture is "store it all, figure out access patterns later"
- Redis's culture is "carefully consider how you'll access data, then store"
- Both have open source tools that depend on them, many of which are used together.
- MongoDB 可以更便宜地存储更多数据。
- 对于整个数据集,Redis 速度更快。
- MongoDB 的文化是“全部存储,稍后找出访问模式”
- Redis 的文化是“仔细考虑如何访问数据,然后存储”
- 两者都有依赖于它们的开源工具,其中许多是一起使用的。
Redis can be used as a replacement for a traditional datastore, but it's most often used withanother normal "long" data store, like Mongo, Postgresql, MySQL, etc.
Redis的可以作为一个传统的数据存储的替代品,但它是最常用的与其他正常的“长”数据存储,如蒙戈和PostgreSQL,MySQL的,等等。
回答by Daniel
Redis works excellently with MongoDB as a caching server. Here is what happens.
Redis 作为缓存服务器与 MongoDB 一起工作得非常好。这是发生的事情。
Anytime that mongoose issues a cache query, it will first go over to the cache server.
任何时候猫鼬发出缓存查询,它都会首先转到缓存服务器。
The cache server will check to see if that exact query has ever been issued before.
缓存服务器将检查该确切查询之前是否曾发出过。
If it hasn't then the cache server will take the query, send it over to mongodb and Mongo will execute the query.
如果没有,则缓存服务器将接受查询,将其发送到 mongodb,Mongo 将执行查询。
We will then take the result of that query, it then goes back to the cache server, the cache server will store the result of the query on itself.
然后我们将获取该查询的结果,然后返回到缓存服务器,缓存服务器会将查询结果存储在自己身上。
It will say anytime I execute that query, I get this response and so its going to maintain a record between queries that are issued and responses that come back from those queries.
它会说无论何时我执行该查询,我都会收到此响应,因此它将在发出的查询和从这些查询返回的响应之间维护记录。
The cache server will take the response and send it back to mongoose, mongoose will give it to express and it eventually ends up inside the application.
缓存服务器将接收响应并将其发送回 mongoose,mongoose 将其交给 express 并最终在应用程序中结束。
Anytime that the same exact query is issued again, mongoose will send the same query to the cache server, but if the cache server sees that this query was issued before it will not send the query onto mongodb, instead its going to take the response to the query it got the last time and immediately send it back over to mongoose. There is no indices here, no full table scan, nothing.
任何时候再次发出相同的查询,mongoose 都会将相同的查询发送到缓存服务器,但是如果缓存服务器看到此查询在它之前发出过,它不会将查询发送到 mongodb,而是将响应发送到它上次得到的查询并立即将其发送回猫鼬。这里没有索引,没有全表扫描,什么都没有。
We are doing a simple lookup to say has this query been executed? Yes? Okay, take the request and send it back immediately and don't send anything to mongo.
我们正在做一个简单的查找来说明这个查询是否被执行了?是的?好的,接受请求并立即将其发回,不要向 mongo 发送任何内容。
We have the mongoose server, the cache server (Redis) and Mongodb.
我们有猫鼬服务器、缓存服务器 (Redis) 和 Mongodb。
On the cache server there might be a datastore with key value type of data store where all the keys are some type of query issued before and the value the result of that query.
在缓存服务器上可能有一个数据存储的键值类型的数据存储,其中所有的键是之前发出的某种类型的查询,值是该查询的结果。
So maybe we are looking up a bunch of blogposts by _id.
所以也许我们正在通过 _id 查找一堆博客文章。
So maybe the keys in here are the _id of the records we have looked up before.
所以也许这里的键是我们之前查找过的记录的 _id。
So lets imagine that mongoose issues a new query where it tries to find a blogpost with _id of 123, the query flows into the cache server, the cache server will check to see if it has a result for any query that was looking for an _id of 123.
所以让我们想象一下猫鼬发出一个新的查询,它试图找到一个 _id 为 123 的博客文章,查询流入缓存服务器,缓存服务器将检查它是否有任何正在寻找 _id 的查询的结果共 123 个。
If it does not exist in the cache server, this query is taken and sent on to the mongodb instance. Mongodb will execute the query, get a response and send it back.
如果缓存服务器中不存在该查询,则执行此查询并将其发送到 mongodb 实例。Mongodb 将执行查询,获得响应并将其发回。
This result is sent back over to the cache server who takes that result and immediately sends it back to mongoose so we get as fast a response as possible.
该结果被发送回缓存服务器,缓存服务器获取该结果并立即将其发送回猫鼬,以便我们尽可能快地获得响应。
Right after that, the cache server will also take the query issued, and add that on to its collection of queries that have been issued and take the result of the query and store it right up against the query.
紧接着,缓存服务器还将接收发出的查询,并将其添加到其已发出的查询集合中,并获取查询的结果并将其直接存储在查询中。
So we can imagine that in the future we issue the same query again, it hits the cache server, it looks at all the keys it has and says oh I already found that blogpost, it doesn't reach out to mongo, it just takes the result of the query and sends it directly to mongoose.
所以我们可以想象,将来我们再次发出相同的查询,它访问缓存服务器,它查看它拥有的所有键并说哦,我已经找到了那篇博文,它没有联系 mongo,它只需要查询的结果并将其直接发送给猫鼬。
We are not doing complex query logic, no indices, nothing like that. Its as fast as possible. Its a simple key value lookup.
我们没有做复杂的查询逻辑,没有索引,没有类似的东西。它尽可能快。它是一个简单的键值查找。
Thats an overview of how the cache server (Redis) works with MongoDB.
这是缓存服务器 (Redis) 如何与 MongoDB 一起工作的概述。
Now there are other concerns. Are we caching data forever? How do we update records?
现在还有其他担忧。我们会永远缓存数据吗?我们如何更新记录?
We don't want to always be storing data in the cache and be reading from the cache.
我们不想总是将数据存储在缓存中并从缓存中读取。
The cache server is not used for any write actions. The cache layer is only used for reading data. If we ever write data, writing will always go over to the mongodb instance and we need to ensure that anytime we write data we clear any data stored on the cache server that is related to the record we just updated in Mongo.
缓存服务器不用于任何写入操作。缓存层仅用于读取数据。如果我们曾经写入数据,写入将始终转到 mongodb 实例,我们需要确保无论何时我们写入数据,我们都会清除缓存服务器上存储的与我们刚刚在 Mongo 中更新的记录相关的任何数据。