Transparent Huge Pages (THP) is a memory management feature in Linux operating systems that aims to enhance system performance. While THP can be beneficial for many applications, enabling it on a database server could have unintended consequences. In this post, we will explore THP, its impact on database servers, and how to disable it for optimal performance and stability.

What are Transparent Huge Pages?

In order to understand THP, we should first start with a brief description of Linux HugePages. The concept of HugePages in Linux has existed for many years, first introduced in 2007. By default, the majority of widely used Linux distributions employ a virtual memory page size of 4KB. However, the inclusion of the HugePages feature allows the Linux kernel to efficiently handle substantial memory pages alongside the standard 4KB size.

In order for an application to utilize HugePages, however, it must explicitly include an instruction to do so. As many applications aren’t written to take advantage of HugePages, the concept of Transparent HugePages (THP) was introduced. 

When enabled, THP can allocate HugePages for applications without their explicit awareness (transparently), with the expectation that this will improve application performance by reducing the overall memory management overhead of Translation Lookaside Buffer lookups. By utilizing larger page sizes (usually 2 MB by default), THP reduces the number of page table entries required to map a given amount of memory, resulting in potential performance gains. 

Transparent Huge Pages and database servers

While HugePages and Transparent HugePages can be beneficial for many applications, they can also inadvertently introduce memory fragmentation on database servers. The larger page sizes that accompany THP may result in inefficient memory allocation, leading to fragmentation and suboptimal memory usage. 

Database systems like MySQL rely on their own memory management systems, carefully designed to optimize performance and ensure efficient memory allocation. 

Transparent Huge Pages can conflict with these built-in memory management strategies, disrupting the balance of the system, degrading performance, and potentially impacting query response times. As such, it is generally recommended to disable THP for most database workloads. 

Disabling THP dynamically

Check that THP is actually enabled on your server with the following commands. The value that appears between square brackets is the current value. In this case, THP is enabled.

Check the amount of memory used by THP (or HugePages):

Disable THP dynamically at runtime by changing the file to contain the value ‘never.’ The simplest way to do this is to overwrite this file by running the following commands as root (or with sudo privileges):

Now, transparent huge pages will no longer be used for any new processes started from this point forward, at least until the next reboot.

Making the change persistent

Disabling THP with grub

To ensure that transparent huge pages are disabled after successive server reboots, you can add the following to the kernel boot command:

For example, edit the /etc/default/grub file in the following line

and then recreate the grub.cnf file with:

After the next reboot, you should see that transparent huge pages are no longer in use. There is no need to explicitly disable THP defragmentation since its code path will not be triggered when THP is disabled in this way.

Disabling THP with systemd in Ubuntu

For Ubuntu, you can also disable THP in a persistent manner by using systemd. Keep in mind that you should only use either the grub or systemd method, not both. 

Create a systemd unit file at:

Edit and save this file so that it contains the following configuration:

Reload your systemd unit files:

Manually start the new disable-thp service:

Set the new service to run at boot time:

Final thoughts

In conclusion, while Transparent Huge Pages can bring performance benefits for many applications, they can also have detrimental effects on database servers such as MySQL. Disabling THP may be crucial to ensure optimal memory management, prevent fragmentation, and maintain overall stability. 

Remember that each server and workload is unique, so it is recommended to thoroughly test the impact of enabling or disabling Transparent Huge Pages on your specific database environment to determine the most suitable configuration. 

By doing so, you can ensure that your database server harnesses the full potential of its memory management capabilities while avoiding potential pitfalls associated with Transparent Huge Pages.

Further reading

Settling the Myth of Transparent HugePages for Databases

Percona Distribution for MySQL is the most complete, stable, scalable, and secure open source MySQL solution available, delivering enterprise-grade database environments for your most critical business applications… and it’s free to use!

 

Try Percona Distribution for MySQL today!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments