Introducing Dragonfly Cloud! Learn More

Question: How does MongoDB manage memory?

Answer

MongoDB uses a memory-mapped files approach for its data management. This means MongoDB leverages the virtual memory subsystem of the operating system to cache data, which allows it to efficiently handle databases that are larger than the available physical RAM in the system.

Memory Management Strategy

MongoDB's memory management is closely tied to how the underlying operating system handles memory. Here's an outline of the process:

  1. Memory-Mapped Files: MongoDB maps its data files directly into virtual memory. When data is queried, MongoDB relies on the operating system to load the necessary parts of these files into physical RAM if they are not already present.

  2. Page Faults: When MongoDB accesses data that is not in RAM, a page fault occurs, prompting the OS to load the required data from disk into RAM. High rates of page faults may indicate that the working set (the portion of data being actively used) exceeds the physical memory, which can degrade performance.

  3. Working Set Size: For optimal performance, it’s important to have a working set size that fits within the server's physical memory. The working set includes all the data and indexes that MongoDB needs for regular operations.

  4. Cache Size Limitation: In MongoDB, the WiredTiger storage engine (default storage engine since version 3.2) has configurations that allow you to specify the maximum amount of memory dedicated to the cache. By default, WiredTiger will use up to 50% of available RAM (minus 1 GB) for its cache. You can adjust this limit based on your application's needs using the storage.wiredTiger.engineConfig.cacheSizeGB setting.

Monitoring and Adjusting Memory Usage

To ensure MongoDB performs efficiently, it's important to monitor your system's memory usage. Tools like mongostat, mongotop, and various monitoring solutions provided by MongoDB, such as MongoDB Atlas, can help identify performance bottlenecks.

If you find your working set does not fit into RAM, consider adding more memory to your server or optimizing your indexes to reduce the size of the working set.

Example: Setting WiredTiger Cache Size

db.adminCommand({ "setParameter": 1, "wiredTigerEngineRuntimeConfig": "cache_size=2GB" });

This command changes the WiredTiger cache size limit to 2GB dynamically. Note that changes made with setParameter are temporary and will revert on restart; permanent changes should be made in the MongoDB configuration file.

Conclusion

MongoDB's memory management is designed to work efficiently with large datasets by utilizing the operating system's virtual memory subsystem. By understanding and monitoring your database's memory usage and adjusting settings like the WiredTiger cache size, you can optimize MongoDB's performance to suit your application's needs.

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.