How to Fix MongoDB WiredTiger Cache Size Issues
In this tutorial, you'll learn about How to Fix MongoDB WiredTiger Cache Size Issues. We cover key concepts, practical examples, and best practices.
The Problem
MongoDB performance degrades, writes slow down, or you see WiredTiger: (cache checkpoint) and cache pressure in logs. The WiredTiger storage engine's internal cache is too small for your working set, causing excessive page evictions and disk I/O. When the cache can't hold the active data set, every read requires a disk access.
Quick Fix
1. Check current cache statistics
mongosh --eval "db.serverStatus().wiredTiger.cache"
Look for these key metrics:
"tracked dirty bytes in the cache": 2147483648,
"pages read into cache": 50000000,
"pages written from cache": 30000000,
"unmodified pages evicted": 15000000
High eviction counts indicate cache pressure.
2. Check the current WiredTiger cache size
mongosh --eval "db.serverStatus().wiredTiger.cache['maximum bytes configured']"
This returns the cache size in bytes. Divide by 1073741824 to get GB.
3. Increase WiredTiger cache size
Edit /etc/mongod.conf:
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 4
Restart MongoDB:
sudo systemctl restart mongod
The default is 50% of RAM minus 1 GB, or 256 MB for systems with less than 2 GB.
4. Calculate cache pressure ratio
mongosh --eval "
const cache = db.serverStatus().wiredTiger.cache;
const evicted = cache['pages evicted by application threads'];
const read = cache['pages read into cache'];
const pressure = (evicted / read * 100).toFixed(2);
print('Cache pressure: ' + pressure + '%');
"
If pressure exceeds 20-30%, increase the cache.
5. Monitor evictions in real time
mongostat --noheaders 5 | grep -E "faults|evict"
High faults values mean the cache is too small for the working set.
6. Optimize indexes to reduce cache pressure
# Check index usage
mongosh --eval "
db.adminCommand({listDatabases:1}).databases.forEach(dbInfo => {
const db = db.getSiblingDB(dbInfo.name);
db.getCollectionNames().forEach(coll => {
db.getCollection(coll).getIndexes().forEach(idx => {
print(dbInfo.name + '.' + coll + ': ' + idx.name);
});
});
});
"
Remove unused indexes:
db.mycollection.dropIndex('unused_index_name')
Common Causes
| Cause | Symptom | Fix |
|---|---|---|
| Default cache too small | 50% of RAM minus 1 GB may not be enough | Increase cacheSizeGB in config |
| Working set exceeds cache | High page eviction rate | Add indexes or increase cache |
| Too many indexes | Indexes consume cache space | Drop unused indexes |
| Poor query patterns | Full collection scans evict useful pages | Optimize queries with explain() |
| Memory competition with other processes | OS and other services use RAM | Dedicate more RAM to MongoDB or reduce cache |
Additional Troubleshooting
# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"
If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.
Prevention
- Set
cacheSizeGBto 50-80% of available RAM, leaving enough for the OS and other processes - Monitor
wiredTiger.cachestatistics in your monitoring system - Ensure your working set fits in the WiredTiger cache
- Add indexes to reduce the number of documents scanned per query
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro