skip to content

A disk fault is reported on one member of a MongoDB replica set: how do `validate`, `dbCheck` and `repair` each fit into deciding what to do?

level: seniorimportance: nice to knowfreq 15%

answer

  1. inside one member versus between members
  2. primary hashes, secondaries compare
  3. the health log, not the reply
  4. repair throws data away
  5. an intact member beats any repair

basics

~20 s

Run validate on the suspect member to check its own structures, and dbCheck from the primary to compare members through hashed batches. Then resync the damaged member from an intact one; repair is standalone only and removes corrupt documents.

solid answer

~40 s

They answer different questions. `validate`, ideally with `full: true`, tells me whether this member's own documents and indexes are sound, and I run it on the suspect with the application off it, because of the exclusive lock. `dbCheck` runs from the primary and asks whether members agree: the primary writes batch hashes to the oplog, each secondary recomputes them, and results land in that member's `local.system.healthlog`, not in the reply. `repair` is not the fix here: on a replica set member `validate` refuses `repair: true`, and it removes corrupt documents rather than recovering them. With an intact member available, the manual's answer is to resync the damaged one from it, on healthy hardware. I would use `mongod --repair` only when no other copy exists, after copying the `dbPath`.

code

javascript · 5 lines
javascript
// On the primary, in the database to check. Returns at once; a background job runs.
db.runCommand({ dbCheck: "orders" })

// Later, on each member: what did this member conclude?
db.getSiblingDB("local").system.healthlog.find({ severity: "error" })

go deeper

for a junior

Recall that repair can lose data and that a replica set usually has another good copy, which is the safer place to recover from.

for a middle

Explain that validate checks one member's internal structures, while dbCheck compares members by hashing batches through the oplog and logging to local.system.healthlog.

for a senior

Sequence it: take the member out of the path, run full validate, run dbCheck from the primary, read each member's health log, and resync from the most up-to-date member onto healthy disks.

for a principal

Decide in advance when a team replaces a member outright rather than diagnosing it, and how much dbCheck throughput it will pay for, since the default throttle makes a full pass very slow.

## Three tools, three questions A reported disk fault raises separate questions, and each tool answers only one of them. | Question | Tool | Runs where | Result appears in | |---|---|---|---| | Are this member's own structures sound? | `validate` | The suspect member | The command's reply | | Do the members hold the same data? | `dbCheck` | The primary, then the oplog carries it | `local.system.healthlog` on each member | | How do I get a good copy back? | Resync from an intact member | The damaged member | The member rejoining the set | | No other copy exists, what can be salvaged? | `validate` with `repair`, or `mongod --repair` | A standalone | The reply, or the server log | ## Step 1: check the suspect with `validate` Run `validate` per collection on the member that reported the fault. **`full: true`** is the useful form here, because it adds WiredTiger's `verify()` over the record store and each index, which is the part of the check that looks at the underlying tables. This costs something. A foreground run takes an exclusive collection lock, and the manual warns it can block all other operations on a secondary until it finishes. So take the member out of the application's path first, and if it is the primary, step it down with `rs.stepDown()`. A `background: true` run is available for a screen with no exclusive lock, but it rejects `full`, so it skips the check you most want after a hardware problem. Read `corruptRecords`, `nInvalidDocuments`, `missingIndexEntries`, `extraIndexEntries` and `indexDetails`. A `valid: true` covers only the collections you ran it on, and a run of `validate` never looks past this member. ## Step 2: compare members with `dbCheck` `dbCheck` exists because a member can pass `validate` and still hold different data from its peers. It works like this: 1. You run `{ dbCheck: "orders" }`, or `{ dbCheck: 1 }` for every collection in the database. The command is refused on a secondary. 2. It returns straight away, and a background job does the work. 3. The primary hashes documents in batches over ranges of keys and writes each batch's MD5 to the oplog. 4. Each secondary, on applying that entry, hashes the same range locally and compares. 5. Results go to the capped **`local.system.healthlog`** collection on each member, 100,000,000 bytes by default. A mismatch reads `dbCheck batch inconsistent` with severity `error`, or `warning` on capped collections and a few other cases where divergence is expected. Points that trip people up: - **It is throttled hard.** `maxDbCheckMBperSec` defaults to 1, so a 100 GB collection takes more than a day unless you raise it. `maxDocsPerBatch` defaults to 5000 and `maxBatchTimeMillis` to 1000. - **It only queues.** One run executes at a time, and `dbCheckMaxRunsOnQueue` allows up to 5 including the running one. - **It reports and does not repair.** It also does not cover what is not replicated, because the whole mechanism travels through the oplog. - **It has no page in the 9.0 manual's command reference.** It appears in release changelogs and in error codes, so the source's help text and parameters are the reference. Check behaviour against your exact version before relying on it. - **Reading the result takes judgement.** One secondary reporting `inconsistent` points at that member. Every secondary reporting it points at the primary. This is an inference from how the hashes flow, so confirm it with `validate` on the member you suspect. ## Step 3: recover with a resync, not a repair The manual is direct about this. If you have an intact copy, such as another member or a recent backup, **restore from that copy**: remove the member's data and let it perform an initial sync from the most up-to-date member. Resyncing from a stale member can leave a write that was majority committed no longer majority committed, which can lose data. Do this on a disk you trust, since a resync onto the same faulty disk only moves the problem. ## What `repair` really does - **`validate` with `repair: true`** is refused on a replica set member with the message that it can only be performed in standalone mode. It inserts missing index keys, removes extra keys and **removes corrupt documents**. - **It also implies a multikey fix.** The manual says you must set `fixMultikey: true`, while the 9.0 code implies it and rejects only an explicit `fixMultikey: false`. - **`mongod --repair`** works on the whole instance. The manual says to use it only with no other options, because it removes and does not save corrupt data. Copy `dbPath` first. - **It does not save you the resync.** If `mongod --repair` changes data or metadata on a replica set member, that member still needs a full resync to rejoin. A tempting shortcut is to run repair on the member and put it back. That leaves it holding less data than its peers, and that gap is exactly what the set must not silently carry.

  • A member passes `validate` with `full: true`, but `dbCheck` reports inconsistent batches on it. What does that tell you?
    Its structures are sound, but its data differs from the primary. `validate` checks only internal consistency, so a member can be well-formed and still wrong. The two results do not conflict. If only this member reports it, treat it as the wrong one and resync it from an intact member, since `dbCheck` cannot repair; then look for what caused the divergence.
  • When is `mongod --repair` the right call on a member of a replica set?
    Almost never while another good copy exists: the manual says to restore from an intact member or backup instead. It is for the case with nothing else to restore from. Copy the `dbPath` first, because it removes corrupt data without saving it, and expect a full resync if it changes anything and the member must rejoin the set.

  • Running validate with repair: true on the damaged member will fix it in place
  • A clean validate on every member proves that the members hold identical data
  • dbCheck can be started on the suspect secondary and reports its verdict in the reply
  • repair recovers corrupt documents so that no data is lost
  • After mongod --repair changes data, the member can simply rejoin the replica set
  • Resyncing from whichever member is nearest is always safe

Assumes MongoDB 9.0, read from the r9.0.4 source; the fixMultikey option arrived in 8.1, so an 8.0 deployment has only repair.