How MongoDB store indexes

Hey!
I wonder how MongoDB is storing the indexes?
( I’m using the default storage engine)
The indexes are stored in the disk and read to the RAM for every query?
Or by default, the indexes are store in the RAM until there is no available storage left and then transfer to the disk?

Thanks

I would start my hunt at https://docs.mongodb.com/manual/faq/storage/.

Short answer:
Indicies have an in-memory and storage presence. Update are frequently written to storage(~100ms or less).

Longer answer see @steevej’s answer.

1 Like

Indexes live in RAM but are written to disk frequently so you don’t have to rebuild them if you restart your mongod. For a healthy and happy mongod, all the indexes must fit in RAM.

You also need RAM for your frequently accessed documents (so you don’t have to fetch them from the disk each time - this is what we refer to as the “Working Set”)

And finally you need some RAM for your queries and pipelines and eventually in-memory sort operations (not great).

See https://docs.mongodb.com/manual/tutorial/ensure-indexes-fit-ram/.

2 Likes

Chipping in with another short answer:

To WiredTiger, indexes and collections are not very different and they are treated the same way once they’re loaded into RAM (at least in the current MongoDB version). They’re just stored physically in different WiredTiger “table type” to optimize for snappy compression (for collection) or prefix compression (for indexes). Once loaded into the WiredTiger cache, both of them have a different representation vs. on disk.

For WiredTiger to work on anything, they would have to be loaded into RAM (WT cache) first. The OS will take care of caching them in the filesystem cache.

Hopefully I’m not introducing any confusion with regard to previous replies :slight_smile:

Best regards,
Kevin

5 Likes