Building a correct distributed system is really, really hard. Building a correct distributed system that has to store data and never lose it is even harder.

Many of our customers build systems where storing data reliably is paramount. Storage is hard even when nothing goes wrong. For example, some customers watch how full their disks are and compact data when necessary to reclaim space, and shard it across machines before any one disk hits its limits. Some maintain a write-ahead log so they can recover from a process restart. Others rely on a careful sequence of operations to guarantee durability and atomicity, for example writing a new copy of a file and renaming it over the old one, so that a change happens completely or not at all.

Unfortunately, storage devices are unreliable, which only adds further complexity to every interaction with the disk. Consider a single write. If it fails with an error, the system has to decide whether it is safe to retry or whether it has to give up. If it fails because the disk is out of space, the system may have to trigger compaction before it can continue. Even creating a file can fail because the filesystem has run out of inodes, and the system then has to delete other files to make room.

Moreover, unreliability can come in the form of latency. A disk can be slow, for example because of I/O contention or a failing device, which can then stretch operations out so they overlap in ways they normally wouldn’t. If a read and a write end up happening at the same time, the reader may see the old data, the new data, or a random interleaving of the two.

Complexity also arises once you assume the disk will not always be there. If a write succeeds and the machine loses power immediately afterwards, a read after the restart could see all of the data, a random part of it, or none of it.

On the Antithesis Forward Deployed Engineering team, we’ve had recurring discussions with customers who build this critical software, and who would like to inject disk faults or storage errors during their Antithesis simulations to mimic these kinds of situations and to ensure their software can handle it. One of the first things we realized is that different customers had vastly different expectations of their storage, and of the guarantees it should provide. This was further complicated by some running on the ext4 filesystem, and some on XFS or ZFS, each with its own behaviors under failure.

This diversity of requirements has made it hard to build a one-size-fits-all solution up front. Instead, we worked with individual customers to build several solutions, geared to their specific use cases. For instance, to test space reclamation logic we’d simulate filling up the whole filesystem.

Other approaches include shouting at the disks.

Once we’d got a sense of the requirements of different customers, we worked to incorporate these pieces of the puzzle into a comprehensive filesystem fault injection mechanism. In this post, we’ll give a tour of the disk faults that a “nightmare mode” fault injector needs to be able to emulate. We’ll start with the fault everyone asks for first, power failure, and explain what it took to emulate it convincingly. From there we’ll get into the subtler things a disk can do: torn writes, I/O errors (including the one behind the fsyncgate bug), and the latency faults that shake out concurrency bugs.

Before we get into faults, we’ll give some background on how file writes work in the “happy path” case.

The happy path

When a program writes to a file, the data does not go to disk right away. Instead, it goes into the kernel’s page cache. The kernel then writes it to disk on its own schedule, through a process called writeback. If a program needs the data on disk immediately, it can call fsync, which tells the kernel to write that file’s data to disk and doesn’t return until the write has finished. Data that is written but not fsynced could be destroyed by a power loss.

The diagram below shows these steps for a successful write to disk:

Diagram of a successful write: the program writes data into the kernel page cache, then fsync flushes it to disk.

Power loss

Now let’s start looking at what happens when things go wrong. We started with power loss because it seemed like the fault most of our customers would care about.

Antithesis already has a related fault type, node kills, which randomly SIGKILL a container. The SIGKILL destroys the process, but has no effect on the machine, which keeps running, so the kernel and its page cache carry on as if nothing happened. Every write the process completed still makes its way to disk, fsync or no fsync, courtesy of the kernel’s writeback mechanism. As far as the process is concerned, a SIGKILL is a fairly polite way to die, since you only lose data that the process never handed to the kernel.

The diagram below shows how data that was written but never fsynced survives the SIGKILL and lands on disk anyway:

Diagram showing written but unsynced data surviving a SIGKILL: the kernel's writeback still flushes it from the page cache to disk.

A power failure is less polite. It loses everything the kernel accepted but hadn’t synced yet. Data that has been written but not yet fsynced is simply gone:

Diagram showing a power failure: written but unsynced data in the page cache is lost and never reaches disk.

Unfortunately, when we’re simulating faults in Antithesis, we can’t just pull the plug on the machine. So the next best option was to fake it. That meant dealing with the two things that stop a container kill from causing data or cache loss: the kernel’s page cache, which holds on to unsynced data, and the kernel’s writeback, which saves unsynced data to disk.

The problem was that both of these live inside the kernel, out of our reach. Currently, there is no way to ask the kernel to forget what’s in its cache, or to stop the kernel’s automatic writeback. However, we found a way around this: LazyFS, an open-source filesystem from INESC TEC and Jepsen.

LazyFS uses FUSE, a Linux feature that lets a userspace program act as a filesystem. Because every read and write goes through LazyFS, it can hold written data in its own internal cache, emulating the kernel’s page cache in user space. Writes to LazyFS stay in the internal cache until the program calls fsync, and only then does LazyFS write it to persistent storage. Also, there is no background writeback to save it in the meantime. This gives us the ability to “forget” unsynced data at any convenient moment, simply by clearing the cache. If we do that at the same moment we kill the application, the two together are indistinguishable from a real power loss.

Here’s a diagram of we can use LazyFS to emulate power loss:

Diagram of emulated power loss with LazyFS: unsynced writes held in LazyFS's internal cache are discarded when the application is killed.

From LazyFS to Amnesia

Before settling on FUSE, we looked at a couple of other mechanisms for injecting faults. eBPF was tempting, since it makes some faults, like returning a particular error code from a syscall, easy to implement. However, it would be difficult to emulate power failure, since it only allows us to watch and tweak calls as they go by. It can’t hold on to data and release it (or lose it) later, which is the trick we needed. We also thought about using a custom block device, but that had the opposite problem: it can hold data, but it sits at the wrong level of abstraction. Since it’s below the filesystem, it sees blocks rather than syscalls, which makes syscall-level faults hard to implement. Power loss is especially tricky because you can’t tell data that arrived through an fsync from data the kernel’s writeback happened to push out. That would cost us the strictness that catches missing fsyncs.

So LazyFS was very useful to us, and we want to extend a thank you to the authors for writing such a high quality tool. We ran a patched version (extended with additional fault types) for a while on a number of customer tenants, and it found some subtle data loss bugs.

After that, we decided to implement our own FUSE filesystem: Amnesia. We wanted to move to Rust for its memory safety, and to rework the design so the model was simpler and faster, since we planned to build a lot more on top of it.

For instance, Amnesia works directly on inodes, whereas LazyFS uses the path for every operation and has to map it back to the inode. Since a path is only a name for a file, this makes operations like renames, hard links, and deleting a file that is still open awkward to track. Working with inodes instead makes that complexity disappear. Amnesia also never touches a real disk. LazyFS writes synced data through to the backing filesystem. Amnesia keeps both the volatile state and the “durable” state in memory, which allows us to model an fsync as a bookkeeping update instead of another real I/O. This makes it much faster, and just as safe, because the machine never actually loses power. We made a number of other changes too, which we will get into below.

Beyond the design, LazyFS introduced us to the subtler effects of a real power loss: torn writes and torn sequences of writes, both caused by the fact that the kernel and the disk do not persist data in the order or the units a program might expect.

The LazyFS paper by Ramos et al. is a good read if you want the details.

It also made us aware of the wrinkles of working at the FUSE layer. For instance, the kernel page cache still sits in front of the FUSE filesystem, and it can be holding copies of the data we just “lost” by emulating a power failure. Thankfully, LazyFS had already worked out the right FUSE settings and patterns to make sure the program never sees them.

Disk I/O errors

Building on FUSE made it easy to add disk I/O errors, another fault type that our customers wanted. The obvious approach is to simply return the error, and that model alone was enough to find some bugs. But because we emulate the kernel page cache, we can sometimes apply the possible side effects of these errors to it, which gives us a higher degree of fidelity.

Out of the box, LazyFS does not simulate disk I/O errors as a form of fault injection, so this was new ground for us. We added the ability to return a few disk I/O error types on syscalls. One of the most interesting ones was EIO (“Error: Input/Output”, the generic “disk failed” error) on fsync.

This error can interact with the kernel page cache in surprising ways. This is exactly what’s behind the infamous fsyncgate bug. The discovery led to PostgreSQL fixing how it handles a failed fsync (it now crashes and recovers from its log rather than trying again), and it changed how the rest of us read fsync’s promise for good. As Craig Ringer, the PostgreSQL developer who reported the problem, put it:

When fsync() returns success it means “all writes since the last fsync have hit disk” but we assume it means “all writes since the last SUCCESSFUL fsync have hit disk”.

To understand why Craig is correct, we need some background on what happens during an fsync on a typical Linux filesystem such as ext4. The kernel’s page cache holds file data in fixed-size chunks called pages, and marks a page as “dirty” when it has been changed but not yet written to disk. When a program calls fsync, the kernel sends each of the file’s dirty pages to the storage device through a driver. Crucially, the kernel clears a page’s dirty flag before handing it off. The fsync then waits for the driver to report whether each write succeeded.

Now suppose one of those writes to the device fails. The fsync will then return EIO, but nothing goes back and marks the page dirty again. As far as the kernel is concerned, that page is now clean, so neither the next fsync nor the background writeback will ever try to submit it to the device again. Since there are no dirty pages associated with the file, the next fsync succeeds. Unless the program happens to write the same data again and re-dirties the pages, that data never reaches the disk.

We can emulate this scenario in the FUSE layer. When the fault fires, fsync returns EIO and the affected pages are marked clean in our internal cache, without being written to persistent storage. A retried fsync then succeeds, but the data will be lost at the next emulated power failure unless the program writes it again and calls fsync.

Concurrency testing

Building on FUSE also let us add “disk latency”, another commonly requested fault type.

Disk latency is really about finding concurrency bugs. Talking to the disk can be slow and unpredictable, and programs that assume otherwise can have races. These races can occur for several reasons. For example, data may land on the disk in an unpredictable order, or land in the kernel page cache in pieces rather than all at once. So we wanted to make those races easier to find.

We currently support two kinds of latency fault:

  1. Completing operations out of order. Instead of answering syscalls in the order they arrive, we let the fuzzer decide the order they complete in.

    Since Amnesia sits underneath the program and sees every operation as a request, it can decide whether to answer immediately or hold on to the request. For instance, when data from a write operation arrives, we sometimes hold onto it with a deadline while other operations continue to be processed (the deadline provides a backstop: once it passes, the operation finishes no matter what). The fuzzer then decides when the parked operation should be executed next.

    Diagram of operations completing out of order: Amnesia parks a write while later operations complete, and the fuzzer decides when the parked write finishes.
  2. Partially completed operations. Syscalls aren’t atomic, and a read racing a write can see the write partially applied. Suppose a program overwrites a few kilobytes of a file while another thread reads the same range. On ext4 the reader can see the old contents, the new contents, or a patchwork of old and new chunks. We let the fuzzer pick which of those the reader gets.

    We implemented this in Amnesia by holding a write and splitting it into small chunks. Instead of applying them all, we let the fuzzer decide which chunks are applied immediately and which are held back. Combined with the out-of-order completion above, this means a read that arrives while the write is parked will likely be answered from the partially applied state, so it sees the same mix of old and new data that a real race could produce.

    Diagram of a partially applied write: the write is split into chunks, some applied and some parked, so a concurrent read sees a mix of old and new data.

Similarly, reads can be held and split into pieces that are captured at different moments. For instance, a single read can return the start of the file from before a write and the end from after it. With several operations parked on the same file at once, the fuzzer picks the order they finish in. We ensure that every combination it tries is a legal interleaving that the kernel page cache or disk could have produced.

Choosing when to inject faults

We built our FUSE filesystem to work hand in hand with the Antithesis fault injector and fuzzer. This lets us inject power failures, torn writes and disk I/O errors at moments our fuzzer considers “interesting”. It also means disk faults are controlled the same way as our other faults, so we get the benefits of swarm testing along the way.

Unlike our normal node kills fault, which could be interesting at any moment, with power failures we only need to sample one moment from one of the three categories of moments where a power failure can land: before the syscall, during it, or after it. Consider a single disk operation. Within “before” or “after”, the exact timing doesn’t matter: if power failure is emulated anywhere in that window it will lead to the same outcome. The interesting states that are uncovered “during” an operation are relatively short, so these moments are rare especially in systems that touch the disk infrequently. By tying the fault to the disk operation itself, we let the fuzzer spend less energy finding these vulnerable states and more energy exploring from them.

Our disk fault injector helps the fuzzer by decreasing the uninteresting state space it has to explore. Instead of letting the fuzzer cut the power at arbitrary instants, it makes the decision on each syscall and injects the fault from inside the FUSE filesystem.

Each time the program is in the middle of a disk operation, there are three specific points where the injector may fire a power failure: before any of the operation has been applied to the internal cache, after part of it has been applied, and after all of it has been applied (but before the syscall returns). These three points line up with the three categories above: nothing applied, partly applied, or fully applied. When it does fire a power failure, the FUSE filesystem kills the container, deletes the internal cache, and makes sure any active syscalls are handled correctly. We call these correlated faults, because the timing of each power failure is tied to a specific disk operation.

Nightmare mode in practice

Our work on disk fault injection has already earned its keep by uncovering a number of bugs in customer software. One open-source bug that we can share is in bbolt, the storage engine behind etcd. We injected all the disk faults in this post while testing the system, but the relevant ones for this particular bug are power failure and error injection.

The issue comes from bbolt committing a transaction in two steps that are each followed by an fsync. First it writes the new data pages, then it writes a single meta page that points at them.

We found a case where the data pages are written successfully, but the fsync of the meta page fails with an EIO. That causes etcd to exit, which is fine.

On restart, bbolt reads the meta page back through the kernel page cache, where the unpersisted meta page still exists, and wrongly treats that transaction as committed. It rebuilds its free page list from that meta page, so the next commits can reuse pages that the on-disk meta page still points to.

Once the cached meta page is evicted, through our emulation of memory pressure or power loss, what remains on disk is an old meta page pointing at overwritten pages. From then on bbolt panics at every open. The etcd node’s write-ahead log can’t help rebuild the store, because the node never gets past opening it.

This bug could only be found by an I/O error followed by a power failure, which is exactly the kind of problem our disk fault injection finds.


We’ve covered a lot of ground: power failures, torn writes and torn sequences, disk I/O errors (including the one behind fsyncgate), and the latency faults that shake out concurrency bugs. All of these are available in our disk fault injector today, and the list keeps growing as customers tell us what keeps them up at night.

Our disk fault injector is customizable, so you don’t have to turn everything on. You can start with the faults that worry you most — power loss for a storage engine, for example, or I/O errors — and add more over time.

If you’re an existing customer, ask your forward deployed engineer to deploy disk faults in your environment. If you’re not, and you’d like to see how your system holds up in nightmare mode, get in touch.

And if there’s a disk failure you’ve been bitten by that we haven’t covered, we’d love to hear about it. The best faults in this post came from exactly those conversations :)