Boost HDD Speed by 10x with bcache: Use an Old SSD as Cache on Linux

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

Quick Setup: 5 Minutes to See the Difference

Do you have a sluggish 2TB HDD and a spare 120GB SSD? Don’t let them go to waste. With just a few commands, we can combine both into a single partition: featuring the massive capacity of an HDD and the blazing speed of an SSD.

In this example, /dev/sdb is the HDD (Backing device) and /dev/sdc is the SSD (Caching device). Warning: This operation will wipe all data on both drives.

# 1. Install control tools
sudo apt update && sudo apt install bcache-tools -y

# 2. Wipe old formatting
sudo wipefs -a /dev/sdb /dev/sdc

# 3. Initialize bcache (HDD is -B, SSD is -C)
sudo make-bcache -B /dev/sdb -C /dev/sdc

# 4. Format and use immediately
sudo mkfs.ext4 /dev/bcache0
sudo mount /dev/bcache0 /mnt/data

The result? You’ll see the /dev/bcache0 device appear. Try copying a large file; you’ll notice an extremely high initial write speed as the SSD carries the load.

Why is bcache a “Steal” for Tech Enthusiasts?

In reality, buying a 4TB or 8TB SSD for data storage is often an unnecessary luxury. Bcache (Block Cache) has been a module in the Linux kernel since version 3.10. It allows you to use a high-speed drive (SSD/NVMe) as a buffer for a large but slow drive (HDD).

Instead of spending a fortune on a high-capacity SSD, you only need a 4TB HDD (~$80) paired with a 250GB SSD (~$25). The system automatically promotes frequently accessed data (Hot data) to the SSD, while rarely used data (Cold data) remains on the HDD. In real-world tests, random read speeds can jump from 1MB/s to over 100MB/s depending on your SSD.

Detailed Configuration and Optimization Guide

Step 1: Identify Devices

Before typing any commands, use lsblk to carefully check the drive names. A single character error could lead to accidentally wiping your OS drive.

lsblk

Once identified, wipe the old partition “signatures” to prevent system conflicts:

sudo wipefs -a /dev/sdb
sudo wipefs -a /dev/sdc

Step 2: Establish the Backing-Caching Relationship

There are two roles you need to distinguish clearly:

  • Backing Device (HDD): Where the data actually resides.
  • Caching Device (SSD): The high-speed transit station.

If you have multiple HDDs, you can use a single SSD as a shared cache for all of them by initializing them separately:

sudo make-bcache -B /dev/sdb
sudo make-bcache -C /dev/sdc

Step 3: Register Devices with the Kernel

Normally, bcache is auto-detected. If /dev/bcache0 hasn’t appeared, force the Kernel to register it manually with the following commands:

echo /dev/sdb | sudo tee /sys/fs/bcache/register
echo /dev/sdc | sudo tee /sys/fs/bcache/register

Choose the Right Cache Mode: Avoid Data Loss

Bcache has three main operating modes. Choosing the wrong one could lead to data loss if the SSD suddenly fails.

  • Writethrough (Default): Data is written to both the SSD and HDD simultaneously. Extremely safe, but write speeds are still limited by the HDD.
  • Writeback: Data is written directly to the SSD first, then flushed to the HDD when the system is idle. Write speeds skyrocket, but there’s a risk of data loss if the SSD fails before syncing to the HDD.
  • Writearound: Only caches data on reads. Ideal for servers hosting movies or photos where content rarely changes.

To enable Writeback for maximum performance (e.g., for running databases or compiling code):

echo writeback | sudo tee /sys/block/bcache0/bcache/cache_mode

Monitoring System Health

To check if bcache is actually working, inspect the system file status:

cat /sys/block/bcache0/bcache/state

If it shows clean, everything is perfect. To see the cache “hit” rate (the percentage of data retrieved from the SSD rather than the HDD), use the command:

watch -n 1 tail /sys/block/bcache0/bcache/stats_total/*

Pro Tips from the Field

After years of deploying bcache for home NAS systems and storage servers, here are three things to keep in mind:

  1. Choose Enterprise-grade SSDs: If using writeback mode, the SSD will face constant writes. Prioritize drives with high TBW (Total Bytes Written) ratings like Samsung Pro series or Intel Optane to avoid premature failure.
  2. SSD Removal Procedure: Never unplug the SSD while it’s running. You must switch back to writethrough mode, wait for dirty_data to reach 0, and only then remove the SSD from the system.
  3. Always Have a Backup Plan: Bcache combines two physical devices into one logical unit, doubling the hardware risk. Don’t forget to back up critical data to the Cloud or an external drive.

Bcache is an excellent solution for reviving old machines or optimizing costs for high-capacity storage servers. It provides a smooth experience without breaking the bank.

Share: