How to get Buffer cache Hit ratio / Recommended ratio

Hello All,

How to get the buffer/cache pool hit ratio in MongoDB as we do it in DB2 and MySQL?
What is the good ratio of buffer/cache hit ratio for best performance?

Thank you in advance.

Hey, welcome to the MongoDB community!

In MongoDB, the buffer/cache hit rate refers to the efficiency with which the in-memory data cache (WiredTiger in the case of the default storage engine) is utilized. A high hit rate typically indicates that most read operations are being served from the in-memory cache, which is faster than reading from disk.

How to obtain the buffer/cache hit rate in MongoDB:

To get statistics on cache performance in MongoDB when using the WiredTiger storage engine, you can use the serverStatus command:

db.serverStatus( { "wiredTiger.cache": 1 } )

Within the wiredTiger.cache section, you’ll find various cache-related statistics. Statistics of interest include:

  • bytes currently in the cache
  • tracked dirty bytes in the cache
  • bytes read into cache
  • bytes written from cache

The hit rate can be approximated from these statistics, but MongoDB does not provide a direct “cache hit rate” like some other databases.

What is a good buffer/cache hit rate ratio for best performance?

In general, a cache hit rate above 95% is considered good for most workloads. This means that 95% or more of the read operations are being served from the cache instead of going to disk. However, the exact value considered “good” may vary based on the nature of your workload and your system’s configuration.

If the cache hit rate is consistently below 95%, it may be an indication that:

  1. Your working set is larger than the allocated cache.
  2. Your working set has an access pattern that is not well-optimized for caching (e.g., very random reads across a large dataset).

In these cases, you might consider:

  1. Increasing the RAM available to MongoDB to increase the cache size.
  2. Optimizing your access patterns (e.g., using efficient indexes).
  3. Reviewing and optimizing your queries and data access patterns.
3 Likes

Hi, thanks for the explanation Samuel_84194. However, it’s unclear to me how we can calculate the hit ratio using these statistics. What would be the logic used?

  • bytes currently in the cache
  • tracked dirty bytes in the cache
  • bytes read into cache
  • bytes written from cache

Best Regards,

Rodrigo Lima.

Hello Rodrigo.

In the db.serverStatus().wiredTiger.cache command you will see some metrics. To perform the calculation we use the pages read in the cache (Pages Read Into Cache) and the total number of requests to the cache (Total Pager Requests).

The calculation is done as:

Cache hit rate = (1 - (Pages read from cache / Total requests)) * 100%

E.g
pages read into cache: 30
pages requested from the cache: 218

Cache hit rate = (1 - (30 / 218)) * 100%
Cache hit rate = 86.24%

1 Like

Hi, Samuel. Thanks for the help. :slight_smile: