ZFS Compression – A Win-Win

Guest Author

This may sound counterintuitive, but turning on ZFS compression not only saves space, but also improves performance. This is because the time it takes to compress and decompress the data is quicker than then time it takes to read and write the uncompressed data to disk (at least on newer laptops with multi-core chips).

To turn on compression simply run:

pfexec zfs set compression=on rpool

All the child datasets of rpool will inherit this setting. For example:

bleonard@opensolaris:~$ zfs get compression rpool/export/home
NAME PROPERTY VALUE SOURCE
rpool/export/home compression on inherited from rpool/export

Note, only new data written after turning on compression is effected. Your existing data is left in its uncompressed state.

To check the compression you’re achieving on a dataset, use the compressratioproperty:

bleonard@opensolaris:~$ zfs get compressratio rpool/export/home
NAME PROPERTY VALUE SOURCE
rpool/export/home compressratio 1.00x

I’m seeing 1.00x because I just enabled compression. Over time, as I write new data, this number will increase. 

An Example 

This part is optional but will give you a better feeling for how compression works. 

Start by creating a new (therefore, empty) file system, ensuring compression is off (otherwise it will inherit the setting from rpool):

bleonard@opensolaris:~$ pfexec zfs create -o compression=off rpool/test
bleonard@opensolaris:~$ cd /rpool/test

Copy a file into the test file system, turn on compression, and copy it again:

bleonard@opensolaris:/rpool/test$ pfexec cp /platform/i86pc/kernel/amd64/unix unix1
bleonard@opensolaris:/rpool/test$ pfexec zfs set compression=on rpool/test
bleonard@opensolaris:/rpool/test$ pfexec cp /platform/i86pc/kernel/amd64/unix unix2

Check the compression ratio:

bleonard@opensolaris:/rpool/test$ pfexec zfs get compressratio rpool/test
NAME PROPERTY VALUE SOURCE
rpool/test compressratio 1.28x -

Check the difference in file size:

bleonard@opensolaris:/rpool/test$ du -hs u\*
1.7Munix1
936Kunix2

Also note the difference between using du and ls:

bleonard@opensolaris:/rpool/test$ ls -lh
total 2.6M
-rwxr-xr-x 1 root root 1.6M 2009-04-28 13:32 unix1\*
-rwxr-xr-x 1 root root 1.6M 2009-04-28 13:33 unix2\*

ls does not show the compressed file size! See ZFS Compression, whcn du and ls appear to disagree for a great explanation of this.

Finally, delete unix1, which was not compressed, and notice how the compression ratio for the file system rises accordingly:

bleonard@opensolaris:/rpool/test$ pfexec rm unix1
bleonard@opensolaris:/rpool/test$ pfexec zfs get compressratio rpool/test
NAME PROPERTY VALUE SOURCE
rpool/test compressratio 1.77x -


Finally, clean up:

bleonard@opensolaris:/rpool/test$ cd
bleonard@opensolaris:~$ pfexec zfs destroy rpool/test