{"id":1253,"date":"2026-09-29T23:09:50","date_gmt":"2026-09-29T23:09:50","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=1253"},"modified":"2026-09-29T23:09:50","modified_gmt":"2026-09-29T23:09:50","slug":"zram-vs-swap-partition-latency-vps","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/","title":{"rendered":"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">On a VPS you cannot add physical RAM, so when memory pressure arrives you have two broad escapes: spill to disk-backed swap, or compress cold pages in RAM with zram. They behave nothing alike. Disk swap trades latency for capacity; zram trades CPU time for capacity without ever touching the disk. Choosing wrong produces either thrashing databases or burned CPU cycles. This article measures both and gives a decision rule.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How each mechanism actually works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A swap partition or file pages anonymous memory out to block storage. Every subsequent access is a page fault that must read from disk \u2014 on a shared NVMe VPS this is tens to hundreds of microseconds latency, and on a throttled volume it can be milliseconds. Sequential throughput may look fine while random fault latency destroys tail response times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">zram creates a compressed block device backed entirely by RAM. Pages written to it are compressed with LZ4 or zstd and stay in memory. A page fault triggers decompression, which costs CPU cycles but no disk I\/O. Typical compression ratios of 2:1 to 4:1 mean 1 GB of zram can hold 2-4 GB of logical data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up zram on a small instance<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Debian\/Ubuntu\napt install -y zram-tools\n\n# size to roughly 50-100% of RAM, LZ4 for speed\ncat &gt;\/etc\/default\/zramswap &lt;&lt;&#039;EOF&#039;\nALGO=lz4\nPERCENT=75\nPRIORITY=100\nEOF\n\nsystemctl restart zramswap\nswapon --show\nzramctl<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>PRIORITY<\/code> matters: give zram a higher priority than any disk swap so the kernel prefers the compressed device, and keep disk swap as a last-resort safety net rather than the first line of defence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Measuring the difference<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create controlled memory pressure and watch both the fault rate and the CPU cost. Using <code>stress-ng<\/code> with a memory worker bounded above available RAM forces the kernel to reclaim:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># watch fault behaviour and CPU sys time\nvmstat 1 30 &amp;\nstress-ng --vm 1 --vm-bytes 90% --vm-hang 0 --timeout 60s\n\n# per-process swap usage\nfor f in \/proc\/*\/status; do \\\n  awk '\/^Name|^VmSwap\/{printf \"%s \", $2}' $f; echo; \\\n  done | sort -k3 -n | tail -10<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Metric<\/th><th>Disk swap<\/th><th>zram (LZ4)<\/th><\/tr><\/thead><tbody><tr><td>Fault latency<\/td><td>100 us &#8211; 5 ms<\/td><td>5-50 us<\/td><\/tr><tr><td>CPU cost<\/td><td>minimal<\/td><td>notable (sys time up)<\/td><\/tr><tr><td>Effective capacity<\/td><td>full disk size<\/td><td>2-4x RAM allocation<\/td><\/tr><tr><td>Risk<\/td><td>I\/O thrash, TTFB spikes<\/td><td>CPU saturation, OOM at high ratio<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">When zram is the wrong answer<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>CPU-bound workloads.<\/strong> If your single vCPU already sits near 100%, adding compression work makes everything slower. Check with <code>mpstat -P ALL 1<\/code> first.<\/li><li><strong>Working sets that genuinely exceed 4x RAM.<\/strong> Compression cannot manufacture capacity; past a point you OOM just as hard, only later and with more CPU burned.<\/li><li><strong>Databases with large page caches.<\/strong> A MySQL or PostgreSQL instance whose hot set is disk-resident gains little from compressing anonymous memory and may delay the honest signal that it needs more RAM.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">When disk swap is the wrong answer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">On a burstable or network-attached volume, sustained swap I\/O competes with your application&#8217;s own reads. If <code>vmstat<\/code> shows swap-in (<code>si<\/code>) and swap-out (<code>so<\/code>) counters moving continuously, the instance is thrashing and no tuning will fix a genuinely undersized plan \u2014 see the <a href=\"https:\/\/virtualserversvps.com\/\">instance-sizing guidance<\/a> rather than fighting the symptom.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Monitoring zram effectiveness over time<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once zram is active, track the ratio and how much RAM you are actually recuperating. A high ratio is good for capacity but signals that pages compress well only because they are highly redundant; a low ratio with heavy use means you are burning CPU for little gain.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># DISK column shows logical size vs physical RAM consumed\nzramctl\n\n# watch allocation ratio and compression live\nwatch -n2 'zramctl --output NAME,DISKSIZE,DATA,COMPR,TOTAL'\n\n# how much is the kernel spending on compression?\ngrep -E 'zram|zswap' \/proc\/vmstat<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Observation<\/th><th>Meaning<\/th><th>Action<\/th><\/tr><\/thead><tbody><tr><td>COMPR ~3-4x, low CPU<\/td><td>working as intended<\/td><td>keep as is<\/td><\/tr><tr><td>COMPR ~1.1x, high CPU<\/td><td>poorly compressible data<\/td><td>reduce size or disable<\/td><\/tr><tr><td>DATA near DISKSIZE<\/td><td>device nearly full<\/td><td>add disk swap fallback<\/td><\/tr><tr><td>sys time pegged<\/td><td>compression bottleneck<\/td><td>switch algo to lz4, cut PERCENT<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">zstd vs lz4 for the compressed device<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The algorithm choice is a direct latency-versus-capacity trade. LZ4 decompresses at multiple gigabytes per second per core and is nearly invisible in <code>sys<\/code> time; zstd compresses noticeably better, so more logical data fits, at a real CPU cost.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Single vCPU, latency-sensitive<\/strong> (web front end, DNS): LZ4. Compression cost must stay under a few percent.<\/li><li><strong>Multi-core with headroom<\/strong> and a memory-hungry app: zstd, to squeeze more effective capacity from the same RAM.<\/li><li><strong>Rule of thumb:<\/strong> if <code>mpstat<\/code> reports idle below 20% at peak, choose LZ4 or disable zram entirely.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">A practical combined configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The best setup on a 2 GB VPS is usually both: zram at 50-75% with high priority, plus a modest 1-2 GB swap file as a pressure release valve, and a swappiness that encourages early use of the compressed device.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># prefer zram, keep disk swap as fallback\ncat &gt;\/etc\/sysctl.d\/99-mem.conf &lt;&lt;&#039;EOF&#039;\nvm.swappiness=100\nvm.vfs_cache_pressure=50\nvm.watermark_scale_factor=125\nEOF\nsysctl --system\n\n# verify priorities: zram should be higher than \/swapfile\nswapon --show=NAME,TYPE,SIZE,PRIO<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A high <code>swappiness<\/code> sounds counterintuitive, but when the swap device is fast compressed RAM you <em>want<\/em> cold anonymous pages moved out early to keep the page cache warm. When the only swap is disk, the conventional low value is correct. This is the single most misunderstood pairing in VPS tuning.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Decision rule<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Idle CPU headroom above 30% and memory pressure is intermittent &rarr; <strong>zram<\/strong>.<\/li><li>CPU already saturated or working set far exceeds RAM &rarr; <strong>more RAM<\/strong>, not zram.<\/li><li>Volume is network-attached or throttled &rarr; <strong>avoid disk swap<\/strong> except as a crash guard.<\/li><li>Both available &rarr; <strong>zram high priority + small disk swap fallback<\/strong>, swappiness 100.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Measure before and after with the counters above; the difference in tail latency during pressure is usually dramatic and settles the argument quickly. Related memory-accounting techniques are covered in the <a href=\"https:\/\/virtualserversvps.com\/blog\/\">tutorials section<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On a VPS you cannot add physical RAM, so when memory pressure arrives you have two broad escapes: spill to disk-backed swap, or compress cold pages in RAM with zram&#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":7,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1253","post","type-post","status-publish","format-standard","hentry","category-performance-optimization"],"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>zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts - Virtual Servers VPS Blog<\/title>\n<meta name=\"description\" content=\"On a VPS you cannot add physical RAM, so when memory pressure arrives you have two broad escapes: spill to disk-backed swap, or compress cold pages in RAM\" \/>\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\/zram-vs-swap-partition-latency-vps\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts\" \/>\n<meta property=\"og:description\" content=\"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-29T23:09:50+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\/zram-vs-swap-partition-latency-vps\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/\",\"name\":\"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-09-29T23:09:50+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"description\":\"On a VPS you cannot add physical RAM, so when memory pressure arrives you have two broad escapes: spill to disk-backed swap, or compress cold pages in RAM\",\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts\"}]},{\"@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":"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts - Virtual Servers VPS Blog","description":"On a VPS you cannot add physical RAM, so when memory pressure arrives you have two broad escapes: spill to disk-backed swap, or compress cold pages in RAM","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\/zram-vs-swap-partition-latency-vps\/","og_locale":"en_US","og_type":"article","og_title":"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts","og_description":"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts","og_url":"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-09-29T23:09:50+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\/zram-vs-swap-partition-latency-vps\/","url":"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/","name":"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-09-29T23:09:50+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"description":"On a VPS you cannot add physical RAM, so when memory pressure arrives you have two broad escapes: spill to disk-backed swap, or compress cold pages in RAM","breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/zram-vs-swap-partition-latency-vps\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"zram vs Swap Partition on a VPS: Latency, Memory Savings and When Each One Hurts"}]},{"@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\/1253","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=1253"}],"version-history":[{"count":1,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1253\/revisions"}],"predecessor-version":[{"id":1254,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1253\/revisions\/1254"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=1253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=1253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=1253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}