If you are running a VPS with a modest amount of RAM (like 8GB or 12GB), you’ve likely encountered the “Swap Death Spiral.” Your RAM fills up, the system starts writing to the swap file on your disk, and suddenly the whole server crawls to a halt.

I recently benchmarked my server’s disk and found it was writing at 162 MB/s. While that’s fine for storage, it is a disaster for swap file. Compared to RAM speeds (which can exceed 20,000 MB/s), my disk is over 100 times slower.

The solution? Zswap.

What is Zswap?

Zswap is a kernel feature that acts as a compressed “waiting room” for your swap file. Instead of writing memory pages directly to your slow disk, the kernel compresses them and keeps them in a small pool within your RAM.

The result: You can often fit 3GB of data into 1GB of space, and you access it at CPU speeds instead of waiting for a virtual disk.

The “Safety First” Implementation

Before enabling Zswap, you need to ensure your kernel supports the best compression algorithms (like zstd) and the most efficient memory allocators (zsmalloc).

I wrote a script to automate these safety checks. It detects your OS (Ubuntu vs. AlmaLinux/RHEL), probes for hardware support, and applies the most aggressive optimizations available for your specific kernel.

The Setup Script: setup_zswap.sh

    1 #!/bin/bash
    2 # A snippet of the logic used to safely enable zswap
    3 if [ -d "/sys/module/zswap" ]; then
    4     # Probe for zstd (the gold standard for compression)
    5     sudo modprobe zstd 2>/dev/null
    6
    7     # Enable and configure
    8     echo 1 | sudo tee /sys/module/zswap/parameters/enabled
    9     echo zstd | sudo tee /sys/module/zswap/parameters/compressor
   10     echo zsmalloc | sudo tee /sys/module/zswap/parameters/zpool
   11     echo 25 | sudo tee /sys/module/zswap/parameters/max_pool_percent
   12     echo "Zswap optimized and active!"
   13 fi

The Results: Real-World Stats

After running the optimization on a 12GB RAM server, the numbers speak for themselves:

  • Compression Ratio: ~2.9:1 (1.38 GB of data compressed into just 473 MB).
  • Disk Protection: Over 93% of swap requests were handled instantly in RAM. Only 7% of the heaviest “cold” data ever touched the slow 162 MB/s disk.
  • Latency: The “lag” typically associated with a full server was virtually eliminated.

How to Make It Permanent

Kernel changes via echo disappear on reboot. To make them stick:

  • On Ubuntu/Debian: Add zswap.enabled=1 zswap.compressor=zstd zswap.zpool=zsmalloc zswap.max_pool_percent=25 to the GRUB_CMDLINE_LINUX_DEFAULT line in /etc/default/grub and run sudo update-grub
  • On AlmaLinux/CentOS: Use the safer grubby tool:
sudo grubby --update-kernel=ALL --args="zswap.enabled=1 zswap.compressor=zstd zswap.zpool=zsmalloc zswap.max_pool_percent=25"

Conclusion

Don’t let a slow disk bottle-neck your applications. If you have a multi-core CPU, use that processing power to compress your memory. It’s the closest thing to “downloading more RAM” that actually works.