Storage architectures frequently face a trade-off between speed and capacity. While enterprise NVMe and SATA solid-state drives (SSDs) offer exceptional random I/O throughput and low latency, mechanical hard drives (HDDs) provide vastly cheaper gigabyte-per-pound storage for large multimedia archives, backup pools, and database repositories.
The Linux kernel includes native block-layer caching technology known as bcache. Operating at the block layer beneath filesystems like Ext4, XFS, and Btrfs, bcache transparently routes random reads and writes through a fast SSD (the cache device) while retaining mass data storage on a mechanical HDD (the backing device). By caching frequently accessed blocks and transforming fragmented random writes into contiguous sequential disk streams, bcache delivers near-SSD performance across terabytes of mechanical storage.
Understanding bcache Operating Modes
Before deploying bcache, determine the optimal caching strategy for your workload:
- Writethrough (Default): Data is simultaneously written to both the SSD cache and the backing HDD before returning success to the application. This mode guarantees zero data loss if the SSD fails, but only accelerates read operations.
- Writeback: Data is written directly to the high-speed SSD cache and acknowledged immediately. Dirty blocks are subsequently flushed to the mechanical HDD in the background in sequential batches. This provides maximum write throughput for heavy write loads.
- Writearound: Bypasses the cache for write operations entirely, caching only data that is subsequently read back. Useful for write-once, read-many workloads.
- None: Temporarily disables caching for diagnostic purposes while leaving device mapping intact.
Installing bcache-tools on Ubuntu
The user-space management utilities required to format and register bcache devices are available in standard Ubuntu repositories:
sudo apt update
sudo apt install -y bcache-tools
Identifying Storage Devices
Inspect your block device topology using lsblk:
lsblk -o NAME,SIZE,TYPE,FSTYPE,MODEL
For demonstration, suppose:
/dev/nvme0n1p2: High-speed SSD partition (Cache Device)./dev/sdb: Large mechanical hard drive (Backing Device).
Warning: Creating bcache devices formats the target partitions, destroying all existing data. Ensure you have backed up critical files before proceeding.
Formatting Backing and Cache Devices
To prepare the storage pool, format both devices using the make-bcache command:
# Format the mechanical disk as a backing device (-B)
sudo make-bcache -B /dev/sdb
# Format the SSD partition as a caching device (-C)
sudo make-bcache -C /dev/nvme0n1p2
Upon execution, the kernel automatically generates a new virtual block device named /dev/bcache0.
Attaching the Cache to the Backing Device
To link the cache device to the backing storage, obtain the unique cache set UUID:
bcache-super-show /dev/nvme0n1p2 | grep cset.uuid
Output example:
cset.uuid 4a6f2b71-3829-4d33-a3d8-5b485d4e1234
Attach the cache set to the active bcache instance by writing the UUID into the backing device’s sysfs registration node:
echo "4a6f2b71-3829-4d33-a3d8-5b485d4e1234" | sudo tee /sys/block/bcache0/bcache/attach
Configuring Writeback Caching Mode
To enable high-performance writeback caching for your storage pool:
echo "writeback" | sudo tee /sys/block/bcache0/bcache/cache_mode
Verify that writeback mode is active:
cat /sys/block/bcache0/bcache/cache_mode
Formatting and Mounting the Accelerated Filesystem
Format the virtual /dev/bcache0 device with your preferred filesystem (e.g. Ext4 or XFS):
sudo mkfs.ext4 -L FastStorage /dev/bcache0
Mount the accelerated volume to your desired mount point:
sudo mkdir -p /mnt/fastdata
sudo mount /dev/bcache0 /mnt/fastdata
Configuring Persistent Mount via /etc/fstab
To ensure /dev/bcache0 mounts automatically across system reboots, locate its filesystem UUID:
sudo blkid /dev/bcache0
Add the volume entry to /etc/fstab using its UUID:
UUID=e4b7c123-4567-89ab-cdef-0123456789ab /mnt/fastdata ext4 defaults,noatime 0 2
Auditing Cache Hit Rates and Statistics
Inspect real-time cache performance and I/O efficiency using sysfs statistics:
# Check cumulative cache hit ratio (percentage)
cat /sys/block/bcache0/bcache/stats_total/cache_hit_ratio
# View pending unwritten dirty data
cat /sys/block/bcache0/bcache/dirty_data
With bcache configured, read and write operations access data at solid-state speeds while transparently taking advantage of bulk mechanical disk capacity.