Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it practical to use Mongo's capped collection a memory cache?

Tags:

mongodb

Is it practical to use Mongo's capped collections as poor man's memcache assuming it will be kept in memory most of the time?

like image 620
Andrewz Avatar asked Sep 30 '11 18:09

Andrewz


People also ask

What is the use of capped collection in MongoDB?

Capped collection in MongoDB supports high-throughput operations used for insertion and retrieval of documents based on the order of insertion. Capped collection working is similar to circular buffers, i.e., there is a fixed space allocated for the capped collection.

Which is the data type used by capped collection?

capped: It is used to create a capped collection. If the value of this field is true then you must specify the maximum size in the size field. It is of boolean type.

Which is the method used to check whether collection is capped or not?

Explanation: Use the isCapped() method to determine if a collection is capped, as: db. collection. isCapped().


2 Answers

You can definitely use capped collections for this purpose. But it's important to know the basic limitations.

  1. Data will expire based on insert order not time-to-expire
  2. Data that is frequently accessed may still be pushed out of memory
  3. _id columns are not defined on Capped Collections by default, you will need to ensure those indexes.
  4. The objects cannot be modified in a way that changes the size of the object. For example, you could increment an existing integer, but you cannot add a field or modify a string value on the entry.
  5. The capped collections cannot be sharded.

Because of #1 & #4 & #5, you are definitely losing some the core Memcache functionality.

There is a long-outstanding JIRA ticket for TTL-based capped collections which is probably exactly what you want.

Of course, the big question in this whole debate is "where is the extra RAM". Many people who use MongoDB as their primary store simply drop Memcache. If you have a bunch of extra RAM sitting around, why not just use it store the actual data instead of copies of that data?

like image 129
Gates VP Avatar answered Oct 29 '22 10:10

Gates VP


Yes. This is a perfectly acceptable use of a MongoDB capped collection. I am assuming you will have many more reads than writes, so make sure you use an index as well.

MongoDB Capped Collections: Applications

like image 21
dbyrne Avatar answered Oct 29 '22 10:10

dbyrne