Question: How do you increase the cache size in MongoDB?


In MongoDB, the WiredTiger storage engine is the default storage engine starting from version 3.2. One of the configuration options for WiredTiger is the cache size, which dictates how much data the database keeps in memory. The cache size can significantly affect performance, as a larger cache can improve read speeds by reducing disk I/O.

To increase the cache size in MongoDB, you will need to adjust the storage.wiredTiger.engineConfig.cacheSizeGB setting in your MongoDB configuration file (mongod.conf), or pass it as a command-line option when starting the MongoDB server.

Example: Setting Cache Size in the Configuration File

  1. Open your mongod.conf file with a text editor.
  2. Locate the storage section and add/update the wiredTiger section with the desired cache size. For example, to set the cache size to 4 GB:
storage: wiredTiger: engineConfig: cacheSizeGB: 4
  1. Save the changes and restart your MongoDB server for the changes to take effect.

Example: Setting Cache Size via Command Line

Alternatively, you can specify the cache size when starting mongod from the command line:

mongod --wiredTigerCacheSizeGB 4

This command starts MongoDB with a WiredTiger cache size of 4 GB.

Best Practices

  • Monitor Performance: After increasing the cache size, monitor your system's performance. A larger cache requires more system RAM. Ensure that your server has enough physical memory to accommodate the increased cache size without swapping to disk, as this can degrade performance.
  • Balance Resources: Consider other applications running on the same server. Make sure there is enough memory left for the operating system and any other applications to avoid contention.

Adjusting the cache size can be a powerful way to enhance MongoDB's performance, but it should be approached with an understanding of your application's specific needs and the available system resources.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.