Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB with redis

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

like image 295
tonyl7126 Avatar asked May 22 '12 05:05

tonyl7126


People also ask

Can we use Redis with MongoDB?

First, we will add a cache configuration and connect our application to the Redis server, for this purpose we will add the Redis package from npm. Second, we need to add the logic that queries Redis, and if there is no answer it will query our MongoDB. After we got new data from MongoDB we'll store it in Redis.


2 Answers

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 is interesting for persistent, document oriented, data indexed in various ways. Redis is more interesting for volatile data, or latency sensitive semi-persistent data.

Here are a few examples of concrete usage of Redis on top of 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.

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.

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.

like image 92
Didier Spezia Avatar answered Sep 20 '22 09:09

Didier Spezia


Obviously there are far more 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.

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.

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.

Redis can be used as a replacement for a traditional datastore, but it's most often used with another normal "long" data store, like Mongo, Postgresql, MySQL, etc.

like image 36
Brian P O'Rourke Avatar answered Sep 20 '22 09:09

Brian P O'Rourke