Question: How can you disable caching in MongoDB?


MongoDB's performance largely benefits from its default behavior of caching frequently accessed data in memory. However, certain scenarios might require you to limit or disable this caching to closely control the system's memory usage or for testing purposes. It's important to note that completely disabling MongoDB's built-in cache is not directly supported because it's integral to its operation and performance. Instead, you can manage and limit the cache size.

Limiting WiredTiger Cache Size

MongoDB uses the WiredTiger storage engine by default, which controls its cache size based on the available system memory. You can limit this cache size through the --wiredTigerCacheSizeGB parameter when starting mongod, or by setting storage.wiredTiger.engineConfig.cacheSizeGB in the configuration file.

For example, to limit the cache size to 2 GB using the command line:

mongod --wiredTigerCacheSizeGB 2

Or, in a MongoDB configuration file (typically named mongod.conf):

storage: wiredTiger: engineConfig: cacheSizeGB: 2

After setting this parameter, restart your MongoDB server for the changes to take effect.

Disabling Query Caching

MongoDB also caches the plans for frequently run queries. Although this is not directly related to data caching in memory, if your concern involves the caching of query execution plans, you can affect this by using the planCacheClear command on a per-collection basis.

To clear a collection's query plan cache:

db.collectionName.runCommand({ planCacheClear: "collectionName" })

Remember, managing cache sizes and understanding their implications is crucial for maintaining the desired balance between performance and resource utilization. Adjusting cache settings without proper benchmarking and testing can lead to degraded performance.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today

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