O_DIRECT
also: direct I/O, unbuffered I/O
O_DIRECT is a file open flag that instructs the kernel to bypass the page cache and perform I/O operations directly between user-space buffers and disk, reducing memory overhead and latency for certain workloads.
O_DIRECT is a flag used with the open() system call that tells the kernel to perform uncached disk I/O. Normally, file reads and writes go through the kernel's page cache, which improves performance for typical workloads but adds memory overhead and context switching.
When O_DIRECT is enabled, data transfers directly from disk to the application's memory buffer, bypassing the page cache entirely. This reduces memory consumption and is particularly useful for applications that manage their own caching strategy, such as databases (MySQL, PostgreSQL) or data warehouses.
Example: fd = open("/path/to/file", O_RDWR | O_DIRECT); opens a file for direct I/O. Note that buffers must be aligned to the disk's sector size (typically 4096 bytes) for this to work correctly.
Trade-offs include reduced flexibility for the kernel to optimize I/O patterns and potential performance degradation if misused, since the kernel cannot prefetch data or coalesce multiple small I/O operations efficiently.