1. Overview
When mounting a filesystem in Linux, we can use either sync or async mode. In this article, we’ll learn about the differences between the two modes and the situations in which it may be better to use one over the other.
2. Differences Between Sync and Async
Sync and async are two different methods for mounting a filesystem. Sync is short for synchronously and means that I/O operations to the drive will occur exactly when they occur on the filesystem. Async is short for asynchronously and means that I/O operations to the drive will stay in memory/buffer for some time rather than immediately writing to disk.
2.1. Sync
Mounting a filesystem with sync is typically not the default option for most filesystems. For starters, sync is single-threaded, so it is only able to utilize the speed of one CPU core. Sync also puts more strain on the device, as it reads and writes immediately rather than waiting to buffer multiple I/O operations together. This will decrease speed, especially for hard drives, which will have to move their drive heads more as the number of I/O operations increases.
However, there are some benefits of using sync. One benefit is that sync sequentially performs I/O operations, so it has more predictable behavior. Another benefit is that sync is less susceptible to data loss, as it won’t wait multiple seconds to write to disk. We should prefer sync when data safety is the highest priority.
2.2. Async
A filesystem using async intelligently buffers I/O operations to optimize performance and extend the health of a drive. There are considerable speed benefits to using async, as it uses multi-threading to utilize multiple CPU cores at a time. Async keeps data in memory before writing, so multiple operations to a file incur fewer actual I/O operations to the drive. Async will also extend the lifespan of a drive as HDDs and SSDs both have a limited number of read and write operations before they start developing issues.
The downsides of using async mostly have to do with data loss. Async can wait multiple seconds before syncing data between buffer and drive. This may cause data loss when the system loses power unexpectedly or when a program is killed. Async is best in most situations but is especially suited for when drive latency is high and/or performance is most important.
2.3. mount With Sync and Async
To mount a filesystem in Linux, we can use the mount command. Let’s mount a filesystem on our computer using sync:
$ mount -o sync /dev/drive1 /mnt/sync_mount
In this example, we use the mount command and specify the option we want to use with -o. In this case, the option we set is sync.
Usually, async is the default mount option, so we should be able to just leave the option blank if we want our drive mounted with async. In situations where async isn’t the default, we can just use -o async in place of -o sync.
There’s also another option that allows us to use sync for directory updates only:
$ mount -o dirsync async /dev/drive3 /mnt/dirsync_mount
In this example, we use mount with -o dirsync to specify we want to use sync for directory changes. All other changes will use async.
2.4. ext4 Filesystem With Sync and Async
The ext4 format is the fourth and newest filesystem format in the ext (extended filesystem) line. ext4 comes with many additional options, including some that allow for customizing the behavior of sync and async.
One option with ext4 is specifying the maximum time data may go unsynced between buffer and device when using async:
$ mount -o commit=3 /dev/drive4 /mnt/ext4_mount
Here, we used the mount command with the -o commit option. This ensures data syncs to drive at least every 3 seconds. The default value for commit is 5 seconds. A larger commit time increases speed and performance but with a higher risk of data loss.
ext4 also allows setting the amount of time to wait for additional operations to be batched together when the drive is being synced:
$ mount -o max_batch_time=500 /dev/drive4 /mnt/ext4_mount
In this example, we use the mount command with max_batch_time set to 500. The max_batch_time option is the time (in microseconds) to wait when a sync operation is being used for other operations to be completed in sequence with one another. This will increase sync and async performance but should have a higher impact on sync, as it performs more operations than async does.
3. Conclusion
In this tutorial, we learned that sync and async are two different methods of mounting a filesystem, each with its pros and cons. Sync emphasizes safety, while async emphasizes performance and drive health. Certain filesystem formats like ext4 may allow for additional customization of sync and async behavior.