Skip to content

GCP: SWAP

Intro

GCP does not provide means to setup swap at provisioning time. Requests do so are met with.. well not a lot of enthusiasm, probably for very good reasons.

Discussions on whether to use SWAP on a modern system or not never comes with one size fits all answers.

So read up, understand your system and do what's right for you. Also check this interesting (but, ancient) discussion on swap vs. no-swap regarding GCE.

For the lazy

(To be precise, for the lazy running modern day servers)

In the end, as a bit of a handwavy prescription for all, having swap on is like having an insurance plan. When things go wrong, it'll be there helping by slowing things down instead of making kernel killing something off.

It's unlikely to add much value when you have proper capacity planning, dedicated resources for important services (say, database gets it's own VM), proper configuration tuning in services, superb monitoring/alerting and a plan on what to do when a spike hits.

All that is a lot of work. So it's ok to be lazy and throw in a swapfile and let things slow down to a crawl when things go awry. Am I right?

Now comes the question whether to use separate disk, partition or swapfile for swap. GCP PDs provides per GB IOPS values. But is it really worth having a massive disk just for some SWAP? Continuing the tradition of handwavy generic prescrption, I'd say just have a swapfile in the bootdisk.

Update [2019-08-10]

Someone with quite a bit of experience makes the case for having a bit of swap anyway. So readup and use it in your decision making process.

Commands

Check which processes use swap

grep VmSize /proc/*/status | sort -n -r --key=2.1 | head -5
  1. Adding swapfile to centos 7 system.
  2. Adding swapfile on GCE
  3. Swap file vs Swap partition
  4. GCP Persistent Disk (PD) performance
  5. Btrfs is not a good candidate for putting a swapfile on