Linux I/O Scheduler Configuration: Benchmarking mq-deadline, BFQ, and Kyber for Real-World Workloads

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

Problem Statement: When Fast Storage Hits a Performance Wall

You slot a 7,000 MB/s Gen4 NVMe drive into your server or provision a premium cloud SSD. You expect near-instant throughput, but your PostgreSQL queries suddenly stall out at 800ms. Worse, a routine nightly tar backup spikes disk activity, freezes your shell prompt, and drives %wa (I/O wait) in top into the 40% range while overall CPU usage sits near idle.

Hardware raw speed is only part of the equation. Every single block read, write, WAL sync, and paging operation must pass through the Linux kernel block layer. When hundreds of processes contend for storage concurrently, the kernel decides which requests get served first, which merge together, and which wait.

Ubuntu, Debian, and RHEL often default to a generic scheduler or pass requests straight through with none. While great for pure throughput, this setup can starve transactional reads during large write bursts. Matching your scheduler to your workload fixes these stalls without spending a dime on faster storage.

Core Concepts: Modern Multi-Queue (blk-mq) Schedulers

Legacy single-queue schedulers like classic deadline, CFQ, and noop were retired in Linux 5.0. Modern kernels run on blk-mq (Multi-Queue Block Layer), which scales parallel queues across multi-core systems. Today, you will work with four main options:

  • none: A pure FIFO pass-through that skips kernel reordering. It hands raw queues directly to storage controllers capable of parallel handling.
  • mq-deadline: A multi-queue adaptation of the deadline engine. It splits reads and writes into separate queues and enforces strict expiration timeouts to prevent starvation.
  • BFQ (Budget Fair Queueing): An interactive scheduler that assigns dynamic sector budgets per process. It gives foreground jobs immediate access even when a saturated background backup floods the disk.
  • Kyber: A lightweight, self-tuning algorithm designed for high-IOPS NVMe drives. It tracks real-time read and write latencies and throttles queue depths automatically whenever response times drift past targeted thresholds.

Choosing the Right Scheduler for Your Workload

Workload / Storage Type Recommended Scheduler Why It Works
Fast NVMe Drives (PCIe Gen3/4/5) none Cuts CPU overhead. Enterprise drives handle up to 64,000 queues in hardware without kernel arbitration.
Relational Databases (PostgreSQL, MySQL) mq-deadline Prioritizes read latency over streaming bulk writes, maintaining sub-10ms query response times.
Interactive Desktop / Workstations bfq Guarantees low latency for foreground apps; audio and terminal windows stay smooth during package builds.
High-IOPS Web APIs (Fast SATA/NVMe) kyber Enforces strict latency ceilings (e.g., 2ms reads) with minimal CPU tracking overhead compared to BFQ.
Mechanical Hard Drives (SATA HDD) bfq Minimizes mechanical head travel through continuous request merging while protecting interactive processes.

Hands-on Practice: Inspecting, Testing, and Automating Schedulers

Tuning your block devices requires three straightforward steps: checking active configurations, applying live runtime adjustments, and making rules persistent across reboots via udev.

Step 1: Check Your Active I/O Scheduler

Storage attributes live under /sys/block/. Start by checking your available disks and media types using lsblk:

lsblk -d -o NAME,ROTA,SIZE,TYPE,MODEL

The ROTA flag shows whether the drive spins: 1 indicates a mechanical HDD, and 0 marks an SSD or NVMe drive. Next, check the current scheduler assigned to your drive (adjust sda or nvme0n1 to match your device):

cat /sys/block/sda/queue/scheduler

The output lists available schedulers, wrapping the active choice in brackets:

[mq-deadline] kyber bfq none

Step 2: Switch Schedulers on the Fly

You can hot-swap schedulers instantly without unmounting partitions or restarting services. This flexibility makes direct A/B benchmarking straightforward under live traffic.

To configure a target drive for mq-deadline:

sudo tee /sys/block/sda/queue/scheduler <<< "mq-deadline"

To prioritize interactive responsiveness on a workstation SATA SSD:

sudo tee /sys/block/sda/queue/scheduler <<< "bfq"

Confirm the change took effect:

cat /sys/block/sda/queue/scheduler

Step 3: Fine-Tuning mq-deadline for Low-Latency Databases

PostgreSQL and MySQL depend heavily on fast random reads. When an active query requests a table page missing from shared_buffers, the thread stops and waits for storage. In contrast, bulk writes—such as transaction logs and checkpoint flushes—can safely buffer in write queues.

On a 32GB RAM production database host experiencing 30ms read spikes during hourly checkpoints, tuning mq-deadline dropped our 99th percentile query latency below 5ms. We shortened the read expiration window to prioritize reads while deferring write pressure:

# Check the active read deadline (default: 500ms)
cat /sys/block/sda/queue/iosched/read_expire

# Cut read deadline to 100ms so pending select queries preempt writes
sudo tee /sys/block/sda/queue/iosched/read_expire <<< "100"

# Extend write starvation threshold to 4000ms (default: 5000ms)
sudo tee /sys/block/sda/queue/iosched/write_expire <<< "4000"

Step 4: Making Your Configuration Permanent with udev

Direct writes to /sys vanish on reboot. To enforce predictable policies across every reboot and drive connection, create a dedicated udev rule.

Open a new configuration file in /etc/udev/rules.d/:

sudo nano /etc/udev/rules.d/60-ioschedulers.rules

Add rules matching your drive bus types and rotational properties:

# NVMe SSDs: bypass kernel scheduling to leverage controller hardware queues
ACTION=="add|change", KERNEL=="nvme[0-9]*n[0-9]*", ATTR{queue/scheduler}="none"

# SATA SSDs: apply mq-deadline to protect read latency on flash media
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="mq-deadline"

# Rotational HDDs: apply BFQ for fair scheduling and request coalescing
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="bfq"

For desktop workstations, change line 5 from mq-deadline to bfq. That tweak prevents background apt upgrades from stuttering active audio streams and browser windows.

Reload the rule engine and trigger it across active block devices:

sudo udevadm control --reload-rules
sudo udevadm trigger --type=devices --action=change

Confirm the final scheduler status across all drives:

for dev in /sys/block/{sd*,nvme*}; do
  [ -e "$dev/queue/scheduler" ] && echo "$dev: $(cat $dev/queue/scheduler)"
done

Step 5: Verifying Performance and Queue Depths

Monitor your disk queue metrics with iostat from the sysstat package during active operations:

sudo apt install -y sysstat
iostat -xz 2

Evaluate two key metrics:

  • r_await and w_await: The average time (in milliseconds) spent servicing read and write requests. Under tuned mq-deadline policies, r_await holds steady under 5ms even while w_await climbs during large writes.
  • aqu-sz: The average queue length of outstanding requests. If this climbs above 30 on standard SATA SSDs, your system is queuing too deeply and introducing tail-latency spikes.

Conclusion

You rarely need to throw budget at faster drives to solve persistent storage lag. Matching your Linux kernel scheduler to physical media and application access patterns eliminates pipeline stalls at the root. Set modern NVMe storage to none to avoid CPU bottlenecks.

Apply mq-deadline on SATA SSDs hosting databases for predictable transaction latency. Use kyber on high-concurrency microservices, and leave bfq to manage mechanical spinning disks and desktop sessions. Test each adjustment with synthetic load tests, persist your settings through udev, and monitor queue latency to keep operations running smoothly.

Share: