Cloud disks, such as Google Cloud's, are billed by the size you provision, so storing fewer bytes should cost less. ZFS, a file system that runs on Linux through OpenZFS, can compress data as it writes it. This article explains when that saves money, what it costs in CPU, and how ZFS decides what to compress.

Contents

  1. Disks are billed by size, and they only grow
  2. Putting numbers on the saving
  3. How ZFS organizes data
  4. Follow a write
  5. Two rules decide the real saving
  6. Follow a read
  7. Before choosing a codec
  8. Sources

Disks are billed by size, and they only grow

On Google Cloud, you pay for the disk size you ask for, not for the data you put on it so if you provision a 200 GB disk, you are billed for that entire disk space, regardless of how you use it (pricing). And a disk can only grow. You can't reduce the size of a disk; to shrink one, you replace it with a smaller one (resize docs). Hyperdisk volumes work the same way: You can't reduce the size of a Hyperdisk volume (Hyperdisk docs).

So compression saves money in two ways. A disk you already have fills more slowly, so you resize it later. And when you set up a new system or migrate an old one, you can provision a smaller disk from the start.

The price is CPU time. ZFS spends it compressing data when it writes (zio.c) and decompressing data when it reads, even when that data is already in RAM (there is an exception coming below) (arc.c).

Here is what the disk space costs. These are on-demand list prices for zonal disks (disks that live in a single zone) in us-east4, as of 2026-11-16 (pricing).

Disk type (zonal, us-east4) Price per GiB-month
pd-balanced $0.11
pd-ssd $0.187
Hyperdisk Balanced $0.08, plus IOPS and throughput above the included baseline

Regional disks cost twice as much (pricing). Committed-use discounts, which lower prices in return for a one- or three-year commitment, aren't available for these disks (CUD overview).

Putting numbers on the saving

Compression pays off most on data that is written once and rarely read again. An event store is a good example: new rows are appended at the end, old rows are seldom read, so most of the compression CPU is paid once, when the data is written.

Our generated, append-only events data set compressed 3.20× with zstd-1 and 3.01× with gzip-9, as ZFS reports it in its compressratio statistic. Our numbers come from a benchmark we ran on Google Cloud VMs with OpenZFS 2.4.1: 512 MiB per data set, stored in 128 KiB records. zstd and gzip are compression algorithms, or codecs, and the number is the level: 1 is fastest, and the top level (9 for gzip, 19 for zstd) gives the best compression ratio (zfsprops(7)).

A ratio of N× means 1/N of the bytes are left, so the saving is:

saving=1−N1​at 3.20×:1−3.21​=0.6875=68.75%

So at 3.20× you save 68.75%, not 220%.

Ratio Bytes left Saving
2× 50% 50%
3× 33.3% 66.7%
3.20× 31.25% 68.75%
4× 25% 75%

Each step up is worth less than the last: going from 3× to 4× saves only another 8.3 percentage points.

Now put numbers on it. Take 10 TiB (10,240 GiB) of history. The disk has to be bigger than the data, for two reasons:

  • Room to grow. We assume you keep 20% free.
  • Slop space. A ZFS pool (the storage ZFS builds from your disks) can't be filled to the last byte. ZFS holds back "the last 3.2% (1/2^spa_slop_shift)" of the pool (zfs(4)). In the code it is about min(32pool size​, 128 GiB) , counting a small area set aside for the intent log, where ZFS records sync writes (more on those below) (spa_misc.c). Both disks in this example are 4 TiB or more, so both hit the 128 GiB cap. Smaller pools use 3.2%.

That makes the disk roughly:

disk≈0.8data​+128 GiB

With our numbers:

 uncompressed zstd-1, 3.20× data 10,240 GiB 3,200 GiB ÷ 0.8 12,800 GiB 4,000 GiB (20% free) + slop 128 GiB 128 GiB disk 12,928 GiB 4,128 GiB 

Bar chart: 10,240 GiB of data needs a 12,928 GiB disk uncompressed and a 4,128 GiB disk at 3.20×. Monthly cost in us-east4 drops from $1,422 to $454 on pd-balanced, $2,418 to $772 on pd-ssd, and $1,034 to $330 on Hyperdisk Balanced (capacity only).

At 3.20×, the disk and the monthly bill both shrink to about a third.

Over a year, that saves $11,616 on pd-balanced and $19,747 on pd-ssd.

On a disk you already have, compression delays the next resize: at a steady growth rate, a disk that would fill in one year uncompressed lasts 3.2 years.

There is one catch: a smaller disk can also be a slower one. Google warns: "Since disk performance grows linearly with the size of your disk, consider your I/O needs when choosing the size of your disk" (pricing). So a smaller Persistent Disk can have lower limits on IOPS (read and write operations per second) and throughput.

Hyperdisk Balanced works differently. It includes 3,000 IOPS and 140 MBps and bills performance above that separately (pricing).

Before counting on any of these numbers, check whether your database already compresses its own data, because compressing it twice gains little. Elasticsearch compresses stored data with LZ4 by default, or with zstd under index.codec: best_compression (Elastic docs). PostgreSQL compresses values through TOAST only when a row is wider than about 2 kB (PostgreSQL docs).

How ZFS organizes data

To see where the CPU goes, it helps to know how ZFS lays data out.

On Linux, a file system is the layer that turns a raw disk into files and directories. Many file systems, such as ext4, sit on one block device: a disk, a partition, or a volume that another tool, such as LVM, builds from several disks. ZFS manages the disks itself: you give it disks, it combines them into a pool, and you create file systems, called datasets, inside that pool.

ZFS doesn't compress whole files. It splits each file into records and compresses each record on its own.

Diagram: a pool of disks holds datasets; each dataset has properties such as compression and recordsize; a dataset holds files; a file is stored as records of up to 128 KiB; each record is compressed on its own and stored in 4 KiB sectors.

Each record is compressed alone and stored in whole sectors.

From the top down:

  • Pool: "a collection of devices that provides physical storage" for datasets (zpool(8)).
  • Dataset: a file system or volume in the pool (zfs(8)).
  • Property: a named setting on a dataset, such as compression (zfsprops(7)).
  • Record: a file's unit of storage, which ZFS also calls a block. recordsize, 128 KiB by default, is the largest record size (zfs_prop.c, zfs.h).
  • Sector: the smallest unit ZFS writes to a disk, 2^ashift bytes. ashift=12 means 4 KiB (zpoolprops(7)). ashift is set per vdev (vdev.c). A vdev is one of the disks or disk groups (a mirror, or a RAID-Z group) that a pool is built from (zpoolconcepts(7)).

Compression is controlled by the compression property. If you set nothing, it is on, which means the lz4 codec when the pool's lz4_compress feature is enabled, as it is on current pools (zfsprops(7), zio_compress.c). If you change it later, the change "affects only newly-written data" (zfsprops(7)).

Follow a write

When an application saves data, it calls write(), a system call: a request to the Linux kernel, the core of the operating system, in this case to write bytes to a file. A successful write() doesn't promise the data is on disk yet (write(2)). For an ordinary, buffered write, ZFS keeps the data in memory and compresses it seconds later, when it flushes a batch of changes to disk.

Flow chart: write() copies data to memory as dirty data; a transaction group sync at least every 5 seconds, or sooner, compresses each record; if it saved enough it is stored compressed, otherwise it is stored raw with the CPU already spent.

Compression CPU is spent at transaction group sync, not at write(), even when the record ends up stored raw.

Step by step:

  1. write() copies the data into memory (zfs_vnops.c). Data waiting in memory to be written is called dirty data. If dirty data hits its limit, "new writes are halted until space frees up" (zfs(4)).
  2. ZFS writes dirty data out in batches. A batch is a transaction group (txg): changes "written to disk in the syncing state" (txg.c). Writing one out is called a txg sync. It happens "at least every" zfs_txg_timeout seconds, 5 by default (zfs(4)), and sooner when:
    • dirty data passes 20% of zfs_dirty_data_max (zfs(4)), or
    • an admin action waits on it, such as zpool sync (zpool-sync(8)) or a snapshot (txg.c).
  3. During the sync, zio_write_compress compresses each dirty record on its own (zio.c).
  4. The codec gets an output budget (Rule 1 below). If the compressed result doesn't fit, ZFS stores the record uncompressed (zio_compress.c, zio.c).

A record that doesn't fit wastes the CPU spent on it. Codecs differ in how much they waste:

  • lz4 and gzip (on Linux) stop as soon as their output overflows the budget (lz4_zfs.c, spl-zlib.c).
  • zstd-3 and higher first try a cheap lz4 pass on records of 128 KiB or more. If lz4 fails, they try zstd-1. They give up only if both fail (zfs(4), zfs_zstd.c). On compressible data, that lz4 pass is extra CPU on every write.

Two kinds of write don't wait for the next txg sync. ZFS compresses them when the application writes or syncs them:

  • Some sync writes. An application calls fsync(), or opens a file with O_SYNC, when it needs its data on disk before it carries on (fsync(2), open(2)). ZFS records sync writes in its intent log (ZIL). For some, it writes the data straight into the pool as full records and logs only a pointer to them, which compresses them right away. This happens with logbias=throughput. On pools without a separate log device, it also happens to most sync writes of 32 KiB or more (zil.c, zfs(4), dmu.c, zfs_vnops.c).
  • O_DIRECT writes aligned to recordsize. O_DIRECT is a flag that asks the kernel to minimize caching of a file's I/O (open(2)). By default, such writes bypass ZFS's RAM cache (zfsprops(7)) and go straight to the write pipeline (dmu_direct.c).

Two rules decide the real saving

Disks store data in fixed-size sectors. With ashift=12, the common setting, a sector is 2^12 bytes = 4 KiB. Think of sectors as fixed-size boxes: ZFS compresses each record on its own and stores it in whole boxes, so it can save less than the same codec would on the whole file. Two rules decide how much.

A 128 KiB record is 32 sectors. Its budget rounds down to 28 sectors; it compresses to 30,000 bytes (7.3 sectors) and is stored in 8 whole sectors, so 24 are saved and the stored ratio is 4.00x, not 4.37x. An 8 KiB record is 2 sectors; its 87.5% budget of 7 KiB rounds down to 1 sector, so it must compress to 4 KiB, a 50% saving.

Both rules at ashift=12. A 128 KiB record saves 24 of its 32 sectors; an 8 KiB record has to halve to save anything.

Rule 1: a record must save enough. ZFS gives the codec a budget of 87.5% of the record (a 12.5% saving), rounded down to whole sectors. If the compressed record doesn't fit, it is stored uncompressed (zio.c).

budget=⌊sector size0.875×record size​⌋ sectors

Rounding down hurts small records most. At ashift=12 on ordinary vdevs (single disks, mirrors or RAID-Z groups):

Record size Must compress to at most Minimum saving
128 KiB 112 KiB 12.5%
16 KiB 12 KiB 25%
8 KiB 4 KiB 50%
4 KiB 112 bytes 97%

The last row is a special case. A 4 KiB record's budget rounds down to zero sectors, so it stays compressed only if it fits in 112 bytes inside its block pointer, the small piece of metadata that records where a block lives. That needs the embedded_data pool feature, which is on by default, and it never happens on encrypted datasets (zio.c, zpool-features(7)).

No tunable changes the 12.5%. A larger ashift means bigger sectors and coarser rounding. It is set per vdev when the vdev is created or added (vdev.c), and changing the pool's ashift property later "will not modify any existing vdev" (zpoolprops(7)).

Rule 2: what's stored is rounded up to whole sectors. A record that compresses to 7.3 sectors still takes 8 (zio.c). ZFS counts that padding in the block's size and in compressratio (zio.c), so compressratio already includes this loss.

stored=⌈sector sizecompressed size​⌉ sectors

Here are both rules applied to the 128 KiB record in the diagram, with 4 KiB (4,096-byte) sectors:

record​=131,072 bytes=32 sectors budget​=⌊0.875×32⌋=28 sectors compressed​=30,000 bytes≈7.3 sectors≤28, so it fits stored​=⌈7.3⌉=8 sectors=32,768 bytes​

The last sector carries 2,768 bytes of padding, which lowers the ratio:

30,000131,072​≈4.37× before rounding32,768131,072​=4.00× as stored

Rounding can also make different codecs land on the same ratio. gzip-9, zstd-1 and zstd-9 all reported a compressratio of 4.56× on our JSON logs. We did not inspect each record's stored size, so rounding is only a possible cause.

What the two rules mean in practice: bigger records keep more of the saving. Both rules work in whole sectors, so each one costs less than one sector per record. In a 128 KiB record (32 sectors), that is a small loss. In an 8 KiB record (2 sectors), it can be half the record.

Grouped bar chart computed from the two rules at ashift=12, assuming every record compresses to the same fraction. Data that compresses 2.5 times, to 40%, where the codec alone would save 60%: saves 0% in 4 KiB records, 50% in 8, 16 and 32 KiB records, 56% at 64 KiB, 59% at 128 KiB and 60% at 1 MiB. Data that compresses 1.5 times, to 67%, where the codec alone would save 33%: saves 0% at 4 and 8 KiB, 25% at 16 and 32 KiB, 31% at 64 and 128 KiB, and 33% at 1 MiB. Footer: small records can lose all of the saving and 1 MiB records keep almost all of it, but a small read still decompresses the whole record, so bigger records cost more per read.

Same data, different record sizes: small records lose part or all of the codec's saving.

Take data that compresses 2.5×, which the codec alone would shrink by 60%. In 128 KiB records it saves 59% of the disk space. In 8 KiB records it saves 50%, and in 4 KiB records nothing at all. Data that compresses only 1.5× saves nothing below 16 KiB.

That doesn't mean you should always raise recordsize. A small read decompresses the whole record, as the next section shows, so bigger records cost more CPU per read. The ZFS docs describe recordsize as "designed solely for use with database workloads that access files in fixed-size records", and suggest setting it "greater than or equal to the record size of the database" (zfsprops(7)). It can be up to 128 KiB, or up to 16 MiB with the large_blocks pool feature (zfsprops(7)), and a change "affects only files created afterward" (zfsprops(7)).

In short: data written and read in big pieces keeps more of its saving in large records; data read in small pieces is better served by a recordsize that matches the application's own record size.

Follow a read

Writes pay for compression once. Reads can pay for decompression again and again, even when the data never leaves RAM.

Like most file systems, ZFS keeps recently used data in RAM so repeated reads don't have to go to disk. Its main RAM cache is the ARC. By default, the ARC keeps blocks as they are on disk, compressed (arc.c, arc.c). That fits more data in RAM. ZFS on Linux 0.7.0 made this the default, and its release notes call the result a larger effective cache (release notes). The cost is that a block has to be decompressed before it can be used.

Three read paths for a 4 KiB request against a 128 KiB compressed record: an uncompressed copy in RAM, usually in the dbuf cache, copies 4 KiB with no decompression; an ARC hit with only the compressed block decompresses the whole 128 KiB; a disk read caches the compressed block in the ARC, then decompresses the whole 128 KiB.

For a compressed record, an ARC hit decodes the whole record even when you asked for 4 KiB, unless an uncompressed copy is still in RAM.

Say an application reads 4 KiB from a file. The read takes one of three paths, from cheapest to most expensive.

Uncompressed copy: no decompression. The copy is usually in the dbuf cache, a list of recently used, uncompressed blocks. It was added with compressed ARC "to keep frequently access data uncompressed" (commit), and the read uses it directly (dbuf.c). The copy can also be another ARC buffer, which ZFS copies (arc.c).

But the dbuf cache is small. By default it targets 1/32 of the ARC's current target size, with no fixed cap (dbuf.c, dbuf.c, zfs(4)).

ARC hit, compressed: decompress the whole record. ZFS turns any byte range into whole blocks (dmu.c). Then it decompresses each block into a new full-size buffer (arc.c, arc.c). Your 4 KiB read decompresses all 128 KiB.

Disk read: the same full decompression. ZFS keeps the block compressed in the ARC, then decompresses the whole block (arc.c, arc.c).

Blocks stored raw skip decompression on every path (arc.c).

In our benchmark, on JSON and event files cached in the ARC, 96–97% of each test's 10,000 random 4 KiB reads decompressed a full 128 KiB record.

Before choosing a codec

Two questions do most of the work: how well your data compresses, record by record, and how often you read it back.

A 2x2 grid. Across: how often you read the data back, rarely or often. Up: how well it compresses, per record. Compresses well and rarely read: store it small, zstd-1, and test a higher zstd level if capacity is tight, because compression CPU is paid once, at write. Compresses well and read often: balance both, lz4 or zstd-1, because most ARC hits decompress the whole record even for a 4 KiB read; measure read CPU on your own CPUs. Compresses poorly and rarely read: little to gain, lz4 or off, because a record that can't save 12.5% is stored raw with the CPU already spent, and lz4 gives up early. Compresses poorly and read often: reads skip decompression, lz4 or off, because raw records skip decompression but every write still pays for the attempt. Footer: data your database already compresses counts as compressing poorly; then measure on your own hardware.

Find your data's box.

A checklist to help you decide:

  1. Read/write mix: do you write once and rarely read, or read the same data all day? (which means you'd probably want to utilize a cache)
  2. Request size: how small are your reads compared with recordsize?
  3. Where reads land: an uncompressed copy, the ARC or disk?
  4. Data: how well does it compress, record by record? Does your database already compress it?
  5. CPU: how much headroom do you have, and how bursty is the load?

Sources

OpenZFS source and man pages are pinned to tag zfs-2.4.1 (upstream). Google Cloud prices are us-east4 on-demand list prices, retrieved 2026-11-16.