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:

  • 50% of RAM minus 1 GB
  • Or, if the host has more than 2GB of RAM, then 60% of RAM over 2 GB plus 1 GB

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

  • Setting the cache size too low can hurt performance, as it may lead to increased I/O due to reduced hit rates in the cache.
  • It's crucial to balance the cache size with the needs of other processes and the overall system requirements.
  • The effective memory usage by MongoDB will also include other factors such as connections and indexes, so the total memory footprint will be larger than just the WiredTiger cache.

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?

Start building today

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