35

I would like to know if Memcache is recommended when using a NoSQL database like mongoDB.

krisk
  • 6,957
  • 1
  • 18
  • 30
Daniel Vigueras
  • 361
  • 1
  • 3
  • 6

4 Answers4

60

The concept of using memcache stems from the idea that you have "extra RAM" sitting around somewhere. Both MongoDB and MySQL (and most DBs) will take every meg of RAM that they can get.

In the case of the very common MySQL / Memcache, it is very well documented that using Memcache is more about reducing query load on the server than it is about speeding up queries. A good memcache implementation basically just tries to keep the most common data in memory so that the database server can churn away on bigger stuff.

In fact, it's been my experience that use of memcache generally becomes a reliance on memcache to maintain system performance.

So back to the original question, where do you have extra RAM?

If you have extra RAM on web servers, you may be able to use Memcache. Of course, you could also run Mongo locally on the web server. Just slave the data you need from the master.

If you have extra RAM on other computers, then there's not really a point in using memcache. Just add more nodes to your MongoDB replica set or shard. This is where MongoDB actually shines. Because of sharding / replication, you can add more RAM to Mongo Horizontally to increase performance. With SQL it's very difficult to "just add more servers" because joins don't scale very well. But with Mongo, it's quite possible to simply "add more nodes" to a problem.

Gates VP
  • 44,957
  • 11
  • 105
  • 108
11

MongoDB stores everything in memory anyway and works in a similar vein, being a key-value based system, however I believe MongoDB is more flexible, as it allows for storing BSON objects within themselves.

(Just for clarification, MongoDB uses BSON, a specialised form of JSON, for storing all its data, which includes objects within objects.)

Arantor
  • 597
  • 4
  • 15
  • 3
    That feature makes me like mongoDB more than other key-value databases :) – Daniel Vigueras Nov 25 '10 at 14:21
  • 4
    This answer is inaccurate, MongoDB is not a key-value store and it doesn't house everything in RAM, it mmaps to Virtual Memory which is quite different. – Sammaye Feb 25 '13 at 16:05
3

At first no. If you run into performance problems later add a caching layer (memcache). But you won't gain anything if you're going to use Redis for example, as Redis already stores everything in memory.

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
1

The answer would depend on your use cases.
In general, accessing RAM is orders of magnitude faster than accessing disk.
Even the fastest SSD drives are about 100 times slower to access than RAM.

Now, I don't know if Mongo has a caching system in place (most likely it does), or what the eviction policy is, but as a programmer i would prefer a cache where i can store/retrieve and delete items at will. Therefore i would prefer using a caching solution even with Mongo.

In summary, it really depends what you are using these solutions for. There is no one answer to cover all possible uses.

Albert S
  • 2,552
  • 1
  • 22
  • 28