Bcache
Bcache Overview
Bcache is the cache of the block device layer in the Linux kernel. One or more high-speed drives (such as SSDs) can be used as the cache of low-speed drives. Bcache is incorporated into the kernel mainline from Linux 3.10.
Bcache has the following features:
- A cache device can serve as the cache for multiple devices. Caches can be dynamically added and deleted while the devices are in operation.
- Data can be recovered from an abnormal shutdown. The write completion is confirmed only after cache data is written to a back-end device.
- Write blocking and cache flushing are correctly processed.
- Write cache modes such as writethrough, writeback, and writearound are supported.
- Sequential I/Os are detected and bypassed (with a configurable threshold or the option to disable this function).
- When the SSD latency exceeds the configured threshold, the SSD traffic is reduced (this function is used when an SSD is used as the cache of multiple drives.)
- Prefetch is performed when cache miss occurs (disabled by default).
- High-performance writeback implementation. Dirty data is sorted and then flushed to drives. If the writeback_percent value is set, the background writeback process uses the PD controller to smoothly process dirty data based on the dirty data proportion.
- The Bcache random read rate can reach 1 million IOPS when an efficient B+ tree is used and the hardware devices are fast enough.
- Bcache can run stably in production.
Cache Modes of Bcache
Bcache supports three cache modes: writeback, writethrough (default), and writearound. The cache mode can be dynamically modified.
- Writeback: All data is written to the cache drive first. The system writes the data to back-end data drives later. This mode is disabled by default.
- Writethrough: Data is written to the cache drive and back-end data drives at the same time. This mode is enabled by default and applies to read-dominant workloads.
- Writearound: Data is directly written to back-end drives.
Common Tuning Methods
- Set the writeback mode to improve the write performance.
echo writeback > /sys/block/bcache0/bcache/cache_mode
- Allow cache sequential I/Os or set the sequential I/O threshold.
- Enable cache sequential I/Os.
echo 0 > /sys/block/bcache0/bcache/sequential_cutoff
- Adjust the sequential I/O threshold.
echo 4M > /sys/block/bcache0/bcache/sequential_cutoff
After the preceding settings are performed, the system writes I/O data directly to the back-end data drives when the sequential I/O cache size exceeds the threshold.
- Enable cache sequential I/Os.
- Disable the congestion control function.
echo 0 > /sys/fs/bcache/<cache set uuid>/congested_read_threshold_us echo 0 > /sys/fs/bcache/<cache set uuid>/congested_write_threshold_us congested_read_threshold_us # Defaults to 2,000 μs. congested_write_threshold_us # Defaults to 20,000 μs.
For more information, see the Bcache home page and manual:
Parent topic: Common Features