skip to content

With WiredTiger, what do the internal cache and filesystem cache each hold, including index pages, and why does MongoDB's default leave room for the second?

level: middleimportance: should knowfreq 38%

answer

  1. two layers, two owners
  2. working form against stored form
  3. indexes keep one kind of compression
  4. a miss need not reach the disk

basics

~20 s

WiredTiger's internal cache holds collection pages uncompressed and index pages in an in-memory form that keeps prefix compression. The OS filesystem cache holds the on-disk, compressed blocks. The default leaves RAM free so those blocks avoid disk reads.

solid answer

~50 s

MongoDB uses two layers of memory. The internal cache belongs to WiredTiger and is sized by `cacheSizeGB`: collection data there is uncompressed, because the server must work on it, and index pages use an in-memory representation that can still benefit from prefix compression. The filesystem cache belongs to the operating system and holds data in the on-disk format, including block compression. When a page misses the internal cache, WiredTiger reads its block, and if the block is already in the filesystem cache no disk I/O happens; it only decompresses and rebuilds the page. Because that layer holds compressed data, it can keep more data per gigabyte. The default leaves roughly half of RAM outside the internal cache, and anything else that takes memory on the host shrinks this layer. Index pages get no reserved space: they compete with collection pages in the one internal cache.

code

javascript · 4 lines
javascript
const c = db.serverStatus().wiredTiger.cache;
print("internal cache max bytes:", c["maximum bytes configured"]);
print("internal cache in use:", c["bytes currently in the cache"]);
print("pages read into cache:", c["pages read into cache"]);

go deeper

for a junior

Recall that there are two layers and who owns each: WiredTiger owns the internal cache, and the operating system owns the filesystem cache.

for a middle

Explain the form data takes in each layer, walk through the read path, and say why a miss in the internal cache does not always mean a disk read.

for a senior

Connect it to incidents: a new process on the host squeezes the filesystem cache without changing the internal cache, so more misses reach storage while the WiredTiger figures look unchanged.

for a principal

Treat free RAM as a resource you are deliberately spending on compressed blocks, and judge any request for a larger internal cache by what it takes away from that layer.

## Two caches with two owners A `mongod` running WiredTiger, MongoDB's storage engine, relies on two layers of memory that are often confused. The **internal cache** is memory that WiredTiger allocates and manages itself. The **filesystem cache** is memory the operating system manages to avoid disk I/O. MongoDB configures the first and only benefits from the second. | | Internal cache | Filesystem cache | |---|---|---| | Owner | WiredTiger, inside `mongod` | the operating system | | Sized by | `cacheSizeGB` or `cacheSizePct`, or the default formula | whatever RAM is free | | Collection data | uncompressed, in-memory representation | on-disk format, including block compression | | Index data | in-memory representation, can keep prefix compression | on-disk format | | Removed by | WiredTiger eviction | the operating system, under memory pressure | The manual sometimes says MongoDB "automatically uses all free memory" for the filesystem cache. Read that as a description of the effect: neither cache option allocates or limits this memory, and the operating system decides what stays in it. ## Why the representations differ Storage is organised to save space, and the cache is organised for fast, concurrent access. So WiredTiger converts a page every time it crosses the boundary. - **Collection pages** must be uncompressed in the internal cache, because the server has to manipulate the data. By default collections use Snappy block compression on disk. - **Index pages** also take a different form in memory than on disk, but index prefix compression, which removes common prefixes from indexed fields, still reduces their RAM use there. - **Blocks in the filesystem cache** are in the same format as the data files, so compression benefits apply and the same gigabyte can usually hold more of your data. ## The read path 1. An operation needs a page, and WiredTiger looks in its internal cache. 2. On a hit, the page is used directly, at no further I/O cost. 3. On a miss, WiredTiger asks for the block from the data file. 4. If the operating system has the block in the filesystem cache, no disk read happens. 5. Otherwise the block comes from the disk. 6. WiredTiger decompresses the block, builds its in-memory page and may have to evict something to make room. So there are three speeds, not two. A miss served by the filesystem cache costs decompression and page construction; a miss that reaches the disk also costs a storage read and competes with every other user of that device. ## Why the default leaves room The default internal cache is the larger of 256 MB and half of (RAM minus 1 GB). The remainder is not idle: it pays for connections and other process memory, and what stays free becomes the filesystem cache. On a 64 GB host the internal cache takes about 31.5 GB, and the second layer can use most of what remains. This gives two practical consequences: - **Other consumers shrink the second layer first.** The internal cache size is fixed at startup, so a process that takes RAM later squeezes the filesystem cache, and internal-cache misses start reaching the disk more often. - **Raising the cache is a trade.** Each gigabyte moved to the internal cache leaves one less for compressed blocks. Whether that helps depends on how much of the working set is being decompressed repeatedly. ## Where index pages fit The cache holding index pages is the same one holding documents. Nothing is reserved for indexes, and a page from an index is evicted by the same machinery as a page from a collection: WiredTiger walks the trees, collects evictable candidates, and takes the least recently accessed of them, approximating least-recently-used. - Index traversal needs internal pages and leaf pages, so a large index keeps real space occupied in the internal cache. - A workload's working set therefore includes the indexes it uses, not only its documents. - Prefix compression helps here, since it applies in memory as well as on disk, though only for shared prefixes. Which indexes to create in order to shrink that working set is an indexing decision covered separately. What belongs here is that they compete for the same memory, so a new large index can raise read misses on collection pages although no query against those collections has changed.

  • Is a read that misses WiredTiger's internal cache always a disk read?
    No. WiredTiger requests the block, and if the operating system holds it in the filesystem cache, the request is served from memory. The miss still costs decompression and building the in-memory page, but no storage I/O. That is why a mongod whose filesystem cache shrinks can slow down although its internal cache has not changed size.
  • Does WiredTiger set aside a part of its cache for indexes?
    No. The manual says the cache is allocated to the whole instance, with no part reserved for reads or writes, and it describes no index share either. Index and collection pages compete for the same space, and eviction chooses among them, so a large index used by the workload takes room from documents.

The internal cache is your desk, with documents unfolded and ready to edit. The filesystem cache is a shelf of sealed folders within arm's reach, and the disk is the archive in the basement. A smaller shelf leaves the desk the same size but means more trips downstairs.

  • Collection data in WiredTiger's internal cache stays Snappy-compressed to save RAM.
  • Blocks in the filesystem cache are the same uncompressed pages as in the internal cache.
  • A cacheSizeGB setting also controls how much filesystem cache a mongod gets.
  • Every miss in WiredTiger's internal cache results in a disk read.
  • WiredTiger keeps a reserved region of its cache for index pages.