Vivian Voss

ZFS Compresses Every Write

like for like freebsd zfs storage

The machine that serves this page runs FreeBSD 15.1, and this morning I asked it what its jails weigh:

zfs list -o name,used,logicalused,compressratio rpool/jails
NAME          USED  LUSED  RATIO
rpool/jails  84.0G   200G  2.60x

That is 200 GiB of web servers and build trees, as the programs inside them see it, with a map server accounting for ninety-odd of them on its own, held on 84 GiB of disk. Nobody inside those jails knows. Every program reads and writes its files at their full size, and the filesystem packs them on the way down and unpacks them again whenever they are read.

On a Debian server the same 200 GiB would need 200 GiB of disk and a little more for the metadata, and there is a reason for that worth hearing in full.

Why ext4 keeps its blocks whole

In January 2025 a user asked on the ext4 mailing list for transparent compression, mentioning a PostgreSQL database that would shrink by a factor of seven. Theodore Ts'o, who maintains ext4, explained what it would cost. A database writes into the middle of its files, so a compressing filesystem has to pack data in clusters, and "a single 4k random write gets amplified into a 32k or 64k write". Worse, rewriting a compressed cluster in place risks tearing it in a crash, and with it the database's promise that a transaction either happened or did not.

Christoph Hellwig answered for XFS that "The only sane way to implement compression in XFS would be using out of place writes", writing the cluster somewhere new and then recording where it went, and since that path in XFS is still young, "So don't plan for it." Ts'o replied by counting what out-of-place writing costs a filesystem built for the other kind: for one small transaction, four steps without compression and nine with it, including a second cache flush. "This kills performance." Dave Chinner added the memory side of the argument. "Linux caches uncompressed data in the page cache", and keeping compressed data there instead "is not a viable caching strategy for a general purpose OS."

Every one of those points is sound for a filesystem that updates its blocks in place, which ext4 and XFS do. chattr +c marks a file for compression all the same, and its own manual page owns up further down that ext4, as the mainline kernel implements it, does not honour the mark. Debian's installer picks ext4 "in most cases", and Red Hat says it plainly: "XFS is the default file system in RHEL."

One small transaction: four steps, or nine with compression on ext4 overwrite in place compressed, written out of place 1 write block A 2 write block B 3 write block C 4 cache flush 1 write cluster A 2 write cluster B 3 write cluster C 4 cache flush 5 journal: where A went 6 journal: where B went 7 journal: where C went 8 journal commit block 9 second cache flush ZFS writes every block out of place anyway; compression is one more stage before the write. Source: Theodore Ts'o, linux-ext4, 22 January 2025

The filesystem that never overwrites

The FreeBSD Handbook answers all three maintainers at once, in a sentence about what happens whenever data changes: "ZFS writes a different block". Every write ZFS has ever made lands somewhere new, and the pointers to it follow in the same transaction. The expensive machinery Ts'o counted out step by step is simply how ZFS writes, compressed or not, so compression adds one stage to a pipeline that was already there.

That stage is careful about what it keeps. A record, 128 KiB unless the dataset says otherwise, is compressed and then rounded up to whole disk sectors, and "If compression saves less than one whole sector, the block will be stored uncompressed." A block of zeros is not compressed at all; "it is stored as a hole". Before zstd at its default level starts on a block, a cheap trial run checks whether the block is worth the effort at all. And the work is spread across the machine, since zio_taskq_batch_pct gives 80 per cent of the CPUs a worker thread for "compression, encryption, checksum and parity calculations".

The FreeBSD installer turns all of this on before the first file arrives. Its ZFS script creates the root pool with -O compression=on -O atime=off, and on currently means LZ4. Everything below the pool inherits it, and each dataset may choose for itself:

zfs set compression=zstd tank/logs
zfs set compression=lz4 tank/pgdata
zfs get compressratio,logicalused tank/logs

Six algorithms sit behind that property, zstd with nineteen levels among them, and zle, which compresses nothing but runs of zeros, for good measure. The last line of the block above answers what most administrators would ask next, which is how much it bought them.

What it saves on one machine

I measured it on the same server, an Intel Core i5-13500 with FreeBSD 15.1 and OpenZFS 2.4.2, in a scratch pool with three datasets that differed in nothing but the compression setting. Into each went the same tar archive of the base system's userland (the programs, libraries, manual pages and headers under /usr), 649 MB as the files see it:

compression=off    652 MB on disk   1.00x
compression=lz4    299 MB on disk   2.19x
compression=zstd   222 MB on disk   2.96x

Then a gibibyte of random data, which no algorithm can shrink, went into each of the three. It took exactly the same space in all of them, to the byte, because ZFS tried each block, found nothing worth a sector and stored it as it was.

Space on disk, same data, three settings userland, 649 MB as the files see it off652 MB · 1.00x lz4299 MB · 2.19x zstd222 MB · 2.96x random data, 1 GiB off1,074 MB lz41,074 MB zstd1,074 MB Nothing shrinks random data, and nothing grew: the same byte count in every dataset. Own measurement, 1 December 2026: FreeBSD 15.1, OpenZFS 2.4.2, Intel Core i5-13500, scratch pool

That attempt is cheap. On the LZ4 project's reference machine, a Core i7-9700K working on a single thread, LZ4 compresses at 780 MB/s and decompresses at 5,060 MB/s, and ZFS hands the work to most of the cores it has. The OpenZFS documentation draws the obvious conclusion: "If you want to use compression and are uncertain which to use, use LZ4."

The saving does not stop at the disk. When ZFS reads a block into its cache, the ARC keeps it as it came: "Since the b_pabd is a copy of the on-disk physical block, it will match its on-disk compression characteristics." That is the compressed caching which Chinner considered unworkable for Linux, and in OpenZFS it is switched on by default, so the cache holds a good deal more of the files than the memory it occupies would suggest. For databases the same documentation says, without a trace of the ext4 list's alarm, "Set compression=lz4 and recordsize=32K" for PostgreSQL's data and its log.

This blog lives in a jail on that server as well. Its files come to 1.84 GiB, and they occupy 868 MiB of disk, which is neither here nor there for the disk and rather pleasant for whoever pays for it.

The server behind this blog, 1 December 2026 all jails 2.60x 200 GiB in 84.0 GiB 116 GiB never written map server data 2.59x 91.3 GiB in 36.7 GiB this blog's jail 2.58x 1.84 GiB in 868 MiB zfs list -o name,used,logicalused,compressratio; FreeBSD 15.1, OpenZFS 2.4.2, compression=zstd

Where Linux has it

Linux does have filesystems that compress, and they confirm the point by how they are built. Btrfs writes out of place, like ZFS, and Fedora has mounted it with compress=zstd:1 by default since Fedora 34. Its rule for awkward data is coarser: "If the first portion of data being compressed is not smaller than the original, the compression of the whole file is disabled", and the flag stays on that file. ZFS makes the same decision for every block on its own.

Red Hat put its compression one floor down. VDO, which LVM manages on RHEL 9, works "inline" at block level, underneath an ordinary XFS or ext4 that goes on writing in place and has no idea. The administrator sets a logical size in advance, "the amount of data that applications can write to the volume before deduplication and compression occurs", and has to keep an eye on the real disk filling up behind it.

And OpenZFS itself runs on Linux, as a module outside the kernel tree that is rebuilt with every kernel update. On FreeBSD it ships in the base system and boots the machine.

The limit

Compression works on what is written after it is switched on. "Changing this property affects only newly-written data", so a pool that ran without it stays uncompressed until its data is written again, by a copy or by zfs send into a new dataset.

Data that arrives compressed stays its size. Video, JPEG images, encrypted backups and compressed archives gain nothing, and ZFS spends one attempt on each block before storing it raw.

Small records gain less. Because blocks are rounded to whole sectors, a 16 KiB record on 4 KiB sectors "needs to save at least 25% to actually save space on disk", which is why the documentation's database advice raises the record size along with the compression.

And a database that already compresses its own tables, as MySQL can, leaves the filesystem less to do.

ZFS paid for compression on the day it decided never to overwrite a block, and FreeBSD has been collecting the change ever since.