A MongoDB server restarted with blockCompressor set to zstd in mongod.conf shows no drop in disk usage: why, and how do you estimate and realise the saving?
answer
- a default, not a conversion
- fixed when the collection was created
- indexes stay out of it
- test on a sample, like with like
- rewrite the data to collect
basics
~20 sblockCompressor only sets the default for collections created afterwards; existing collections keep their compressor. Estimate by copying a sample into a snappy and a zstd collection, then rewrite the data to collect the saving. Indexes will not shrink.
solid answer
~40 s`storage.wiredTiger.collectionConfig.blockCompressor` is a default for new collections: an existing collection keeps the compressor it was created with, and `collMod` and `compact` do not change it. To estimate, copy one sample of documents into two new collections, one `snappy` and one `zstd` through a `configString`, flush, and compare `storageSize()`. Apply that ratio only to collection storage, because index files have no block compressor and the journal has its own setting. To collect the saving, the data must be rewritten: create a new collection, copy, recreate indexes and swap, or on a replica set resync members one at a time with the new default. Dropping a collection frees its disk space; deleting documents generally does not return it. Check CPU as well, since the extra cost appears whenever pages move between disk and the WiredTiger cache.
code
javascript · 8 linesconst opts = (c) => ({ storageEngine: { wiredTiger: { configString: `block_compressor=${c}` } } })
db.createCollection('sample_snappy', opts('snappy'))
db.createCollection('sample_zstd', opts('zstd'))
db.events.aggregate([{ $sample: { size: 200000 } }, { $merge: { into: 'sample_snappy' } }])
db.sample_snappy.aggregate([{ $merge: { into: 'sample_zstd' } }])
db.adminCommand({ fsync: 1 })
db.sample_snappy.storageSize()
db.sample_zstd.storageSize()go deeper
Remember that blockCompressor only applies to collections created after you set it, and that existing ones keep what they had.
Show how to set a compressor for one collection with a configString at creation, and how to compare storageSize against dataSize to see the effect.
Estimate on a like-for-like sample, apply the ratio only to collection storage, and pick a rewrite route that fits the deployment, remembering that only a drop frees the old file.
Weigh the saving against the risk and effort of rewriting. A default for new collections costs nothing to change; converting existing data is a project that must earn its place.
## Why nothing moved `storage.wiredTiger.collectionConfig.blockCompressor` sets the **default compressor for collections created from now on**. The manual is explicit: existing collections continue to use the compressor specified when they were created, or the default at that time. The source shows why. The compressor is written into the WiredTiger table's create string when the table is made, as `block_compressor=<value>`, and nothing rewrites it later. So a restart with the new setting changes nothing already on disk, including after many checkpoints. Several other things also stay as they were: - **Indexes.** The index create string sets `prefix_compression=true` and no block compressor, so index files are outside this setting altogether. - **The journal.** It has its own setting, `storage.wiredTiger.engineConfig.journalCompressor`. - **Collections with an explicit compressor.** A per-collection `configString` is appended after the server's defaults and wins over them. - **Time-series collections.** They already default to `zstd`. ## What does not convert a collection These come up as suggestions, and none of them changes the compressor: - `compact` releases free space from a collection's existing file, and the collection keeps the compressor set at creation. - `collMod` has no option for changing a collection's storage engine configuration. - Waiting for eviction or checkpoints only writes pages using the table's existing configuration. ## Estimating the saving The manual gives no universal ratio, because it depends on your data and the library. Measure with your own documents. Copy the same random sample into two new collections so that they differ only in compressor, as in the example below. Comparing a sample against the original collection would be misleading, because the two are written in different orders and page locality affects compression. The `fsync` command matters: until pages are flushed, `storageSize()` under-reports a freshly loaded collection. Then turn the ratio into a disk figure, using `db.collection.storageSize()` and `db.collection.totalIndexSize()` across the database. Only the collection share moves. As an invented example, take 400 GB of collection storage and 100 GB of indexes, with the sample showing `zstd` at 70% of `snappy`. The saving is 120 GB of 500 GB, or 24% of the total, and not the 30% the ratio suggests. Disk is also only half the question. Run a representative workload against the candidate before committing to it, because `zstd` costs more CPU than `snappy`, and that cost appears on every page read from or written to disk. ## Realising the saving The data must be rewritten, and which route fits depends on the deployment: | Route | How it works | Watch out for | |---|---|---| | Copy and swap | New collection, copy, recreate indexes, then `renameCollection` | Writes during the copy; needs a cutover and room for both copies | | Dump and restore | `mongorestore` creates collections afresh under the target's default | Downtime; an explicit `storageEngine` option in the dump's metadata is reapplied unless you pass `--noOptionsRestore` | | Member resync | On a replica set, a member with the new default and an empty data path rebuilds through initial sync | One member at a time; a window of reduced redundancy | The resync route works because the member creates its collections fresh, so its own default applies, except where a collection's stored options name a compressor. Members may run different compressors during the rollout, as `blockCompressor` is a per-`mongod` setting. ## Getting the disk back The last step is where people are surprised again. Deleting documents leaves empty records that WiredTiger reuses, and outside very specific circumstances the space does not go back to the operating system. Dropping a collection or index does free its disk space, so a copy-and-swap only pays off when the old collection is dropped. On a replica set, a resync also defragments the member's files, which means part of the saving you see may be defragmentation rather than compression. The point worth stating in an interview is the order of work: 1. Confirm that the setting is a default and find which collections still use the old compressor. 2. Measure the ratio on a sample, and apply it only to collection storage. 3. Check the CPU cost under a representative workload. 4. Rewrite the data by the route that fits, then drop the old copy.
- How do you check which block compressor an existing MongoDB collection actually uses?Read the WiredTiger creation string in the collection's statistics: `db.collection.stats()` includes a `wiredTiger` section, and its `creationString` contains `block_compressor=<value>`. The running `mongod` setting only tells you the default for new collections, so it says nothing certain about old ones.
- After a copy and swap on a MongoDB server, the new zstd collection is smaller but overall disk usage has not dropped. What was missed?Probably the old collection still exists, perhaps under a backup name, so its file is still on disk. WiredTiger frees the space when a collection is dropped. If the old collection was emptied with deletes instead, that space is generally kept for reuse rather than returned to the operating system.
- Changing blockCompressor rewrites existing collections at the next restart.
- Running compact converts a collection to the new compressor.
- Pages are gradually recompressed with the new compressor as they are evicted.
- A better compressor shrinks indexes by the same ratio as collections.
- Compressors differ only in disk space, with no CPU cost worth measuring.
- Deleting the old documents is enough to return their disk space to the operating system.