The 100MB RAM Stage Limit & allowDiskUse: true
Aggregation pipeline stages are constrained by a strict 100MB in-memory RAM limit; learn how to diagnose memory spillover and utilize allowDiskUse: true safely.
Aggregation Pipeline Multi-Stage Stream Stepper
Filter completed orders, aggregate total revenue by payment method, and sort highest first.
The pipeline begins by streaming documents from the orders collection into the memory pipeline buffer.
1. Formal Definition & Architecture Intent
MongoDB enforces a 100MB RAM ceiling per stage in an aggregation pipeline to prevent memory starvation and OOM crashes. If a blocking stage (such as $group, unindexed $sort, or $bucket) accumulates more than 100MB of data in RAM, MongoDB aborts the query with an error unless { allowDiskUse: true } is explicitly enabled.
2. Explain Like I'm 5 (Mental Model)
Each station on the assembly line is only given a 100MB workbench. If parts pile up past 100MB, the worker throws their hands up and stops the factory. Setting allowDiskUse gives the worker permission to stack overflow parts into temporary storage boxes on the warehouse floor.
3. Real-World Architecture Example
Sorting 5,000,000 unindexed audit log records in an aggregation pipeline.
{
"code": 16819,
"errmsg": "Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting."
}Unindexed sort breached 100MB (104,857,600 bytes), crashing the query.
4. MQL Implementation Steps
db.audit_logs.aggregate(
[
{ $match: { severity: "CRITICAL" } },
{ $sort: { timestamp: -1 } } // Unindexed sort
],
{ allowDiskUse: true } // Permits writing temp files to disk!
);5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| Exact Limit | 104,857,600 bytes (100MB) per pipeline stage. |
| Performance Trade-off | Spilling to disk avoids query abortion, but disk I/O degrades execution speed by 10x-50x. |
6. Deep Dive: MongoDB 6.0+ Pipeline Spill Changes
In MongoDB 6.0+, pipeline stages that spill to disk write temporary files to the storageEngine directory. However, relying on allowDiskUse in user-facing online web applications is an architectural anti-pattern. If an API query hits disk spillover, customer response times jump from 50ms to 4,000ms. Disk use should be reserved for offline nightly ETL jobs.
// Check if query spilled to disk in explain():
const exp = db.orders.explain("executionStats").aggregate([...], { allowDiskUse: true });
// Look for "usedDisk": true in stage stats7. Architectural Trade-offs
- •Protects the mongod server from running out of memory (OOM) due to runaway analytical queries
- •Enables massive multi-gigabyte analytical reporting jobs to complete without failure
- •Spill files are cleaned up automatically upon pipeline termination
- •Writing and reading temporary files to disk severely degrades query execution speed
- •Increases storage IOPS contention on active production primary nodes
8. Technical Interview Questions
The default memory limit is 100MB (104,857,600 bytes) per stage. If a blocking stage (like $sort or $group) exceeds this limit, the query terminates with an error (code 16819: Sort exceeded memory limit). To allow the query to complete by writing temporary spillover files to disk, the client must pass { allowDiskUse: true }.
9. Staff-Level Ready Answer
"MongoDB limits each aggregation stage to 100MB of RAM to prevent OOM server failure. Blocking stages exceeding this threshold fail with error 16819 unless { allowDiskUse: true } is supplied, which writes temporary files to disk. In production, index the sort/match fields rather than relying on disk spill in user-facing APIs."
10. Quick Knowledge Check
What is the exact memory limit for a single aggregation pipeline stage in MongoDB before allowDiskUse is required?