Linux Internals

Direct I/O vs the Page Cache in Linux: When to Bypass the Cache

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

When an application reads or writes a file on Linux, the data normally passes through the page cache, an area of RAM where the kernel keeps copies of file contents. This buffered I/O is what makes a second read of the same file nearly instant and lets writes return before the disk has finished. Direct I/O, requested with the O_DIRECT flag, skips that cache and moves data between the application's memory and the storage device. Neither is better in general. This guide explains how each path works, when bypassing the cache helps, and the pitfalls that catch people out.

How buffered I/O works

With a normal open():

  • Reads check the page cache first. On a miss, the kernel reads the data from disk into the cache, often with readahead for sequential access, then copies it to the application's buffer.
  • Writes copy data into the cache and mark the pages dirty. The call returns immediately, and kernel writeback threads flush dirty pages to disk later.
  • Durability requires fsync() or fdatasync(), or opening with O_SYNC or O_DSYNC. Until then, a power loss can lose recently written data.

The page cache uses memory that would otherwise sit idle and is released when applications need RAM. For most software, including web servers, file servers and build tools, buffered I/O is the right default. We explain the cache in more depth in the Linux page cache.

How direct I/O works

With open(path, O_RDWR | O_DIRECT), the kernel transfers data using DMA directly between the device and the user buffer, with no copy in the page cache. Effects:

  • No double caching when the application already has its own cache.
  • No extra memory copy, which saves CPU at high throughput.
  • No readahead and no write-behind: each request waits for the device.
  • Predictable memory use, because the page cache does not grow with file I/O.

Comparison at a glance

AspectBuffered I/ODirect I/O (O_DIRECT)
Repeated readsServed from RAMGo to the device every time
Small writesFast, absorbed by the cacheEach one hits the device
Memory usePage cache grows with dataApplication manages its own buffers
CPU copiesExtra copy through the cacheData moves straight to user memory
Alignment rulesNoneBuffer, offset and length must be aligned
Typical usersMost applicationsDatabases with their own cache, benchmarks, VM storage

Alignment requirements

Direct I/O has strict rules. The user buffer address, the file offset and the transfer length usually must be multiples of the device's logical block size, commonly 512 bytes or 4096 bytes. Violations return EINVAL. Check the device's block sizes with:

lsblk -o NAME,LOG-SEC,PHY-SEC
cat /sys/block/vda/queue/logical_block_size

On Linux 6.1 and newer, statx() can report the exact alignment a file needs through STATX_DIOALIGN. In C, allocate aligned buffers with posix_memalign(), and use 4096-byte alignment to be safe on most devices.

O_DIRECT is not the same as durable

A common misconception is that direct writes are on stable storage when the call returns. They have reached the device, but the device may have a volatile write cache, and file metadata such as size and block allocation may not be committed. For crash safety, still call fdatasync() or open with O_DSYNC. Databases that use direct I/O do exactly this.

Who should use direct I/O

  • Databases with their own buffer pool. MySQL and MariaDB InnoDB support innodb_flush_method = O_DIRECT, a common recommendation on Linux, so data is cached once in the buffer pool instead of twice. Size the buffer pool accordingly, since the OS cache no longer helps.
  • Storage benchmarks. Without direct I/O, a test may measure RAM rather than the disk.
  • Hypervisors and VM disk images, where the guest already caches data.

PostgreSQL, by contrast, has long relied on the page cache, so its shared_buffers is typically set to only a fraction of RAM. Follow your database's own documentation rather than forcing O_DIRECT.

Testing disk performance correctly on a VPS

To measure the storage of a virtual server instead of its memory, use direct I/O:

# Sequential write, 1 GB, bypassing the page cache
dd if=/dev/zero of=testfile bs=1M count=1024 oflag=direct status=progress

# Random 4K reads with fio
fio --name=randread --filename=testfile --size=1G --rw=randread \
    --bs=4k --direct=1 --ioengine=libaio --iodepth=32 --runtime=30 --time_based

Delete testfile afterwards. Results from --direct=1 are lower than buffered numbers, but they reflect what a database will experience on cache misses. If you want a clean server to run these tests, you can deploy a VPS in a few minutes.

Pitfalls to avoid

  • Mixing buffered and direct access to the same file can return stale data; pick one mode per file.
  • Small unbatched direct writes are slow; batch them or use asynchronous I/O such as io_uring or libaio.
  • Some filesystems, such as tmpfs, do not support O_DIRECT and open() fails.
  • Turning on direct I/O for an application without its own cache usually makes it slower.

FAQ

Does O_DIRECT make my application faster?

Only in specific cases, mainly when the application already caches data itself or streams large amounts of data that will not be reread. For typical applications, buffered I/O through the page cache is faster.

Why does my disk benchmark show very high speeds?

Without direct I/O, writes land in the page cache and reads may come from RAM, so you are measuring memory. Use oflag=direct with dd or --direct=1 with fio, and a test size larger than RAM if you must use buffered mode.

Do I still need fsync with direct I/O?

Yes, if you need data to survive a crash. Direct I/O bypasses the kernel cache, but it does not guarantee that the device's write cache or the file metadata has been flushed. Use fdatasync() or O_DSYNC for durability.

Related guides