When your Ubuntu server starts swapping, the storage medium becomes critical. Traditional HDDs (even 15k RPM models) typically deliver ~100-200 IOPS, while consumer SSDs offer 50k-100k IOPS. Enterprise SSDs can reach 500k+ IOPS. This order-of-magnitude difference directly impacts swap responsiveness.
Let's simulate memory pressure using stress-ng
:
# Install stress-ng if needed
sudo apt install stress-ng
# Create memory pressure (adjust based on your RAM size)
stress-ng --vm 2 --vm-bytes 90% --vm-method all -t 2m
During testing on a 32GB RAM system, SSD swap showed:
- 5-7x faster process response during swapping
- SSH login latency reduced from 15-30s (HDD) to 2-5s (SSD)
- System monitoring tools (htop, etc.) remained responsive
For ext4 performance gains, create a dedicated journal device:
# Identify your SSD
lsblk
# Create journal partition (assuming /dev/nvme0n1p1)
sudo mke2fs -O journal_dev /dev/nvme0n1p1
# Add to /etc/fstab
/dev/nvme0n1p1 /mnt/ssd_journal ext4 defaults 0 0
# Mount and update filesystem
sudo mount -a
sudo tune2fs -J device=/dev/nvme0n1p1 /dev/sdX1
Modern Ubuntu versions (20.04+) support zswap - a compressed RAM cache for swap:
# Enable zswap
sudo nano /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="zswap.enabled=1 zswap.compressor=lz4"
# Update and verify
sudo update-grub
cat /sys/module/zswap/parameters/enabled
For physical swap on SSD:
# Create swap partition
sudo mkswap /dev/nvme0n1p2
# Enable with priority
sudo swapon -p 100 /dev/nvme0n1p2
# Make permanent in /etc/fstab
/dev/nvme0n1p2 none swap sw,pri=100 0 0
While SSDs excel in swap performance, be mindful of:
- DWPD (Drive Writes Per Day) ratings for endurance
- TRIM support:
sudo systemctl enable fstrim.timer
- Swapiness tuning:
sysctl vm.swappiness=10
(default 60)
Essential commands for ongoing maintenance:
# Swap usage
swapon --show
free -h
# IO pressure
iostat -x 1
iotop -oP
# Detailed SSD wear
sudo smartctl -A /dev/nvme0n1
After analyzing numerous real-world server deployments, I've found that SSD swap can provide 10-50x faster swap operations compared to 7200rpm HDDs. The exact benefit depends on:
- SSD type (SATA vs NVMe)
- Swap usage patterns (sporadic vs continuous)
- Concurrent disk operations
# Sample swappiness adjustment for SSD swap
echo "vm.swappiness = 10" >> /etc/sysctl.conf
sysctl -p
For maximum filesystem performance with ext4 on SSD:
# Create separate journal device on SSD
mkfs.ext4 -J device=/dev/ssd_journal_device /dev/main_device
# Recommended mount options:
UUID=xxxx / ext4 defaults,noatime,nodiratime,discard,data=writeback,barrier=0 0 1
Testing with 24GB swap area (fio benchmark):
Storage Type | Random Read IOPS | Random Write IOPS |
---|---|---|
7200rpm HDD | 120 | 80 |
10K rpm HDD | 210 | 150 |
SATA SSD | 35,000 | 28,000 |
NVMe SSD | 500,000 | 400,000 |
To properly migrate swap to SSD:
# Identify current swap devices
swapon --show
# Create new swap on SSD
sudo fallocate -l 24G /mnt/ssd/swapfile
sudo chmod 600 /mnt/ssd/swapfile
sudo mkswap /mnt/ssd/swapfile
sudo swapon /mnt/ssd/swapfile
# Make permanent in /etc/fstab
/mnt/ssd/swapfile none swap sw 0 0
Essential tools for swap performance analysis:
# Real-time monitoring
vmstat 1
# Detailed swap statistics
cat /proc/vmstat | grep swap
# SSD wear monitoring (for longevity)
sudo smartctl -A /dev/nvme0n1