zRAM and next-gen Cloud VPS (Miniservers)

zRAM and next-gen Cloud VPS (Miniservers)

Memset’s next generation Cloud VPS have introduced several new features which are not possible on the previous Miniserver platform. One such possibility is the loading and configuring of custom kernel modules. This blog piece will examine the zRAM kernel module and how to use it as a swap partition on a Cloud VPS.

What is zRam and why is it useful as a swap partition?

Swapping occurs when the Operating System transfers some data from RAM to an alternative storage device, typically the hard disk. Swapping will occur for two reasons. The first is when programs no longer requires some data which was only required during start up. The second occasion is when there is insufficient RAM to support all the active programs. When the latter situation occurs the OS will resort to using the swap as active memory where it would normally use RAM. When this occurs there is a serious performance penalty because accessing the disk is far slower than accessing RAM.

The zRAM module enables the swap partition to be located in RAM instead of the hard disk. This appears counter-intuitive because swapping usually occurs the available RAM has been exhausted. Therefore, putting the data for which there is insufficient RAM back into RAM does not look like it is going to solve any problems. The answer is that the data stored in a zRAM partition is compressed thereby allowing more data to be stored in RAM than there is RAM available. A small amount of CPU time is traded for much more performant swap. These performance gains are why many systems, such as Android 4.4+, which are RAM-constrained employ zRAM.

Enabling a zRam swap partition

This guide will cover the three OS types available as stock images for next-gen Cloud VPS:

Debian 8 Jessie

Centos 7

Ubuntu 16.04 LTS

All of these Linux distributions employ systemd as their init system so this guide will configure systemd to enable and manage the zRAM swap partitions. The steps below use the zile text editor but any command line text editor that you are comfortable will work just as well.

Please note, the zram swap partition is being managed by systemd so an entry in the fstab is not necessary and so has been deliberately omitted.

The following steps are the same for all three distributions mentioned above: The zram module needs to be loaded on boot. This is done by creating the following file:

zile /etc/modules-load.d/zram.conf

and adding only the single word:

zram

Then another new file needs to be created with an instruction to set the number of zram devices which should be created:

zile /etc/modprobe.d/zram.conf

with the following contents:

options zram num_devices=1

Next, the size of the swap space is configured in this file:

zile /etc/udev/rules.d/99-zram.rules

with the following contents for a 512 megabyte partition:

KERNEL=="zram0", ATTR{disksize}="512M",TAG+="systemd"

The size of the partition can be changed to suit the needs of your system. The actual size of this partition i.e. how much RAM is used, will grow as more swap is needed from a starting size of 0MB.

Now that the zram kernel module will be loaded and configured at boot we need to create a systemd unit file that will prepare the zram space as swap (mkswap) as well as enable it (swapon). This must be repeated on every boot because the swap partition resides in RAM and is therefore destroyed every time the server is powered off.

Due to the different paths for the mkswap and swapon binaries in CentOS versus the Debian based distributions, there are two versions of the systemd unit file.

All of the distributions have the same location and name for the unit file: