21

I've seen many people using Redis as a cache lately, why not Mongo? As far as I could tell Redis can set an expire date on an index, like memcache but otherwise are there any reasons not to use Mongo for this?

I ask as I'm doing a large join in MySQL and then changing the data after selecting it. I'm already using memcache on other parts of the site but saving this in Mongo would allow me to do geospatial searches on the cached data.

jfountain
  • 3,715
  • 2
  • 24
  • 22

3 Answers3

17

A lot of people do use MongoDB for a low-medium grade cache and it works just great.

Because it offers more functionality than a simple key value store via ad-hoc queryability it isn't as pure of a caching layer as a memcache or redis (it can be slower to insert and retrieve data).

Extremely high performance is attainable (the working set is in RAM after all), but the data model is heavier.

However, on the flip side, MongoDB does offer a persistance layer that makes a lot more sense (to most developers) for the type of data that is most likely needed at a later time, unlike Redis.

girasquid
  • 15,121
  • 2
  • 48
  • 58
Tyler Brock
  • 29,626
  • 15
  • 79
  • 79
  • 5
    Other MongoDB features like [capped collections](http://www.mongodb.org/display/DOCS/Capped+Collections) can also be helpful in caching layers. There is also a feature request for [configurable TTL collections](https://jira.mongodb.org/browse/SERVER-211) which will be very helpful for caching layers, though it is not yet implemented. – dcrosta Apr 25 '12 at 14:51
  • 6
    It's implemented now :) – wprl Sep 19 '12 at 17:37
  • configurable TTL collections: https://docs.mongodb.com/manual/tutorial/expire-data/ – Simon B. Nov 07 '17 at 13:18
  • Now MongoDB provides In-Memory Storage Engine, a good fit for caching https://docs.mongodb.com/manual/core/inmemory/#in-memory-storage-engine – Mark Jun 19 '19 at 14:16
11

The biggest difference between MongoDB and Redis is that Redis usually stores the entire database in memory. MongoDB uses a memory mapped file to pretend everything is in memory, and lets the OS page bits in and out of disk as necessary. If the OS can keep everything in memory, performance will be somewhat similar.

Joshua Martell
  • 7,074
  • 2
  • 30
  • 37
4

When we say caching, speed comes to mind. The goal here is to set and retrieve something as fast as possible. In this sense, redis is faster than mongodb. However, if you find that mongodb is suitable for doing geospatial searches on cached data, it's ok to use it. You can of course invest some time and implement the same in redis, and then benchmark to see what you gain.

Eve Freeman
  • 32,467
  • 4
  • 86
  • 101
hymloth
  • 6,869
  • 5
  • 36
  • 47