This blog demonstrates how to increase mongo WiredTiger cache limits in case when your applications grows and you start to see bottlenecks as the data doesn't fit into the memory. Or decrease the limits if you have to share the memory with other resident applications in shared resource environment.

How to get current cache configuration

You can get the current cache limits and utilizations using serverStatus.

Command to see the wiredTiger cache usage statistics: db.serverStatus().wiredTiger.cache

db.serverStatus().wiredTiger.cache
{ 'application threads page read from disk to cache count': 45,
  'application threads page read from disk to cache time (usecs)': 0,
  'application threads page write from cache to disk count': 0,
  'application threads page write from cache to disk time (usecs)': 0,
  'bytes belonging to page images in the cache': 811402,
  'bytes currently in the cache': 42431646,
  'bytes not belonging to page images in the cache': 41620244,
  'bytes read into cache': 751299,
  'bytes written from cache': 207861165,
  'checkpoint blocked page eviction': 0,
  'eviction calls to get a page': 5844,
  'eviction calls to get a page found queue empty': 5841,
  'eviction calls to get a page found queue empty after locking': 0,
  'eviction currently operating in aggressive mode': 0,
  'eviction empty score': 0,
  'eviction server candidate queue empty when topping up': 0,
  'eviction server candidate queue not empty when topping up': 0,
  'eviction server evicting pages': 0,
  'eviction server slept, because we did not make progress with eviction': 0,
  'eviction server unable to reach eviction goal': 0,
  'eviction state': 16,
  'eviction walks abandoned': 0,
  'eviction worker thread active': 0,
  'eviction worker thread created': 0,
  'eviction worker thread evicting pages': 0,
  'eviction worker thread removed': 0,
  'eviction worker thread stable number': 0,
  'failed eviction of pages that exceeded the in-memory maximum': 0,
  'files with active eviction walks': 0,
  'files with new eviction walks started': 0,
  'force re-tuning of eviction workers once in a while': 0,
  'hazard pointer blocked page eviction': 0,
  'hazard pointer check calls': 6,
  'hazard pointer check entries walked': 12,
  'hazard pointer maximum array length': 2,
  'in-memory page passed criteria to be split': 12,
  'in-memory page splits': 6,
  'internal pages evicted': 0,
  'internal pages split during eviction': 0,
  'leaf pages split during eviction': 0,
  'lookaside table insert calls': 0,
  'lookaside table remove calls': 0,
  'maximum bytes configured': 268435456,
  'maximum page size at eviction': 0,
  'modified pages evicted': 0,
  'modified pages evicted by application threads': 0,
  'overflow pages read into cache': 0,
  'overflow values cached in memory': 0,
  'page split during eviction deepened the tree': 0,
  'page written requiring lookaside records': 0,
  'pages currently held in the cache': 104,
  'pages evicted because they exceeded the in-memory maximum': 6,
  'pages evicted because they had chains of deleted items': 0,
  'pages evicted by application threads': 0,
  'pages queued for eviction': 0,
  'pages queued for urgent eviction': 0,
  'pages queued for urgent eviction during walk': 0,
  'pages read into cache': 91,
  'pages read into cache requiring lookaside entries': 0,
  'pages requested from the cache': 335673,
  'pages seen by eviction walk': 0,
  'pages selected for eviction unable to be evicted': 0,
  'pages walked for eviction': 0,
  'pages written from cache': 20839,
  'pages written requiring in-memory restoration': 0,
  'percentage overhead': 8,
  'tracked bytes belonging to internal pages in the cache': 118185,
  'tracked bytes belonging to leaf pages in the cache': 42313461,
  'tracked dirty bytes in the cache': 80749,
  'tracked dirty pages in the cache': 2,
  'unmodified pages evicted': 0 }

By default, the wiredTiger has a maximum cache usage ('maximum bytes configured' ) set to 50% of the available memory - 1GB or 256MB which ever is more, for example if your system has 16GB of memory then wiredTiger could take up to 7.5Gb ( (16-1)/2 )

If you want to increase or decrease the maximum size of WiredTiger cache then we can set the 'storage.wiredTiger.engineConfig.cacheSizeGB' in mogod.conf

storage:
   wiredTiger:
       engineConfig:
           cacheSizeGB: 10

After setting the desired value restart mongo services and you can see the limits are set by running the db.serverStatus().wiredTiger.cache.

You can look at the actual data size your mongodb is using and based on this information you can make an educated decision what to set the WiredTiger cache limits to.

db.runCommand({dbStats:1})

{ db: 'testdb',
  collections: 14,
  views: 0,
  objects: 123456,
  avgObjSize: 999,
  dataSize: 10000000,
  storageSize: 2000000,
  numExtents: 0,
  indexes: 26,
  indexSize: 7500000,
  ok: 1 }