Home›Learn›MongoDB 1: why MongoDB
Course · Inside one serverLesson 7 of 330 read

MongoDB · WiredTiger

Why does MongoDB feel fast until the cache fills?

Chapter 1 · Why MongoDB

When I reach for MongoDB

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.

The shape of the data matches the app.

An order with its items, a product with its variants: one document, read and written as one piece, with no join.

High availability is built in.

Every production deployment is a replica set that elects its own primary. Drivers follow it without a proxy.

Scaling out is a first-class feature.

Sharding spreads collections across replica sets when one server is not enough.

Where I would think twice.

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.

MongoDB · WiredTiger: in one line

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 sharded

Printable one-page cheat sheet for MongoDB →

Follow it

Follow one change to a secondary

Step through a write travelling from the primary to a secondary. Then break it.

A write was acknowledged with w: 1, and the primary crashes a second later. Can the write be lost?

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.

Your turn

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.

Show my answer

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.

Next · chapter 2How it stores your datamongod, the WiredTiger cache, files and logs
Connected

Where this shows up

The same mechanism, caches and memory, replication and failover, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 4 questions

Check yourself

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?

2. A secondary falls further behind than the oplog window. What happens?

3. A multi-document transaction stays open past transactionLifetimeLimitSeconds. What happens?

4. How does MongoDB store documents on disk?

One database decision a weekA short email each week: one real decision, the trade-off, and the command to check it. Unsubscribe in one click.

Part 1 of 4 · Learn · 56 pagesAll pages

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.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect