Understanding Barriers I/O: Ensuring Data Reliability in Storage Systems

Understanding Barriers I/O: Ensuring Data Reliability in Storage Systems

Barriers I/O is a critical mechanism for ensuring data write reliability. Its core principle is to force the operating system and storage devices to confirm that previous critical data has been physically written to storage before executing subsequent I/O operations, thereby preventing data loss or corruption.

  1. Why Do We Need Barriers I/O?

By default, operating systems and storage devices use caching mechanisms to temporarily hold data for writing to physical disks (HDDs, SSDs) rather than writing immediately. This "delayed write" approach works fine under normal circumstances, but in cases of sudden power loss, system crashes, or other unexpected events, data in the cache that hasn't been written to disk will be lost, potentially causing:

  • File system corruption (such as disordered partition tables, lost file directories)
  • Incomplete critical data (such as uncommitted database transactions, fragmented log files)

Barriers I/O was developed to address these isues by ensuring "forced disk flushing" to guarantee data reliability.

  1. Core Principles of Barriers I/O

It ensures data is truly written through two layers of "forced synchronization":

  • Operating System Layer: Forces the operating system to immediately send pending data from the "page cache" (the temporary data storage area in memory) to the storage device, prohibiting delayed writes.
  • Storage Device Layer: Requires storage devices (such as SSDs, disk arrays) to immediately write data from their own caches to the physical medium (such as flash memory chips, disk platters) and return a "write complete" confirmation signal.

Only after both layers complete they operations does Barriers I/O allow subsequent I/O requests to proceed.

  1. Application Scenarios

Barriers I/O is primarily used in scenarios with extremely high data reliability requirements, including:

  • Database Systems (such as MySQL, PostgreSQL): Ensures that transaction logs and data blocks are not lost, preventing transaction rollback failures.
  • File Systems (such as Ext4, XFS): During formatting, partition adjustments, or updates to critical metadata (such as file names, permissions), it prevents file system structure corruption.
  • Logging Systems (such as Kafka, ELK): Ensures log data is actually written to disk, preventing log gaps due to unexpected events that could affect troubleshooting.
  1. Comparison Between Barriers I/O and Standard I/O

Comparison Aspect Barriers I/O Standard I/O
Primary Goal Prioritizes data write reliability to prevent data loss or file corruption Prioritizes I/O performance to maximize data transfer efficiency
Caching Strategy Forbids "delayed writes", immediately flushes data from OS cache and device cache to physical storage medium Relies on "cached delayed writes", data first stored in memory cache or device cache, written to physical medium in batches when conditions are right (cache full, reaching time threshold)
Reliability Very high. Even with sudden power loss or system crashes, data triggered by Barriers has been written to disk with no risk of loss Lower. In unexpected situations, uncached data not yet written to disk will be lost directly, potentially causing file disorder or database transaction failures
Performance Lower. Each I/O requires waiting for "cache flushing + device confirmation", increasing operation delay, not suitable for high-frequency write scenarios Higher. Reduces direct interactions with physical storage through batch writing, lowering latency, suitable for scenarios with lower reliability requirements
Use Cases Database transactions, file system metadata updates, log writes and other core data scenarios Regular file copying, video playback caching, temporary file storage and other non-critical data scenarios
  1. Implementation and Verification Guide (Linux Systems)

File System Layer (Using Ext4 and XFS as Examples)

File system Barriers functionality is typically enabled by default, but can be checked and adjusted via commands to avoid失效 due to configuration changes.

Checking Current Barriers Status

Use the mount command to view mount parameters for the target partition to determine if Barriers are enabled.

# View mount information for all partitions, filtering for parameters containing "barrier"
mount | grep -E "barrier|data=ordered"

# Example: When Barriers are enabled on an Ext4 partition, output will include "data=ordered" (the default mode for Barriers)
# /dev/sda1 on / type ext4 (rw,relatime,data=ordered)

# Example: When Barriers are enabled on an XFS partition, output will include "barrier=1"
# /dev/sdb1 on /data type xfs (rw,relatime,attr2,inode64,barrier=1)

Key Determination Criteria:

  • Ext4: Presence of data=ordered or barrier=1 indicates it's enabled.
  • XFS: Presence of barrier=1 indicates it's enabled; barrier=0 means it's disabled.

Enabling/Adjusting Barriers Configuration

If Barriers are not enabled, they can be temporarily or permanently enabled by remounting the partition or modifying /etc/fstab.

# 1. Temporary enable (lost after reboot): Remount the target partition with Barriers parameters
# Ext4 partition example (/dev/sda1 mounted to /mnt)
mount -o remount,barrier=1 /dev/sda1 /mnt

# XFS partition example (/dev/sdb1 mounted to /data)
mount -o remount,barrier=1 /dev/sdb1 /data

# 2. Permanent enable (persists after reboot): Edit /etc/fstab and add Barriers configuration to the target partition's mount parameters
vim /etc/fstab

# Ext4 partition configuration example (add "barrier=1" after "defaults")
/dev/sda1  /mnt  ext4  defaults,barrier=1  0  2

# XFS partition configuration example (add "barrier=1" after "defaults")
/dev/sdb1  /data  xfs  defaults,barrier=1  0  2

# After editing, execute the following command to apply /etc/fstab configuration (no reboot needed)
mount -a

Database Layer (Using MySQL as Example)

MySQL requires configuration parameters to associate with Barriers I/O to ensure reliability for transaction logs and data file writes, primarily relying on the innodb_flush_log_at_trx_commit parameter.

Checking Current MySQL Barriers Configuration

View key parameters via the MySQL command line to determine if a Barriers-compatible flushing strategy is enabled.

# After logging into MySQL, execute to view InnoDB log flushing configuration
show variables like 'innodb_flush_log_at_trx_commit';

Paramter Value Meanings (related to Barriers I/O):

  • 1 (Recommended, fully enables Barriers effect): On each transaction commit, forces logs to be flushed from memory to physical disk (relies on file system Barriers for protection), ensuring zero data loss.
  • 2: On transaction commit, logs are only flushed to the OS cache, relying on periodic OS flushing, still carrying risk of loss during power outages (not fully utilizing Barriers).
  • 0: Logs are flushed in batches every 1 second, not dependent on transaction commits, highest risk of loss (Barriers-related protection not enabled).

Enabling MySQL Barriers-Compatible Configuration

Modify the MySQL configuration file to set innodb_flush_log_at_trx_commit=1, ensuring it works in coordination with file system Barriers.

# 1. Edit the MySQL main configuration file (path may vary by system)
# CentOS/RHEL:
vim /etc/my.cnf
# Ubuntu/Debian:
vim /etc/mysql/my.cnf

# 2. Add or modify the following parameters under the [mysqld] section
[mysqld]
innodb_flush_log_at_trx_commit = 1  # Forces transaction log flushing to match Barriers I/O reliability requirements
innodb_flush_method = O_DIRECT     # Optional, skips OS page cache for direct device writes, further improving reliability

# 3. Restart MySQL service to apply configuration
# CentOS/RHEL:
systemctl restart mysqld
# Ubuntu/Debian:
systemctl restart mysql

# 4. Verify configuration is applied (execute MySQL command again)
show variables like 'innodb_flush_log_at_trx_commit';
# Output "Value: 1" indicates successful configuration

Tags: storage systems data reliability I/O Optimization Linux filesystems Database Configuration

Posted on Sat, 31 Oct 2026 16:30:58 +0000 by rp2006