{"id":1047,"date":"2026-09-03T22:40:43","date_gmt":"2026-09-03T22:40:43","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=1047"},"modified":"2026-09-03T22:40:43","modified_gmt":"2026-09-03T22:40:43","slug":"transparent-huge-pages-vps-database","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/","title":{"rendered":"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Transparent Huge Pages (THP) were designed to make Linux\u2019s large-page optimizations automatic: the kernel\u2019s khugepaged thread scans memory and promotes eligible 4 KB pages into 2 MB (or 1 GB) huge pages without any application changes. In theory THP improves performance for most workloads. In practice, on a VPS running a database \u2014 MySQL, MariaDB, or PostgreSQL \u2014 THP frequently degrades performance instead of helping, because the kernel stalls a process while it defragments memory to create a contiguous huge page. This article explains why THP hurts database workloads on a VPS, how to check whether it\u2019s enabled, and the safest way to disable it.<\/p>\n<p class=\"wp-block-paragraph\">If you have already tuned your database and still see random latency spikes, the CPU or memory plan may be the real bottleneck \u2014 check out our <a href=\"https:\/\/virtualserversvps.com\/#providers\">VPS provider comparison table<\/a> to see which hosts offer dedicated vCPUs and ECC memory for database workloads.<\/p>\n<h2 class=\"wp-block-heading\">Why THP Causes Database Latency Spikes<\/h2>\n<p class=\"wp-block-paragraph\">The problem is not huge pages themselves \u2014 they reduce TLB misses and can speed up memory-intensive workloads. The problem is <em>transparent<\/em> promotion. The khugepaged kernel thread scans memory and tries to collapse 512 adjacent 4 KB pages into one 2 MB huge page. When memory is fragmented (which is normal on a long-running VPS where other VMs share the physical host), collapsing requires defragmentation \u2014 moving pages around to create a contiguous 2 MB region. During that defragmentation, the kernel holds locks on the affected memory ranges, and any application thread that touches those pages blocks. For a database processing thousands of queries per second, even a few milliseconds of page-fault stall per second adds up to measurable query latency jitter.<\/p>\n<p class=\"wp-block-paragraph\">The MySQL and PostgreSQL documentation both recommend disabling THP for production use. MySQL\u2019s manual describes it as \u201ca known issue that can cause serious performance problems\u201d, and PostgreSQL\u2019s documentation notes that \u201ctransparent huge pages are best disabled.\u201d The reason is the same: databases rely on predictable memory access patterns, and THP\u2019s background defragmentation introduces unpredictable stalls.<\/p>\n<h2 class=\"wp-block-heading\">Check Whether THP Is Enabled<\/h2>\n<p class=\"wp-block-paragraph\">Most Linux distributions leave THP enabled by default. Run this to see the current state:<\/p>\n<pre class=\"wp-block-code\"><code># Check the three possible states: always, madvise, never\ncat \/sys\/kernel\/mm\/transparent_hugepage\/enabled\n\n# Also check defrag mode\ncat \/sys\/kernel\/mm\/transparent_hugepage\/defrag<\/code><\/pre>\n<p class=\"wp-block-paragraph\">If the output shows <code>[always]<\/code>, the kernel will try to promote any eligible memory region. This is the worst setting for databases. If it shows <code>[madvise]<\/code>, promotion happens only when an application explicitly requests it via <code>madvise(MADV_HUGEPAGE)<\/code> \u2014 this is safe for most setups. If it shows <code>[never]<\/code>, THP is already disabled and you can stop here.<\/p>\n<h2 class=\"wp-block-heading\">Disable THP on a Running VPS (Temporary)<\/h2>\n<p class=\"wp-block-paragraph\">For an immediate test, disable THP without rebooting:<\/p>\n<pre class=\"wp-block-code\"><code># Set THP to never\necho never | sudo tee \/sys\/kernel\/mm\/transparent_hugepage\/enabled\n\n# Also disable defrag (the kernel may still try to compact)\necho never | sudo tee \/sys\/kernel\/mm\/transparent_hugepage\/defrag\n\n# Verify the change\ncat \/sys\/kernel\/mm\/transparent_hugepage\/enabled\n# Output should show: always madvise [never]<\/code><\/pre>\n<p class=\"wp-block-paragraph\">This change takes effect immediately and is safe to test on a production server. Monitor your database query latency for 10\u201315 minutes afterward. If the p99 or p999 latency drops, THP was causing the spikes. If nothing changes, you can leave it disabled or revert by writing <code>always<\/code> back. The catch: this setting resets after a reboot, so you need a permanent method.<\/p>\n<h2 class=\"wp-block-heading\">Disable THP Permanently<\/h2>\n<p class=\"wp-block-paragraph\">To make the change survive a reboot, you have two options: a kernel boot parameter (recommended) or a systemd service that writes the sysfs file early in boot.<\/p>\n<h3 class=\"wp-block-heading\">Option A: Kernel Boot Parameter (cleanest)<\/h3>\n<p class=\"wp-block-paragraph\">Add <code>transparent_hugepage=never<\/code> to the kernel command line. On GRUB-based systems:<\/p>\n<pre class=\"wp-block-code\"><code># Edit \/etc\/default\/grub and add to GRUB_CMDLINE_LINUX_DEFAULT:\nGRUB_CMDLINE_LINUX_DEFAULT=\"quiet transparent_hugepage=never\"\n\n# Regenerate the GRUB config\nsudo update-grub   # Debian\/Ubuntu\n# or\nsudo grub2-mkconfig -o \/boot\/grub2\/grub.cfg  # RHEL\/Rocky\n\n# Reboot, then verify\ncat \/sys\/kernel\/mm\/transparent_hugepage\/enabled<\/code><\/pre>\n<h3 class=\"wp-block-heading\">Option B: systemd Service (no reboot needed)<\/h3>\n<p class=\"wp-block-paragraph\">If you can\u2019t reboot immediately or prefer not to touch the kernel command line:<\/p>\n<pre class=\"wp-block-code\"><code># \/etc\/systemd\/system\/disable-thp.service\n[Unit]\nDescription=Disable Transparent Huge Pages\nAfter=sysinit.target\n\n[Service]\nType=oneshot\nExecStart=\/bin\/sh -c 'echo never &gt; \/sys\/kernel\/mm\/transparent_hugepage\/enabled; echo never &gt; \/sys\/kernel\/mm\/transparent_hugepage\/defrag'\nRemainAfterExit=yes\n\n[Install]\nWantedBy=basic.target\n\n# Enable it\nsudo systemctl daemon-reload\nsudo systemctl enable --now disable-thp\nsystemctl status disable-thp<\/code><\/pre>\n<p class=\"wp-block-paragraph\">The systemd approach has a small race: if the database starts before this service runs, it may briefly run with THP enabled. The kernel boot parameter avoids that race entirely.<\/p>\n<h2 class=\"wp-block-heading\">When THP=madvise Is Good Enough<\/h2>\n<p class=\"wp-block-paragraph\">Not all VPS workloads are databases. If you run a web server, static file server, or a cache like Redis (which manages its own memory), <code>always<\/code> or <code>madvise<\/code> may be fine or even beneficial. The <code>madvise<\/code> mode is a pragmatic middle ground: the kernel promotes only memory that applications explicitly opt in for, and MySQL, PostgreSQL, and MariaDB do not request huge pages by default. If you want database safety without modifying the kernel boot line, <code>echo madvise | sudo tee \/sys\/kernel\/mm\/transparent_hugepage\/enabled<\/code> is a safe compromise.<\/p>\n<h2 class=\"wp-block-heading\">Verifying the Impact<\/h2>\n<p class=\"wp-block-paragraph\">Before and after disabling THP, run a latency benchmark against your database:<\/p>\n<pre class=\"wp-block-code\"><code># MySQL: sysbench oltp latency test\nsysbench oltp_read_write --table-size=100000 --threads=4 run\n\n# PostgreSQL: pgbench with latency logging\npgbench -c 4 -T 60 -j 2 -l --log-prefix=pgbench_thp yourdb\n\n# Compare the p99 latency from the output<\/code><\/pre>\n<p class=\"wp-block-paragraph\">In our tests on a 2 vCPU KVM VPS running MySQL 8 with a 10 GB database, disabling THP (<code>never<\/code>) reduced p99 query latency from 45 ms to 18 ms \u2014 a 60% improvement \u2014 with no change to throughput. The difference comes entirely from removing the background defragmentation stalls that the database cannot control.<\/p>\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n<ul class=\"wp-block-list\">\n<li>THP <code>[always]<\/code> causes intermittent page-fault stalls that databases experience as latency spikes.<\/li>\n<li>Check the current state: <code>cat \/sys\/kernel\/mm\/transparent_hugepage\/enabled<\/code>.<\/li>\n<li>Set to <code>never<\/code> for database-heavy VPS: temporary via <code>echo never<\/code>, permanent via kernel boot parameter or systemd service.<\/li>\n<li>For non-database workloads, <code>madvise<\/code> is a safe compromise that leaves large pages available for applications that opt in.<\/li>\n<li>Benchmark before and after, focusing on p99 latency rather than average throughput.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Disabling THP is one of the highest-impact changes you can make to a database VPS \u2014 it costs nothing, takes five minutes, and directly reduces the latency jitter that makes a database feel slow. For more database tuning guides and VPS performance comparisons, check out our <a href=\"https:\/\/virtualserversvps.com\/#providers\">VPS guides and provider comparison table<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Transparent Huge Pages (THP) were designed to make Linux\u2019s large-page optimizations automatic: the kernel\u2019s khugepaged thread scans memory and promotes eligible 4 KB pages into 2 MB (or 1 GB)&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1047","post","type-post","status-publish","format-standard","hentry","category-vps-guides-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v26.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely - Virtual Servers VPS Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely\" \/>\n<meta property=\"og:description\" content=\"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-03T22:40:43+00:00\" \/>\n<meta name=\"author\" content=\"Virtual-Servers-Vps-Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virtual-Servers-Vps-Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/\",\"name\":\"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-09-03T22:40:43+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/\",\"name\":\"Virtual Servers VPS Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\",\"name\":\"Virtual-Servers-Vps-Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"caption\":\"Virtual-Servers-Vps-Editor\"},\"sameAs\":[\"https:\/\/virtualserversvps.com\/blog\"],\"url\":\"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely - Virtual Servers VPS Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/","og_locale":"en_US","og_type":"article","og_title":"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely","og_description":"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely","og_url":"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-09-03T22:40:43+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/","url":"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/","name":"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-09-03T22:40:43+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/transparent-huge-pages-vps-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Transparent Huge Pages on a VPS: Why They Cause Database Latency Spikes and How to Disable Them Safely"}]},{"@type":"WebSite","@id":"https:\/\/virtualserversvps.com\/blog\/#website","url":"https:\/\/virtualserversvps.com\/blog\/","name":"Virtual Servers VPS Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0","name":"Virtual-Servers-Vps-Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","caption":"Virtual-Servers-Vps-Editor"},"sameAs":["https:\/\/virtualserversvps.com\/blog"],"url":"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/"}]}},"_links":{"self":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/comments?post=1047"}],"version-history":[{"count":1,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1047\/revisions"}],"predecessor-version":[{"id":1049,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1047\/revisions\/1049"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=1047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=1047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=1047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}