Direct I/O in InnoDB (O_DIRECT)
InnoDB offers a choice whether it should use buffered or direct (unbuffered) I/O when accessing data files. It is settable in MySQL configuration file through innodb_flush_method parameter.
By default InnoDB uses buffered I/O. It means that when performing I/O operations database in fact interacts with system’s file cache rather than with disk directly. If there is a request to read a block that is already in the cache, then that block is returned immediately. Otherwise data is read from disk and placed into the cache first. When database performs a write, the modified block also is stored into the cache. Afterwards, depending on the innodb_flush_methodvalue, the write may either be synchronously pushed to disk or, much more commonly (and by default), system performs flushing of the modified cache contents asynchronously at certain interval.
However InnoDB already has a complete caching solution built-in called buffer pool. It does not have to rely on the operating system’s caching capabilities for better performance, unlike many other MySQL storage engines. Setting the flush method to O_DIRECT instructs database instance to open InnoDB data files in a special way – using direct I/O mode – that disables system cache on its requests to these files. Database accesses disk directly. Enabling direct I/O isgenerally recommended as it prevents double buffering of information in the system file cache and often results in improved MySQL performance.
Changing innodb_flush_method value cannot be done at run time and requires a database restart.
The known problems with direct I/O:
Some newer versions of MariaDB and Percona Server offer also ALL_O_DIRECT mode in addition to O_DIRECT. The new mode provides direct I/O not only on InnoDB data files, but also on InnoDB transaction logs at the same time.