Joezer
Joezer

Reputation: 665

Can I configure MongoDB to be In-Memory?

I am interested in using a database that will allow high performance, with an expected requirement of cluster for a massive horizontal scaling.

We are looking into using MongoDB, does anyone know if I can use it InMemory (i.e. in the RAM - for performance reasons)?

Tnx

Upvotes: 8

Views: 26551

Answers (5)

Manjesh V
Manjesh V

Reputation: 1240

Try https://github.com/nodkz/mongodb-memory-server

this helps for testing

npm i mongodb-memory-server

Upvotes: 2

cptaffe
cptaffe

Reputation: 426

Using a tmpfs

At least on linux, since MongoDB uses memory mapped (mmap) files, you can set up a tmpfs system that resides in memory.

Here's a neat tutorial on setting up a tmpfs for MongoDB.

Memory mapped files

Memory mapped files are explained in more detail on their FAQ. It also says that MongoDB is automatically configured to use all available free memory on a system as its cache (link).

Conclusion

Basically, there is no module for a purely in-memory database, but by using an in-memory fs, one can emulate it.

On a side note, I did find MorboDB, a "In-memory database, mostly-compatible clone of MongoDB". Probably not useful to you, but I thought it interesting.

Upvotes: 6

Piyush Katariya
Piyush Katariya

Reputation: 765

As of today, latest version of MongoDB, i.e. v3.4 supports in memory engine in their Enterprise software edition. If you seek open source edition, Percona's MongoDB is way to go.

Mongo Enterprise edition : https://docs.mongodb.com/manual/core/inmemory/

Percona's MongoDB offering: https://www.percona.com/software/mongo-database/percona-memory-engine-for-mongodb

Upvotes: 10

user3382570
user3382570

Reputation:

A performance boost approach you can take is to use a RAM disk

e.g.:

mongod --smallfiles --noprealloc --nojournal --dbpath <ramdisk mounted localtion>

See also:

Upvotes: 8

Markus W Mahlberg
Markus W Mahlberg

Reputation: 20683

Actually, it is quite easy to do so. Simply set syncPeriodSecs to 0 and disable journaling. In order to prevent most files to be created, simply start mongod with

mongod --noprealloc --nojournal 

or the equivalent options configured in mongod.conf.

However, namespace files will be created whatever you do. In case you use a replica set, oplog files will be created, too.

In order to make sure that your mongodb will not eat up all available RAM and inserts don't throw an exception under those circumstances, you might want to have a look at capped collections.

Upvotes: 2

Related Questions