Introducing Dragonfly Cloud! Learn More

Question: How does MongoDB WiredTiger manage memory usage?


WiredTiger, the default storage engine for MongoDB since version 3.2, is designed for high performance, scalability, and efficient memory usage. Understanding how WiredTiger manages memory is crucial for optimizing your MongoDB deployments.

Memory Allocation

WiredTiger primarily uses two pools of memory: the cache and the lookaside table.

  • Cache: The main memory pool where data is stored temporarily. WiredTiger's cache is configured as a percentage of the system's RAM, with the default setting allocating 50% of the available physical memory to the WiredTiger cache, but not exceeding 1 GB on systems with more than 2 GB of RAM. This behavior ensures that there is enough memory for both MongoDB and the underlying operating system.

    To configure the cache size, you can adjust the storage.wiredTiger.engineConfig.cacheSizeGB parameter in MongoDB's configuration file or through command-line options.

  • Lookaside Table: A mechanism used by WiredTiger to maintain performance even when working sets exceed the cache size. It stores older versions of records outside the main cache to allow transactions to access historical data without needing to keep everything in the primary memory area, thus helping to manage cache overflow.

Memory Usage Optimization Tips

  1. Monitor Performance: Use MongoDB monitoring tools to keep an eye on your cache hit/miss ratios and page eviction rates. These metrics can help determine if your cache is adequately sized for your workload.

  2. Adjust Cache Size Appropriately: If you have memory to spare and notice frequent cache evictions or poor read performance, consider increasing the cache size. Conversely, if your system struggles with available memory, reducing MongoDB's cache size might help.

  3. Use Indexes Efficiently: Indexes are kept in the cache as well. Ensuring your queries are well-indexed means less data needs to be loaded into memory, improving overall efficiency.

  4. Consider Workload Types: Read-heavy workloads might benefit from a larger cache size, while write-heavy environments could perform better with a smaller cache, as it reduces the overhead of managing cache evictions.

  5. Balance with Other Applications: If MongoDB shares the server with other applications, carefully balance the memory requirements to avoid competition for resources, which could degrade performance across the board.

For specific configurations and optimizations, always refer to MongoDB's official documentation and consider running tests in a controlled environment before applying changes to production systems.

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.