MySQL swapping with Fsync
[sylvester@host~]$ free -m
total used free shared buff/cache available
Mem: 16046 10861 242 16 4941 4735
Swap: 15255 821 14434 The point that needs a bit more attention here is the amount of memory being used by the OS cache. As you can see, there is a total of 16046M of physical memory available to the host, with only 10861M in use and the majority of what’s left over being used by the OS cache. This typically isn’t a problem. When requests for more memory come in from threads running in the OS, it should evict pages from the OS cache in order to make memory available to the requesting process. In this case, this did not occur. Instead, we observed that it held onto that cache memory and forced MySQL to turn to swap. But why? As it turns out, the system in question had recently been converted from MYISAM to InnoDB and hadn’t had any server configuration set to accommodate for this. As such it was still configured for innodb_flush_method at the default value,
which in 5.7 is still fsync. Both
Ivan Groenwold
and I have both written blog posts in regards to flush methods, and it’s been generally accepted that O_DIRECT is a much better way to go in most use cases on Linux, including this one, so we wanted to get the system in question more aligned with best practices before investigating further. As it turns out, we didn’t have to look any further than this, as switching the system over to innodb_flush_method = O_DIRECT resolved the issue. It appears that fsync causes the kernel to want to hang onto its data pages, so when innodb attempted to expand its required amount of memory, it was unable to do so without accessing swap, even with swappiness set to 0 to test. Ever since we did the change to O_DIRECT, the OS cache usage has dropped and there have been no problems with OS cache page eviction.
Conclusion
MySQL swapping can really ruin your day and it’s something you want to avoid if at all possible. We still run into issues with swapping every now and then and want to continue to provide the community with our findings as they become available. So if you have a server that is swapping, and the OS cache isn’t making room for MySQL, and if you’re still using fsync for InnoDB flush, consider switching to O_DIRECT.On this page
Share this
Share this
More resources
Learn more about Pythian by reading the following blogs and articles.
InnoDB flushing and Linux I/O
InnoDB flushing and Linux I/O
May 5, 2016 12:00:00 AM
2
min read
Log buffer #548: a carnival of the vanities for DBAs
Log buffer #548: a carnival of the vanities for DBAs
Jul 5, 2018 12:00:00 AM
2
min read
Exposing Innodb Internals via System Variables: Part 1, MemoryExposing Innodb Internals via System Variables: Part 1, Memory
Exposing Innodb Internals via System Variables: Part 1, MemoryExposing Innodb Internals via System Variables: Part 1, Memory
Aug 18, 2016 12:00:00 AM
9
min read
Ready to unlock value from your data?
With Pythian, you can accomplish your data transformation goals and more.