Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

understand MongoDB cache system

This is a basic question, but very important, and i am not sure to really get the point.

On the official documentation we can read

MongoDB keeps all of the most recently used data in RAM. If you have created indexes for your queries and your working data set fits in RAM, MongoDB serves all queries from memory.

The part i am not sure to understand is

If you have created indexes for your queries and your working data set fits in RAM

what does mean "indexes" here?

For example, if i update a model, then i query it, because i have updated it, it's now in RAM so it will come from the memory, but this is not very clear in my mind.

How can we be sure that datas we query will come from the memory or not? I understand that MongoDB uses the free memory to cache datas about the memory which is free on the moment, but does someone could explain further the global behavior ?

In which case could it be better to use a variable in our node server which store datas than trust the MongoDB cache system?

How do you globally advise to use MongoDB for huge traffic?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Note: This was written back in 2013 when MongoDB was still quite young, it didn't have the features it does today, while this answer still holds true for mmap, it does not for the other storage technologies MongoDB now implements, such as WiredTiger, or Percona.


A good place to start to understand exactly what is an index: http://docs.mongodb.org/manual/core/indexes/

After you have brushed up on that you will udersand why they are so good, however, skipping forward to some of the more intricate questions.

How can we be sure that datas we query will come from the memory or not?

One way is to look at the yields field on any query explain(). This will tell you how many times the reader yielded its lock because data was not in RAM.

Another more indepth way is to look on programs like mongostat and other such programs. These programs will tell you about what page faults (when data needs to be paged into RAM from disk) are happening on your mongod.

I understand that MongoDB uses the free memory to cache datas about the memory which is free on the moment, but does someone could explain further the global behavior ?

This is actually incorrect. It is easier to just say that MongoDB does this but in reality it does not. It is in fact the OS and its own paging algorithms, usually the LRU, that does this for MongoDB. MongoDB does cache index plans for a certain period of time though so that it doesn't have to constantly keep checking and testing for indexes.

In which case could it be better to use a variable in our node server which store datas than trust the MongoDB cache system?

Not sure how you expect that to work...I mean the two do quite different things and if you intend to read your data from MongoDB into your application on startup into that var then I definitely would not recommend it.

Besides OS algorithms for memory management are extremely mature and fast, so it is ok.

How do you globally advise to use MongoDB for huge traffic?

Hmm, this is such a huge question. Really I would recommend you Google a little in this subject but as the documentation states you need to ensure your working set fits into RAM for one.

Here is a good starting point: What does it mean to fit "working set" into RAM for MongoDB?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...