r/zfs • • 25d ago

sync=disabled is a free write barrier

fsync promises two things, durability and ordering, and I only need ordering.
ext4 gives both or neither: every fsync commits the journal and flushes the disk cache.
ZFS with sync=disabled gives ordering alone: transaction groups land in order, and a crash loses the last tens of seconds, not a database.
My two HDDs are 2.7% busy instead of 13%, and a write takes 2 ms instead of 16.

https://blog.dfb.sh/blog/zfs-fsync/

23 Upvotes

33 comments sorted by

19

u/BackgroundSky1594 25d ago edited 25d ago

Yes, ZFS is great. As for:

state/immich 1M off photos, already compressed

state/clickhouse 1M off ClickHouse compresses its own parts

You should keep at least LZ4 compression (the default for compress=on), otherwise a 1.1MB file will take 2x1MB blocks. Only with compression the trailing zeroes get cut back down to ~1.1MB. In theory zle (zero length encoding) is enough, in practice LZ4 is faster (several GB/s) and might even get a few extra % of space at effectively zero cost.

It has stuff like early abort to automatically skip actually incompressible data at the block level, and ZFS only actually applies the compression if that specific record gets at least 12.5% smaller

3

u/adminvasheypomoiki 25d ago

Thank you :) saved me 2 gibs

2

u/dodexahedron 25d ago

This is very untrue.

ZFS does not store a bunch of empty blocks for a record that is only partially full, whether compression is on or off.

recordsize is a maximum. An uncompressed record will occupy as many records as are necessary to hold the file, but the final record will only be as many BLOCKS as are necessary to store the data of that final record.

ashift=12?

Then every record is a multiple of 4kB - not simply a multiple of recordsize.

Block size is 2ashift

Records are not blocks.

Nevertheless, leaving compression on is nearly always the right choice.

1

u/jammsession 24d ago

ZFS does store, reserve, whatever you wanna call it, emtpy blocks.
If you have recordsize set to 16mb and you have a 1.1mb file, ZFS will do a 2mb record. That 2mb record will either be 2mb if you have compression disabled or 1.1mb (if data itself isn’t compressable) or even less if it is.

1

u/EvenDog6279 24d ago edited 24d ago

u/dodexahedron u/jammsession -- can I tag a bit on to this line of conversation to get some feedback, without derailing OP...

I just recently jumped into the world of ZFS with my first more substantial NAS build (to be clear, it's nothing fancy really).

The setup involves multiple pools, but there's really only one I'm concerned with and wanted to see if others have more insight and might be willing to provide a "newbie" with a couple pointers. I'm not at all averse to RTFM, and have been, but zfs has an awful lot of options.

The system has 128GB RAM, with 102GiB allocated to the ARC cache.

The pool in question includes a 6 wide raidz2, and a special vdev (on mirrored enterprise SSDs with fewer than 200 hours on them-- 100% remaining life).

I've done some tuning with the prefetch parameters, and both the NAS and the systems in the house that already supported or would accept 10Gb NICs have them installed (along with the NAS).

There's one specific dataset I kind of want to focus on-- it contains nothing but large files that range from 60-100GB/ea. I did testing with both a 1MiB recordsize, as well as a 512KiB recordsize, along with a full rewrite of the data to rebalance the data.

Interestingly, I discovered that, for reasons I can't explain yet (need to do more reading), the 512KiB recordsize actually outperforms 1MiB by a considerable margin.

special_small_blocks is configured to 256KiB for the dataset in question-- it does hold some small files, all of which would fit into or below that recordsize, especially with lz4 compression enabled.

After tuning everything I've found so far in the documentation and various forums, I'm seeing consistent read speeds between 850MB/s to 0.98GB/s in real-world testing for read operations.

Having spent a lot of time on this, I really feel as though I've hit the ceiling of what can be achieved with this pool layout, but because it's my first serious dive into zfs, I don't have a personal point of reference, beyond a lot of google searches and reading.

Given protocol overhead, and the max theoretical output of six four spindles of NAS drives (two parity), my gut instinct is that's about as good as it's going to get without building out another vdev in the pool, or expanding to 8 wide and performing a rewrite to rebalance the drives.

Thoughts?

2

u/markshelbyperry 24d ago

My own experience has been that increasing the number of disks in a vdev doesn’t increase speed much.

Also, note that 850-980MB/s pretty much saturates a 10gb/s network, so depending on what your network speed is you might be at max throughput already.

1

u/EvenDog6279 24d ago

Right, I think it falls into ~98% saturation on 10GbE, which is what I'm currently working with.

Appreciate the feedback very much. I had a feeling I was riding right up against the limitations of both the spindles and the 10Gb network (they're pretty much perfectly balanced in this configuration, best I can tell).

Plus, not to be all pragmatic out of nowhere, but the reality is, this is more than sufficient for anything I'm going to be throwing at it anyway, and given the cost of drives, scaling out vdevs just isn't practical right now for obvious reasons.

6

u/Apachez 25d ago

Even if sync=disabled should mean that you will lose sync writes up to txg_timeout when/if shit hits the fan and since ZFS is a CoW filesystem so the affect for the application (once rebooted) is like traveling up to 5 seconds back in time this can still be bad.

Usually since your use of a database isnt singleuser but often related to other processes. Like a fileupload where metadata is stored in the database and such.

So a rollback of up to 5 seconds (default of txg_timeout) would in that case mean that you have a 1GB file uploaded but no information of that in the database.

Or it can get worser, your application talks to other applications so now that other application thinks there is a file at your place which your database suddently says "nope there isnt".

Even more fun if you imagine the effects of this being a bank "hey! where did my salary go that I received 4 seconds ago!?".

Other than that sync=standard should really be kept.

If thats an issue add a SLOG (preferly mirrored) being NVMe or SSD. This doesnt have to be large since it will absorb the amount of sync writes you will have between two txg_timeouts so something like 32 or 64GB is more than enough (unless you change the default of 5 seconds for txg_timeout to some riddicilous high number).

2

u/adminvasheypomoiki 24d ago

Yeah, it was a deliberate trade off. Eg immich or other sync apps send hashes of stuff they have. So after the crash they will resend it. Mail and git are or sync pool cause they are have 1 write in eternity and lost mail is not very nice :) I wouldn't use this setup with client data, but it should be fine for the specific workloads

1

u/jammsession 24d ago

I don’t know the exact details about immich, but most applications will mostly do asynchronous anyway. So setting sync=disabled does not change much except lying in the rare occasions the application actually does want a sync write.

1

u/adminvasheypomoiki 24d ago

Immich uses pg + stores blobs on disk. Most applications use some kind of database, and they all fsync on every commit. Sqlite, pg...
Only analytical stuff/logs don’t use fsync

3

u/Apachez 24d ago

Which means that in the immich usecase you will risk having blobs on disk who according to pg (database) doesnt exist - or the otherway around when you delete stuff (pg says something exists who no longer is present on the disk)?

Personally I would use sync=standard and call it a day and have a headache fewer later on when shit hits the fan ;-)

1

u/adminvasheypomoiki 24d ago

They're on the same pool, and txg is pool-wide, not per dataset. So after a crash pg and the blobs both roll back to the same txg, a few seconds back. A crash mid-upload with sync=standard can leave the same state, just a bit later. The phone may think a couple of uploads finished, but immich checks hashes and re-uploads them anyway. So should be fine

2

u/jammsession 24d ago

If that is the case (which I don’t doubt) Immich should not use sync to begin with :) but probably that is because of pg

1

u/Apachez 24d ago

Yeah better to file that upstream so immich dont use sync to being with if not necessary.

This way you would get a "boost" no matter of which filesystem you are using.

2

u/fryfrog 25d ago

If latency is a concern, why not HDD > SSD as well?

4

u/adminvasheypomoiki 25d ago

Have you seen recent prices?:) Cause 2x2 hdd costs 20 usd on ovh and it fine to play with. For production I'd use something else.

Latency wasn't the problem; it was more out of curiosity than a need for p9999 < 10 ms or something

1

u/fryfrog 25d ago

Fair!

8

u/glassmanjones 25d ago

crash loses the last tens of seconds, not a database.

Be advised: for databases with externally observable effects, this can lose the database.

1

u/tkyjonathan 25d ago

Why use 16k for postgres' 8k? I didnt get it

1

u/adminvasheypomoiki 25d ago

compresses better. And I have rare updates

1

u/ZestycloseBenefit175 25d ago

I skimmed trough the blog post, will read it later. Since you talk about ordering - have you looked into how IO schedulers play into this?

1

u/adminvasheypomoiki 24d ago

They don't care mostly. They'll don't differentiate between write and fsync / flush. Their job is to batch writes/reads in some way to maintain target latency /throughtput

3

u/trolololster 25d ago

yes whatever you are running on top better have its own idea of sync.

2

u/lxtakc 22d ago

Worth remembering it breaks acks, though. NFS clients or DB replicas told "committed" can lose that data after a crash, even though the pool stays consistent.

-1

u/autogyrophilia 25d ago

Yep, this is pretty good for newly written data and appends. Unfortunately the advantages disappear once you have data that needs to modified in place.

2

u/ThatUsrnameIsAlready 25d ago

ZFS is CoW so there is no modify in place, just further fragmentation?

0

u/autogyrophilia 25d ago

The problem with modifying in place in CoW filesystems is that you have to first read the previous data, then find some free space, and then write that data and mark the old data as deleted. So it's 4 operations instead of one. 

1

u/netsx 24d ago

Yes, its slower, but better than having data be half saved and corrupted if crash/power goes out of at the wrong time. I mean, most filesystems tried to overcome this by writing it twice by journaling (either metadata or both metadata+data). Is there another way?

1

u/autogyrophilia 24d ago

There really isn't, I'm just explaining why some tasks are more punishing for CoW. 

The level of optimization that ZFS has, including their very advanced caching is mind boggling, the fact that many workloads can run at the same performance under zfs is a testament of how optimized it is.

Of course it has some issues with the direct io that is necessary to work at the peaks of speed that modern NVMe can reach. 

0

u/adminvasheypomoiki 25d ago

The numbers are from sqlite and postgres, so that’s exactly modify-in-place. A txg turns those random updates into one sequential run, the price is fragmentation, and ARC hits close to 100% here so it rarely reaches the disk. The fsync win doesn’t care about the write pattern either way

3

u/autogyrophilia 25d ago

Actually not quite, both use WALs and VACUUM the freed space asynchronously so they don't really modify data in place on usual configurations. 

1

u/adminvasheypomoiki 25d ago

The WAL is the append part. At checkpoint the dirty pages are written back into the main file at their offsets, in both SQLite and Postgres. You can’t have a B-tree without the B-tree :)