带有 redis 的 MongoDB [英] MongoDB with redis

查看:33
本文介绍了带有 redis 的 MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能举例说明什么时候将 Redis 和 MongoDB 结合使用会受益?

Can anyone give example use cases of when you would benefit from using Redis and MongoDB in conjunction with each other?

推荐答案

Redis和MongoDB可以一起使用,效果不错.一家以运行 MongoDB 和 Redis(以及 MySQL 和 Sphinx)而闻名的公司是 Craiglist.请参阅此演示文稿来自 Jeremy Zawodny.

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 presentation from Jeremy Zawodny.

MongoDB 对于持久的、面向文档的、以各种方式索引的数据很有趣.Redis 对于易失性数据或延迟敏感的半持久性数据更有趣.

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.

  • 2.2 之前的 MongoDB 还没有过期机制.Capped collections 不能真正用于实现真正的 TTL.Redis 有基于 TTL 的过期机制,方便存储易失数据.例如,用户会话通常存储在 Redis 中,而用户数据将存储和索引在 MongoDB 中.请注意,MongoDB 2.2 在集合级别引入了低精度过期机制(例如用于清除数据).

  • 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 提供了一种方便的集合数据类型及其相关操作(联合、交集、多个集合的差异等...).在此功能之上实现基本的分面搜索或标记引擎非常容易,这是对 MongoDB 更传统的索引功能的有趣补充.

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 支持对列表进行高效的阻塞弹出操作.这可以用来实现一个 ad-hoc 分布式排队系统.它比 MongoDB 可尾游标 IMO 更灵活,因为后端应用程序可以监听多个超时队列,原子地将项目传输到另一个队列等......如果应用程序需要一些排队,则将队列存储在 Redis 中是有意义的,并将持久化的函数数据保存在 MongoDB 中.

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 还提供了发布/订阅机制.在分布式应用程序中,事件传播系统可能很有用.这又是 Redis 的一个很好的用例,而持久性数据保存在 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.

因为使用 MongoDB 设计数据模型比使用 Redis 容易得多(Redis 更底层),有趣的是从 MongoDB 对主要持久数据的灵活性以及 Redis 提供的额外功能中受益(低延迟、项目过期、队列、发布/订阅、原子块等...).这确实是一个很好的组合.

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.

请注意,您永远不应该在同一台机器上运行 Redis 和 MongoDB 服务器.MongoDB 内存被设计为可以换出,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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆