skip to content

What compressor policy would you set for a fleet of MongoDB deployments, and how would you decide where to depart from snappy?

level: principalimportance: should knowfreq 18%

answer

  1. disk bill against CPU headroom
  2. new collections are the cheap part
  3. measure on your own data
  4. exceptions need an owner
  5. leave journal and index defaults alone

basics

~20 s

Set one default for new collections by workload class, usually snappy or zstd, based on measured disk savings against CPU headroom. Avoid mass conversions unless the saving pays for the rewrite, and leave journal and index defaults alone.

solid answer

~50 s

I would set a default per workload class rather than per team. Per MongoDB's production notes, `snappy` has the lowest CPU cost and the lowest rate, `zstd` compresses better than both alternatives and costs less CPU than `zlib`, so `zlib` rarely wins a new decision. Where disk cost dominates and CPU headroom exists, I would make `zstd` the default for new collections, since changing a default costs little; where latency budgets are tight and working sets miss the cache often, I would stay on `snappy`. The decision comes from measuring your own documents, because ratios vary. Converting existing data is a separate project that must pay for itself, done by rewriting members or collections. Per-collection exceptions via `configString` need an owner. I would keep `prefixCompression` on and the journal on `snappy`, and write the `zstdCompressionLevel` into the template, since it is process-wide.

code

yaml · 6 lines
yaml
storage:
  wiredTiger:
    engineConfig:
      zstdCompressionLevel: 6
    collectionConfig:
      blockCompressor: zstd

go deeper

for a junior

Know that snappy is the default and that zstd and zlib compress harder for more CPU. A policy question is about who chooses and why, not about memorising ratios.

for a middle

Explain where the CPU is spent: pages crossing between disk and the WiredTiger cache. That is why a cache-heavy workload and a cache-missing workload pay differently for one compressor.

for a senior

Bring a measurement method: one sample under two compressors, a realistic CPU test, and a disk figure that counts only collection storage. Know how a rolling resync converts members.

for a principal

Separate the cheap decision about new collections from the expensive decision about existing data, give exceptions owners, and name the trigger that reopens the policy.

## The decision being made There is no right compressor for a fleet in the abstract. A compressor policy decides how much CPU you spend to reduce disk, how often that trade is revisited, and who may vary from it. A lead owns this because nobody on a single product team sees the whole bill, and because `storage.wiredTiger.collectionConfig.blockCompressor` only affects collections created after it changes, so a policy shapes the estate slowly and for a long time. ## What the options offer MongoDB's production notes rank the three libraries, and the glossary agrees: | Compressor | Compression rate | CPU cost | Typical place in a policy | |---|---|---|---| | `snappy` | Lowest of the three | Lowest of the three | The server default; latency-sensitive workloads | | `zstd` | Best of the three | Lower than `zlib` | Cost-sensitive and archive-like data | | `zlib` | Better than `snappy` | Highest of the three | Existing collections that already use it | | `none` | No saving | No compression CPU | Data that will not compress, once measured | Taken at face value, those rankings make `zlib` hard to choose for anything new. Time-series collections already default to `zstd`. The CPU cost falls in a specific place. Collection data in the WiredTiger cache is uncompressed, so a page is compressed when written and decompressed when read from disk. A workload that fits in cache pays little for reads; one that misses the cache constantly pays on many reads, so its latency is more exposed. ## Inputs to the decision - **Storage cost and growth.** The price of disk, including every replica set member and backup copy, against the CPU you would add. - **Cache miss rate.** The more pages cross the disk boundary, the more a heavier compressor costs. - **I/O cost.** WiredTiger's tuning notes point both ways: on fast solid-state drives turning compression off may improve performance by reducing CPU, while where I/O is expensive turning it on may improve it by reducing I/O. - **Data shape.** Repetitive documents compress well, while already-compressed or client-side encrypted values have little left to give. - **Index share.** Indexes get prefix compression and no block compressor, so a database dominated by indexes gains less. ## A policy that holds up 1. Classify workloads, for example latency-critical, general and cost-optimised, and give each class one default in the `mongod.conf` template. 2. Measure before adopting: one copied sample under two compressors, plus a representative CPU test. 3. Apply the default to new collections first. It costs nothing to change, and the estate then improves through normal turnover. 4. Treat conversion of existing data as a separately justified project with a rollback plan. 5. Record per-collection exceptions, each with an owner and the measurement behind it. ## Rollout and exceptions Exceptions and conversions need some mechanics: - A per-collection `configString` such as `block_compressor=zstd` is logged to the oplog during replication, so it travels to other members and takes priority over each member's default. - `blockCompressor` itself is a per-`mongod` setting, so members can differ during a rolling change. Resyncing members one at a time converts their data without a cutover, at the price of reduced redundancy while each one rebuilds. - `storage.wiredTiger.engineConfig.zstdCompressionLevel` is **process-wide**, not per-collection. The default is 6, and it applies to `zstd` block and journal compression alike. - Since 8.2 the level accepts -7 to 22; 8.0 accepts 1 to 22. Check the range before a downgrade, because a negative level is outside what 8.0 supports. A value of 0 means zstd's internal level of 3, not the default of 6. ## What I would leave alone Two defaults rarely repay the effort of changing them: - **`prefixCompression`.** It saves both memory and disk at modest cost. Turning it off only makes sense for a measured case with almost no shared prefixes. - **`journalCompressor`.** The journal is short-lived, since old files are removed after checkpoints. Changing the compressor needs a clean shutdown, and after an unclean one the old setting is needed to read the existing journal. Revisit the policy on a trigger, such as a storage-cost review or a major upgrade, rather than leaving it to individual preference. Write down what was decided and why, because a compressor picked today will still be on disk years from now. A related question usually comes up in the same review: whether to ask for `j: true` by default. On a replica set the documented global default is `majority`, unless someone has changed it, and that implies `j: true` while `writeConcernMajorityJournalDefault` is true, so adding it rarely changes anything there. The full promise belongs with write concern, not with this policy.

  • A team wants zlib because it has always used it. How would you handle that in a MongoDB compressor policy?
    I would not force a rewrite on that basis alone, since existing collections keep working and conversion has its own cost and risk. For new collections, I would ask them to measure `zstd` on their data, since the docs rate it better than `zlib` on both rate and CPU. If a measurement disagrees, it becomes a recorded exception.
  • Should a MongoDB fleet convert everything to zstd in a single campaign?
    Only if the sums justify it. Multiply the measured ratio by collection storage across every member and backup, then compare that with the work: rewrites, reduced redundancy during resyncs, added CPU, and the chance of a failed migration. Often the better plan is to change the default and convert only the few largest collections.

  • zlib is the safe choice for new collections because it compresses hardest.
  • Everything should use zstd because it is always strictly better than snappy.
  • Changing the fleet default takes care of existing data.
  • You can quote a universal compression ratio without testing your own documents.
  • Compression is purely a disk decision with no effect on latency.
  • zstdCompressionLevel can be tuned separately for each collection.

Assumes MongoDB 9.0; zstdCompressionLevel accepts -7 to 22 since 8.2, while 8.0 accepts 1 to 22.