My MongoDB instance keeps running out of RAM and then crashes. I checked the collection sizes and they are not that big. What else could be causing such high memory pressure? I suspect it might be my index usage, but I am not sure how to confirm that.
High MongoDB memory usage is typically caused by unindexed collection scans, inefficient aggregation pipelines, or oversized working sets that exceed available RAM, necessitating index optimization and query refactoring.
4 answers
To determine if your indexing strategy is the root cause, you must systematically evaluate the following diagnostic indicators:
- Check your logs for the COLLSCAN tag which indicates a scan of the entire collection.
- Review the database profiler to identify queries exceeding the expected duration.
- Monitor the wiredTiger cache statistics to see if the cache is being evicted frequently.
- Analyze schema design patterns to ensure you are not joining large sets in memory.
You should investigate unindexed queries performing full collection scans alongside excessive connection pooling. When MongoDB is forced to load documents into the wiredTiger cache for sorting or filtering without proper indexing, RAM consumption spikes rapidly and leads to page faults that inevitably crash the process.
I once saw a production cluster fall over because a junior developer pushed a reporting script that performed aggregations on a massive dataset without a single index in sight. The memory usage was perfectly fine until that job hit the wire and forced the entire working set into RAM.
You are almost certainly dealing with a runaway aggregation pipeline or a lack of compound indexes for your most common query patterns. Do not underestimate how quickly a few missing indexes can blow out your buffer pool and kill your node.
The choice between increasing your RAM or optimizing your indexes depends heavily on your query patterns. Increasing hardware headroom is a temporary fix that masks the underlying inefficiency of scanning entire collections, whereas optimizing your indexing strategy is the only sustainable way to prevent the wiredTiger engine from exhausting system resources during heavy operations. While scaling up might resolve the immediate crash, it will only delay the inevitable bottleneck if your application logic forces excessive read-ahead operations into the cache.

