Why You Should Configure Swap Space: A Hard-Learned Lesson From My Crashed Server

Why You Should Configure Swap Space: A Hard-Learned Lesson From My Crashed Server

What happened?

Swap space is one of those things you don’t think about—until your server crashes and you realize how crucial it is.

I recently faced a situation where one of my self-hosted services consumed all the available memory on my server, causing everything else to go down. What followed was hours of helpless waiting and a hard lesson in preventive system design. Here’s how it went down.

When One App Brings Down the Whole Server

My home server hosts several self-hosted applications—blog server, monitoring stack, photo library, etc.—mostly managed using Docker.

Recently, I installed an app to play around with (which I hadn't set proper memory limits for), and it eats up all the RAM like crazy.

Without a swap space configured, there was no safety net. RAM usage hit 100%, and soon the system became unresponsive. But it didn’t just stop at the offending app—other critical services like Cockpit and SSH went down too, since the kernel couldn't allocate memory for them.


No Remote Access, No Way to Intervene

To make things worse, I was away from home when it happened.

There was no SSH, no web dashboard, and no other backdoor into the system.

It feels like I cut my own rope while climbing without other safety features—I fell head down on to the ground.

With no swap configured and no remote access, I had to wait for the applications to either crash on their own or force a physical reboot later.


The fix: Adding Swap Using an Old HDD

After the apps crashed and able to access my server again, I was determined not to let this happen again. I had two old HDDs set up in a Btrfs RAID0 configuration. Since they are just a test environment, I broke the RAID, removed one of the drives from the Btrfs volums, and turned it into swap space.

Steps I took:

  1. Ran a Btrfs balance to convert the RAID0 volume to a single-device setup.
    1. sudo btrfs filesystem show to check file system
    2. sudo btrfs balance start -dconvert=single -mconvert=single /mnt/nas to convert the data and metadata into single-device layout on the currently mounted /mnt/nas btrfs filesystem.
  2. Remove /dev/sdf(in my case, it's sdf) from the volume using sudo btrfs device remove /dev/sdf /mnt/nas
    1. Confirm the device is removed using sudo btrfs filesystem show
  3. Wiped it clean and formatted it as swap:
    1. sudo wipefs -a /dev/sdf
    2. sudo mkswap /dev/sdf
    3. sudo swapon /dev/sdf
  4. Add it to /etc/fstab to make it persistent:
    1. Get its UUID using sudo blkid /dev/sdf
    2. In /etc/fstab, add a line UUID=xxxxxxxx none swap sw 0 0 to the bottom of the file and save it.

Conclusion

Even if you think you “won’t need it,” swap space is your last line of defense. It could mean the difference between a slow server and a totally unreachable one. Learn from my mistake—set up swap now, before it's too late.