[TUTORIAL] ZFS: SLOG & ZIL & sync writes

IsThisThingOn

Well-Known Member
Nov 26, 2021
594
380
88
This tutorial might leave out some details, and in parts be written a little bit sloppy. Sorry if you don't like that writing style, sorry. If you have suggestions on how to edit it to make it better, feel free to add stuff.

There seems to be a lot of misunderstandings, when it comes to ZIL, SLOG, PLP, sync writes and so on.
Let us look what the different options are, when writing data onto your ZFS pool.

async without SLOG:
A async write, goes into ARC (which is in RAM) into TXGs (transaction groups).
By default, the TXGs have the to goal to collect around 5 seconds and then write that to the pool. I won't use the word cache here, since it is not traditional cache. Yes, I it can speed up writes, since it can collect many writes into one sequential one.
That part of ARC isn't read cache, so that part is NOT shrinkable. ZFS calls this zfs_dirty_data, because it is dirty write data.

This is sometimes important in the Proxmox context. You can fill your RAM with VMs and bet on ARC making room if needed, but it can't shrink that TXGs. So multiply you NIC speed with 5 and you have roughly the amount that isn't shrinkable. Add some cushing to that, this is the amount of RAM you should mentally reserve for ZFS.

sync write without SLOG:
A sync write is written to TXG and to ZIL at the same time. Since we don't have a SLOG, ZIL is on the pool*.
So again, we write the data to TXG in ARC (RAM) and ZIL (pool) at the same time.
ZFS reports back that the write finished, after it was written to ZIL.
After the TXG is written onto the pool, the data in ZIL gets discarded.
So in a normal operation, ZIL is never ever read from! Never.

Only when the system halts, crashed, looses power, this changes.
During the import process for a zpool, ZFS checks the ZIL for any dirty writes. If it finds some , it will replay them from the ZIL, aggregating them into TXG, and committing the TXG to the pool. So your pool import during boot will take a little bit longer, but everything is fine.

What happens if you add a SLOG?
ZIL moves from the pool, to that SLOG device. Nothing more. Huge implications though.

With the knowlege from above applied, what does that mean?
For async, nothing. Async never touched ZIL to begin with.
For sync? ZIL is now on SLOG. So imagine you do a sync write.
The write goes into your ZIL which is on your hopefully extremly fast SLOG. And at the same time into TXG. It will be pretty fast be finished on ZIL and ZFS will report back that the write is finieshed. Even though in reality, it isn't. It is in TXG in RAM. But in case something goes wrong, it can replay it from SLOG. That is the only situation where a read from SLOG happens.
Like above, like for async, the write gets written from TXG onto your pool and after that gets discarded on ZIL/SLOG.

What makes a good SLOG?
One with fast sync writes. Which is probably one with PLP. Why?
A normal drive can't make use of its DRAM cache for sync writes. Because in case of a power failure, that DRAM data (just like RAM) will be lost.
A PLP drive is a liar. Instead of actually wait until the data is written onto NAND, it lies.
Even thoug the data is dirty in DRAM, the drive will say "yeah, yeah I have written it down, don't you worry!".
But it hasn't. In case something goes wrong, it can use it's condensators to finish the write onto NAND. This approach makes PLP so much faster for sync writes than normal drives.
It also explains why, against popular homelabbers believe, PLP drives are NOT more secure. They cover their ass for a lie, while normal drives simply don't lie*.

With that in mind, one could argue that you don't need to mirror SLOG. Since it is is only read from after a halt. So you would have to have a system halt AND your SLOG do give up to loose data. On the other hand, people that care about speeding up sync writes, probably don't care much about the extra costs of a mirror.

Some AFAIK current defaults:
zfs_txg_timeout: 5s
zfs_dirty_data_max: 10% of physical RAM

* can also be on special vdev
** there have been ADATA phision controller firmware bugs in around 2018 or something, where consumer drives lied about sync status. It is extremly rare.
 
  • Like
Reactions: Johannes S and UdoB
Thanks for the great writeup, two remarks though:

* can also be on special vdev
The reason for this is, that beginning with zfs 2.4 ( a patched PVE9 and PBS 4.2 should have it, not sure about PMG though , since I'm not using it ) a special device acts as slog if no dedicated slog is present:

It also explains why, against popular homelabbers believe, PLP drives are NOT more secure

But they are still more durable. First since they have a higher TBW to start with. Second due to the capacitator/cache/due mechanisms they need to write less so I woukd expect less wearout
 
  • Like
Reactions: IsThisThingOn
PLP drives are NOT more secure. They cover their ass for a lie, while normal drives simply don't lie*.
Calling PLP drives “liars” is a cute line, but it’s backwards. A PLP drive is telling the truth. When it acknowledges a flush with data still in DRAM, that data really is durable. The capacitors guarantee it reaches NAND. The DRAM is effectively non-volatile, so the acknowledgment means what it says. That’s the whole design. Consumer drives without PLP do lose data. Without PLP, a drive has to actually write to NAND before acknowledging a flush, which is why sync writes on them are so slow. But power loss can also corrupt data that was already written. The drive’s internal mapping table (FTL) can get damaged, and on TLC/QLC an interrupted program can corrupt previously written pages that share the same cells. PLP protects against that too. Your footnote understates the risk. Consumer drives that mishandle flushes aren’t limited to one ADATA bug around 2018. Academic power-fault studies have found data loss on consumer SSDs, and as recently as a few years ago people testing NVMe drives found several mainstream models losing writes they had already acknowledged as flushed.

https://www.tomshardware.com/news/sk-hynix-sabrent-rocket-ssds-data-loss
https://www.ece.iastate.edu/~mai/docs/papers/2013_FAST_SSD.pdf
 
  • Like
Reactions: Onslow and uzumo
TXGs (write buffer) and ARC (read cache) are different entities in RAM or did I misunderstand sth ?
Also not 100% sure about that one. The write buffer is in RAM, but I guess that it will be counted as ARC. So for example, if you have 1GB in zfs_dirty_data, Proxmox would show 1GB more ARC usage.

Calling PLP drives “liars” is a cute line, but it’s backwards. A PLP drive is telling the truth. When it acknowledges a flush with data still in DRAM, that data really is durable. The capacitors guarantee it reaches NAND. The DRAM is effectively non-volatile, so the acknowledgment means what it says. That’s the whole design.
It is a little bit a chicken & egg discussion. I could argue that if the capcitors break down, you pay with a data loss for that lie. Just like if your BBU in RAID is needed for the lie to work.
Because it isn't really in NAND. Course you could also argue that that point is mood, since if NAND goes bad, that data is also not in NAND.

Consumer drives that mishandle flushes aren’t limited to one ADATA bug around 2018
Your linked PDF is from 2013. I think that a lot of progress happened since than. Nowadays consumer drives have the TBW of 2013 enterprise drives. But yeah, in regards to sync, that is just wishfull thinking of me.

That article you linked, links to the deleted twitter account @xenadu02, which is what I am referring to.
While the twitter post was in 2022, the ADATA drives was from 2018.
Too bad this tweets are no longer online, since there were the only good source I ever found on the topic.

Out of my head, the results where basically this:
- almost all big brands, did a good job. WD, Samsung, Intel and so on
- the only ones that did not do a good job, where some very few smaller brands, that treat is like a side project. Like ADATA. They basically acted as a system integrator for the drive, nothing despite the broken firmware for the controller is done by them, everything is bought.

For me the takeaway was:
- Buy from big, trustworth brands like WD, Samsung and the likes. Yeah, I know SK Hynix is huge, but not their consumer stuff. Some niche SK hynix consumer drive can get away with not honoring sync. A 990 Pro that gets sold a million times can not.
- You never know. That is why I would never use two drives from the same vendor in a mirror. That is IMHO good advice in general, detached from the topic of sync. A Samsung firmware bug that is causing a shutdown instead of throtteling when the drive gets hot, is also bad if you use two of them in a mirror.
 
Last edited: