Dragonfly Cloud announces new enterprise security features - learn more

Dragonfly

Question: How do you set a memory limit for MongoDBs WiredTiger storage engine?

Answer

MongoDB utilizes the WiredTiger storage engine by default, which is renowned for its performance and scalability. One of the aspects that contribute to its efficiency is the way it manages memory. However, in some scenarios, controlling the amount of memory WiredTiger consumes can be beneficial, especially in constrained environments or when running multiple services on the same machine.

Understanding Memory Use in WiredTiger

WiredTiger uses an internal cache to hold recently accessed data. By default, this cache is dynamically sized to use the lesser of either:

It means, for instance, on a system with 8 GB of RAM, WiredTiger aims to use up to approximately 3.4 GB as its internal cache (excluding the memory used by the MongoDB process itself and other overheads).

Setting a Memory Limit

To explicitly set a memory limit for WiredTiger, you can configure the storage.wiredTiger.engineConfig.cacheSizeGB option in your MongoDB configuration file (mongod.conf), or provide it as a command-line argument when starting mongod.

Using the Configuration File

Add or modify the storage section in your mongod.conf:

storage:
  wiredTiger:
    engineConfig:
      cacheSizeGB: <desired cache size in GB>

Replace <desired cache size in GB> with the maximum amount of memory in gigabytes you want WiredTiger to use for its cache.

Using Command-Line Arguments

Start mongod with the --wiredTigerCacheSizeGB option:

mongod --wiredTigerCacheSizeGB <desired cache size in GB> [...other options...]

Considerations

By carefully managing the cache size, you can ensure that MongoDB's WiredTiger storage engine efficiently utilizes memory resources without jeopardizing the performance and stability of your overall system.

Was this content helpful?

Other Common MongoDB Performance Questions (and Answers)

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

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost