Introducing Dragonfly Cloud! Learn More

Question: How does the allowDiskUse option affect performance in MongoDB?


In MongoDB, operations that require a sort operation but cannot perform the sort in memory due to the size of the data involved, will fail unless the allowDiskUse option is enabled. This option, when set to true, allows MongoDB to write data to temporary files on the disk as part of aggregation pipeline stages like $group, $sort, or $lookup that exceed the 100 megabyte memory limit.

Understanding the Impact on Performance

Using allowDiskUse can have a significant impact on the performance of your queries. Here are key points to consider:

  1. Memory vs. Disk Access: Disk access is significantly slower than memory access. When MongoDB resorts to using disk space for operations that exceed memory limits, this introduces a performance penalty due to the slower read/write speeds of disks compared to RAM.

  2. IO Load: Enabling allowDiskUse can increase the IO load on the server, especially if multiple queries or operations use this option concurrently. This can lead to overall system slowdowns, affecting not just the database but other applications running on the same server.

  3. Use Cases: For certain large datasets and complex aggregations, allowing disk use might be the only practical way to execute a query. In these cases, it's a trade-off between slower performance and the ability to process large quantities of data.

  4. Optimization: Before resorting to allowDiskUse, consider optimizing your query or schema. Indexes, better schema design, or breaking down the query into smaller parts might help avoid exceeding the memory limit without needing to use disk space.

Example Usage

db.collection.aggregate([ // Your aggregation stages here ], { allowDiskUse: true })

This enables operations within the aggregation pipeline to spill over to disk if they exceed the memory limitations.


While allowDiskUse can enable you to run operations on large datasets that wouldn't be possible otherwise due to memory constraints, it is essential to consider the impact on performance. Disk-based operations are slower and can increase the load on your system. Optimizing queries and considering the architecture of your solution can often mitigate the need for disk use, preserving performance while still enabling you to process substantial amounts of data.

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.