skip to content

In a MongoDB deployment on WiredTiger, what is compressed by default, and with which compression method?

level: juniorimportance: should knowfreq 28%

answer

  1. three things reach disk
  2. documents, index keys, journal records
  3. block against prefix
  4. one exception: time-series collections

basics

~10 s

Three things: collection data gets snappy block compression (time-series collections get zstd), indexes get prefix compression, and the journal gets snappy. Block compression shrinks data only on disk; prefix compression also saves memory.

solid answer

~40 s

MongoDB configures WiredTiger to compress three things out of the box. Collection data uses block compression with `snappy`, set by `storage.wiredTiger.collectionConfig.blockCompressor`; `zlib`, `zstd` and `none` are the alternatives, and time-series collections default to `zstd`. Indexes use prefix compression, controlled by `storage.wiredTiger.indexConfig.prefixCompression`, which stores identical key prefixes once per page and helps in memory as well as on disk. The journal also uses `snappy`, through `storage.wiredTiger.engineConfig.journalCompressor`, except that records of 128 bytes or less stay uncompressed. Block compression happens on the way to disk: collection data in the WiredTiger cache is uncompressed, so the saving is disk space and the cost is CPU whenever a page crosses that boundary.

code

yaml · 8 lines
yaml
storage:
  wiredTiger:
    engineConfig:
      journalCompressor: snappy
    collectionConfig:
      blockCompressor: snappy
    indexConfig:
      prefixCompression: true

go deeper

for a junior

Recall the three targets and their defaults: collections use snappy, indexes use prefix compression, the journal uses snappy. Add that time-series collections default to zstd.

for a middle

Explain why block compression helps only on disk while prefix compression also saves memory, and name the setting that controls each target.

for a senior

Point out that index files get no block compressor, so a compressor change moves only part of the disk bill. Estimates that ignore the index share come out too optimistic.

for a principal

Treat these as three separate decisions with different costs. Collection compressors are worth a policy, while journal and index defaults rarely repay the effort of changing them.

## What WiredTiger compresses by default MongoDB's default storage engine, **WiredTiger**, puts three kinds of data on disk that are worth compressing: documents, index keys and journal records. The **journal** is WiredTiger's write-ahead log, the file it uses to recover recent writes after a crash. Each kind has its own mechanism and its own setting, and these are the values a `mongod` 9.0 uses when its configuration file says nothing. | What | Method | Default | Setting | |---|---|---|---| | Collection data | Block compression | `snappy` | `storage.wiredTiger.collectionConfig.blockCompressor` | | Time-series collections | Block compression | `zstd` | Set by the server for that collection type | | Indexes | Prefix compression | On | `storage.wiredTiger.indexConfig.prefixCompression` | | Journal records | Record compression | `snappy` | `storage.wiredTiger.engineConfig.journalCompressor` | The two compressor settings accept the same four values: `none`, `snappy`, `zlib` and `zstd`. ## Block compression for collections **Block compression** works on a page's bytes as they are written to a data file, and reverses the work when the page is read back. Two consequences follow, and both come up in interviews: - It saves **disk only**. Collection data in the WiredTiger cache is uncompressed, because the server has to work with it. - The filesystem cache holds the on-disk format, so it does benefit from compression. - The CPU cost is paid when pages move between disk and the WiredTiger cache, not on each read served from that cache. The three libraries differ in the expected way, and MongoDB's production notes rank them like this: - `snappy` has the lowest compression rate of the three and the lowest CPU cost. - `zlib` compresses better than `snappy` but costs more CPU than both `snappy` and `zstd`. - `zstd` compresses better than both and costs less CPU than `zlib`. The real ratio depends on your data. MongoDB publishes no universal figure, and a sensible candidate does not quote one. ## Prefix compression for indexes **Prefix compression** does something different. Index keys are stored in sorted order, so neighbours on a page often begin with the same bytes. WiredTiger stores each identical prefix only once per page. A compound index on `{ country: 1, city: 1 }` is a natural fit, because many keys on a page can share the same country. Because it changes how keys are represented, rather than squeezing bytes on the way to disk, prefix compression reduces both memory and disk use. Its cost is extra CPU and memory on the in-memory tree: moving a cursor backwards through a page, or looking up a random key, can allocate memory for uncompressed keys. Index files are **not** block compressed by default. The server's index configuration sets `prefix_compression=true` and names no block compressor. This is why changing `blockCompressor` does nothing to the size of your indexes. ## Journal compression Journal records are compressed with `snappy` before they reach the files under `dbPath/journal`. A record of 128 bytes or less, the minimum record size, stays uncompressed. Journal compression is an independent setting: changing the collection compressor leaves it alone. Changing the journal compressor needs a clean shutdown, because after an unclean one the old setting is needed to read the existing journal files. ## Whose default is this These are **MongoDB's** defaults, passed down to the library when it creates tables and opens the engine. WiredTiger's own library defaults are the opposite: `block_compressor` is `none`, `prefix_compression` is `false`, and log compression is `none`. Anyone reading WiredTiger's documentation directly can reach the wrong conclusion about what a `mongod` does. ## What this does not cover Storage compression is one of several unrelated compression features in a MongoDB estate: - `net.compression.compressors` compresses traffic between clients and servers, and defaults to `snappy,zstd,zlib`. - `mongodump --gzip` compresses a dump, and has nothing to do with data files. - Time-series collections also use column compression, a separate feature from the block compressor. ## Seeing it on a real collection Three `mongosh` helpers show the effect, and each answers a different question: 1. `db.collection.dataSize()` returns the uncompressed size of the documents. 2. `db.collection.storageSize()` returns the space allocated on disk for them, which reflects block compression. 3. `db.collection.totalIndexSize()` returns the space taken by the collection's indexes, which reflects prefix compression. A large gap between the first two is the block compressor at work.

  • Does a better block compression ratio let a MongoDB server hold more collection data in the WiredTiger cache?
    No. Block compression only affects what is on disk, and collection data in the WiredTiger cache is uncompressed. The filesystem cache does hold the compressed on-disk format, so it gets more out of the same memory. Index prefix compression is the one that reduces memory inside the WiredTiger cache.
  • If a mongod uses zstd for collections, are its index files compressed with zstd as well?
    Not by default. `blockCompressor` feeds the collection create string, while the index create string sets `prefix_compression=true` and no block compressor. Indexes therefore stay prefix compressed only, whatever the collection compressor is, and their size does not move when it changes.

  • MongoDB stores data uncompressed unless you switch compression on.
  • zstd is the default block compressor for every collection.
  • Indexes are block compressed with snappy by default.
  • Block compression also shrinks collection data held in the WiredTiger cache.
  • The blockCompressor setting also controls the journal and network traffic.