12

Can we use MongoDB as a replacement of Memory Caching library (memcache and memcached).?

and what are the deference in MongoDB and Memory Caching Library.

Ayaz
  • 274
  • 1
  • 3
  • 14
  • I think one difference is memory caching library provide time interval for keep object specific time, but with mongoDB we cant do that. – Ayaz Jan 11 '13 at 19:09
  • Not a bad question - there's definitely precedence in using MongoDB as cache. – Brian Knight Jan 11 '13 at 20:17
  • @CRUSADER Actually MongoDB was made with two case studies in mind. Both were as a storage layer but one was for a database replacement and the other was for a cache replacement. – Sammaye Jan 11 '13 at 20:57
  • @CRUSADER Questions are meant to seek answers, not make points. – raffian Jan 14 '13 at 16:09
  • 1
    This question is certainly on topic, look here for the very similar [Memcached vs. Redis](http://stackoverflow.com/questions/10558465/memcached-vs-redis) question. – dotancohen May 03 '15 at 13:00

3 Answers3

8

One of the main problems with MongoDB as a cache store is that it does not have expiration mechanism. More details about that you can find in this answer.

UPDATED Since MongoDB v2.2 it allows TTL for collections. Thanks to JohnnyHK for pointing.

I suggest to use Redis for caching and MongoDB as a database. They work nice together.

Community
  • 1
  • 1
Voldy
  • 12,436
  • 7
  • 47
  • 66
6

The biggest problem you have with MongoDB is that it doesn't naturally and forcefully load everything into memory unlike memcached, which, as in it's name, is a in-memory key value store.

You have also got to consider that MongoDB does no memory management for itself, it will instead allow the OS to take utter control of it using the native LRU algorithm. I cannot fathom any problems this might give you because I don't have your data or indexes or queries.

However many people who try to use memcache as a database (which many do) are finding that it isn't particularly fast at doing that (wasn't really designed for it) and this is where MongoDB breaks through.

Whether or not MongoDB is a replacement for memcache depends on your need, but then if you need is such that MongoDB needs to replace the cache then you might as well replace SQL with MongoDB as well, that is my honest opinion on the matter because you have blurred the lines between cache and storage.

Sammaye
  • 40,888
  • 7
  • 90
  • 139
4

Here's an example of some folks who have replaced memcached with MongoDB. Or at least obviated the need for memcached.

http://blog.serverdensity.com/removing-memcached-because-its-too-slow/

Brian Knight
  • 4,792
  • 24
  • 34