[This article was written by Jay Janssen]
It seems these days if anyone knows anything about tuning InnoDB, it’s that you MUST tune your innodb_buffer_pool_size to 80% of your physical memory. This is such prolific tuning advice, it seems engrained in many a DBA’s mind. The MySQL manual to this day refers to this rule, so who can blame the DBA? The question is: does it makes sense?
What uses the memory on your server?
Before we question such advice, let’s consider what can take up RAM in a typical MySQL server in their broad categories. This list isn’t necessarily complete, but I think it outlines the large areas a MySQL server could consume memory.
- OS Usage:Kernel, running processes, filesystem cache, etc.
- MySQL fixed usage:query cache, InnoDB buffer pool size, mysqld rss, etc.
- MySQL workload based usage:connections, per-query buffers (join buffer, sort buffer, etc.)
- MySQL replication usage: binary log cache, replication connections, Galera gcache and cert index, etc.
- Any other services on the same server:Web server, caching server, cronjobs, etc.
There’s no question that for tuning InnoDB, the innodb_buffer_pool_size is the most important variable. It’s expected to occupy most of the RAM on a dedicated MySQL/Innodb server, but of course other local services may affect how it is tuned. If it (and other memory consumption on the server) is too large, swapping can kick in and degrade your performance rapidly.
Further, the workload of the MySQL server itself may cause a lot of variation. Does the server have a lot of open connections and active query workload consuming memory? The memory consumption caused by this can be dramatically different server to server.
Finally, replication mechanisms like Galera have their own memory usage pattern and can require some adjustments to your buffer pool.
We can see clearly that the 80% rule isn’t as nuanced as reality.
A rule of thumb
However, for the sake of argument, let’s say the 80% rule is a starting point. A rule of thumb to help us get a quick tuning number to get the server running. Assuming we don’t know anything really about the workload on the system yet, but we know that the system is dedicated to InnoDB, how might our 80% rule play out?
|TOTAL SERVER RAM||BUFFER POOL WITH 80% RULE||REMAINING RAM|
At lower numbers, our 80% rule looks pretty reasonable. However, as we get into large servers, it starts to seem less sane. For the rule to hold true, it must mean that workload memory consumption increases in proportion to needed size of the buffer pool, but that usually isn’t the case. Our server that has 1TB of RAM likely doesn’t need 205G of that to handle things like connections and queries (likely MySQL couldn’t handle that many active connections and queries anyway).
So, if you really just spent all that money on a beefy server do you really want to pay a 20% tax on that resource because of this rule of thumb?
The origins of the rule
At one of my first MySQL conferences, probably around 2006-2007 when I worked at Yahoo, I attended an InnoDB tuning talk hosted by Heikki Tuuri (the original author of InnoDB) and Peter Zaitsev. I distinctly remember asking about the 80% rule because at the time Yahoo had some beefy 64G servers and the rule wasn’t sitting right with me.
Heikki’s answer stuck with me. He said something to the effect of (not a direct quote): “Well, the server I was testing on had 1GB of RAM and 80% seemed about right”. He then, if memory serves, clarified it and said it would not apply similarly to larger servers.
How should you tune?
80% is maybe a great start and rule of thumb. You do want to be sure the server has plenty of free RAM for the OS and the usually unknown workload. However, as we can see above, the larger the server, the more likely the rule will wind up wasting RAM. I think for most people it starts and ends at the rule of thumb, mostly because changing the InnoDB buffer pool requires a restart in current releases.
So what’s a better rule of thumb? My rule is that you tune the innodb_buffer_pool_size as large as possible without using swap when the system is running the production workload. This sounds good in principle, but again, it requires a bunch of restarts and may be easier said than done.
Fortunately MySQL 5.7 and it’s online buffer pool resize featureshould make this an easier principle to follow. Seeing lots of free RAM (and/or filesystem cache usage)? Turn the buffer pool up dynamically. Seeing some swap activity? Just turn it down with no restart required. In practice, I suspect there will be some performance related hiccups of using this feature, but it is at least a big step in the right direction.