Why does MongoDB feel fast until the cache fills?
This page is about documents and replica sets. I show you where WiredTiger keeps every collection and index, and how two logs split the jobs: journal for crashes, oplog for the replica set. After it you will be able to find any collection's .wt file, read the oplog window, and check whether the cache is under pressure.
A document database. JSON-like documents in collections. Built to run as a replica set.
Good at: Records that genuinely differ per item. Catalogues and profiles. Spreading across machines with built-in sharding. Two logs: journal for crashes, oplog for the replica set.
Runs as: mongod processes in a replica set; mongos routers when shardedProcess, memory, disk: a request enters at the process layer, memory serves it when it can, and disk makes it permanent. Name the layer first, then diagnose inside it. The full rule is on the PostgreSQL page: name the layer first, then diagnose inside it.
The same layers as a still diagram, with every part named:
Request path
Same idea as PostgreSQL: exact names, one job per log, one check per incident. The rule is written once on the PostgreSQL page. This table adds the operational logs a DBA reads first: server, error, slow.
| Log | Exact file name | Where it sits (on-prem) | What it does | What to check in it |
|---|---|---|---|---|
| MongoDB Journal | journal/WiredTigerLog.0000000123 | journal/ inside the dbPath | WiredTiger's write-ahead log. It covers the gap between checkpoints (about 60 s). j:true waits for this. | Check that the files exist and turn over. Check journal commit latency in db.serverStatus().wiredTiger.log. |
| MongoDB Oplog | local.oplog.rs | A capped collection in the local database. Not a file | Ordered record of every write on the primary. Secondaries tail it. Its size divided by write rate is the catch-up window. | rs.printReplicationInfo(). "log length start to end" is the window in hours. Compare it to your longest maintenance. |
| MongoDB mongod log | mongod.log | /var/log/mongodb/mongod.log | The server's diary. Elections. Slow operations (over the profiler threshold). Connection events. | Election events and slow-operation lines around the incident start. Stepdown and stepup pairs mark every failover. |
Same three outage rules as PostgreSQL: Safe live lever, Planned change, Do not touch. What each one means is written once on the PostgreSQL page.
| Parameter | What it does | During an outage |
|---|---|---|
| storage.wiredTiger.engineConfig.cacheSizeGB | Size of the WiredTiger cache. Default is about 50% of (RAM minus 1 GB), or 256 MB if that is larger. | Planned change. It is set in the config file. Applying it means a rolling restart of the set. Mid-incident, the evidence comes first: serverStatus().wiredTiger.cache. The live fixes are killing the cache-churning operation or the pinned snapshot, not the cache size. |
| oplogSizeMB / replSetResizeOplog | Size of the capped oplog collection. Window equals size divided by write rate. | Safe live lever (dynamic in 4.4 and later). Enlarge the oplog before planned maintenance so the window exceeds the downtime. It cannot recover entries that already rolled off. |
| writeConcern / readConcern | Per-operation guarantees set by the application. How many members must acknowledge a write (w). Whether it must be journaled (j). Which writes a read may see. | Application change, not a server knob. Do not weaken w:'majority' mid-outage to make writes "fast again", unless rollback-on-election is explicitly acceptable. Shifting readPreference to secondaries sheds primary load but serves staler data. That is a routing decision, not a repair. |
Same rule as PostgreSQL: there is no hidden state. Every file here is inspectable with ls and du while the server runs. The full version is on the PostgreSQL page.
$ ls /var/lib/mongo WiredTiger # WiredTiger version marker WiredTiger.wt # WT's own metadata: where the # last checkpoint is WiredTiger.turtle # backup copy of that metadata _mdb_catalog.wt # THE MAP: "users" = which file? collection-2-4471….wt # ← collection "users" collection-5-9912….wt # ← collection "orders" index-3-4471….wt # ← the _id index of "users": # one file per index journal/ # THE JOURNAL (write-ahead log): WiredTigerLog.0000000123 mongod.lock # lock file; a stale one means the # last shutdown was not clean sizeStorer.wt storage.bson diagnostic.data/ # metrics mongod records on itself
Same pattern as PostgreSQL: five logs, one job each, and every operational budget is a time window. The full version is on the PostgreSQL page.
Journal to checkpoint: what covers the 60-second gap
The other logs: PostgreSQL: WAL · MySQL · InnoDB: redo log and binary log · the pattern behind all five, and where they break
Same rule as PostgreSQL: every mechanism above is observable on a running server in under two minutes. The full version is on the PostgreSQL page.
Run it locally: docker-compose-mongo.yml starts MongoDB 8.0 on port 27017 with a shop database. docker compose -f docker-compose-mongo.yml up -d, then run the commands below against it.
// where is everything? db.adminCommand({getCmdLineOpts: 1}) // → parsed.storage.dbPath db.users.stats().wiredTiger.uri // → "table:collection-2-4471…" $ ls /var/lib/mongo/journal // → WiredTigerLog.0000000123 // the oplog: size, window, latest entry rs.printReplicationInfo() rs.status() // members + optime lag // cache pressure db.serverStatus().wiredTiger.cache
Problem. My query filters, sorts, and returns. It runs slow. I run explain. It shows a SORT stage. The index exists. The sort still happens.
Mechanism. A compound index stores keys in field order. Field 1 first. Field 2 inside field 1. Field 3 inside field 2. Put the equality field first. Put the sort field second. Put the range field last. That is ESR.
The equality match narrows the walk to one slice. Inside that slice, the sort field is already in order. The query reads rows in sorted order. The range field sits last. It does not change the order. It only filters keys as the scan walks the equality slice.
Put the fields in the wrong order and the sort breaks. A range field before the sort field breaks it. The index can no longer hand back rows in sort order. MongoDB adds a SORT stage. That stage sorts in memory. The limit is 100 MB. Since MongoDB 6.0 a sort past that limit spills to temporary files on disk by default (allowDiskUseByDefault), which is slow, and it fails only if the query sets allowDiskUse(false) or the server option is off.
ESR: equality, sort, range, in that order
Range before sort: the order breaks
MongoDB caches the winning plan for a query shape. Adding or dropping an index clears the plan cache for that collection automatically, so the next run re-plans and picks up the new index. A manual clear is only needed for other causes, such as a plan that went stale as data distribution changed.
Exact check. Build the index in ESR order. Then explain the query. Then check index use and plan cache.
Read explain for SORT stage. No SORT stage means the index order did the sorting. Read totalDocsExamined next to nReturned. A wide gap means the index is not narrowing the work.
Every new write landed on one shard. That node ran at 100 percent while the rest idled, and inserts slowed for every customer.
I checked distribution, not totals. One node was at 100 percent and the rest were idle. A monotonic shard key was sending every new write to the same shard.
The fix was the key, not more shards. Hash-prefix a monotonic shard key, or give the whale tenant its own shard. Choose the shard key from the query pattern before launch.
How to host MongoDB on-prem, on EC2, in Kubernetes, or on Atlas is on the Cloud page.
One command. It tells you how long a secondary can stay down and still catch up.
The oplog window: size divided by write rate
Read "log length start to end". That is the window in hours. Compare it to your longest maintenance.
Step through a write travelling from the primary to a secondary. Then break it.
Yes. With w: 1 the primary answered before any secondary had the write. If a secondary that never received it is elected, the write is rolled back. With w: "majority" it cannot be lost this way.
Reads were fine last month. Now p99 latency has tripled, with no code change and the same traffic. The data has grown past the WiredTiger cache. What changed, and what do you look at?
Write your answer down first. Then open mine.
What changed: the pages your queries touch no longer all fit in the WiredTiger cache. Some reads now come from the operating system's file cache, which holds compressed pages, and some from disk. When the cache is full, application threads are also asked to help evict pages, which adds latency to ordinary queries.
What I look at: db.serverStatus().wiredTiger.cache: bytes in the cache against the maximum, pages read into the cache per second, and eviction by application threads. Then index sizes from db.collection.stats(), because indexes that no longer fit hurt every query.
What I would change: drop indexes nobody uses ($indexStats shows them), archive old documents, or add memory. Sharding is the answer only when the working set is too big for any one machine you would want to run.
The same mechanism, caches and memory, replication and failover, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. With w: "majority" and the default journaling setting, when is a write acknowledged?
writeConcernMajorityJournalDefault is true by default, so majority writes wait for the journal on a majority.
2. A secondary falls further behind than the oplog window. What happens?
The oplog entries it needs have been overwritten, so it cannot catch up from the oplog.
3. A multi-document transaction stays open past transactionLifetimeLimitSeconds. What happens?
In Notebook 1, the commit failed after the limit, and the write was gone.
4. How does MongoDB store documents on disk?
Each collection and index is a .wt file, with BSON documents in compressed blocks.
How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.