Why does MongoDB feel fast until the cache fills?
MongoDB stores documents, not rows, and builds failover into the database itself. Here is when that is the right trade, and when it is not.
An order with its items, a product with its variants: one document, read and written as one piece, with no join.
Every production deployment is a replica set that elects its own primary. Drivers follow it without a proxy.
Sharding spreads collections across replica sets when one server is not enough.
Data that is mostly relationships and joins, or reports across everything: a relational database is usually simpler.
This track tells MongoDB as one story: what WiredTiger is, how it stores your data, what happens on every write, how a replica set stays up, how I run it, and what to do when it breaks.
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 shardedStep 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.