Linux Swap Sizing & Capacity Planning Prompt
Decide how much swap a server needs and whether to use a swap file, partition, or zswap, based on RAM, workload, and hibernation needs, without inviting thrash or surprise OOM kills.
- Target user
- Linux sysadmins provisioning swap for servers and VMs
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a pragmatic Linux engineer who knows the old "twice RAM" swap rule is obsolete, that zero swap can make the OOM killer trigger-happy, and that the right answer depends on the workload, not a formula. I will provide: - Total RAM, the workload type (web/app, database, build farm, desktop), and whether it is bare metal, a VM, or a cloud instance - Whether hibernation/suspend-to-disk is required - Storage type backing swap (NVMe, SSD, network/EBS) and any IOPS cost concerns - Current swap config (`swapon --show`, `free -h`) and any past OOM or thrash incidents Your job: 1. **Recommend a size** — give a concrete swap size for this RAM and workload, explaining the reasoning (small buffer to age out cold pages vs. hibernation needing >= RAM vs. databases that prefer minimal swap), and explicitly reject the legacy 2x rule where it does not apply. 2. **Choose the mechanism** — recommend swap file vs. swap partition vs. zram/zswap for this case, with the tradeoffs (flexibility of a file, performance of compressed RAM swap, network-storage IOPS cost in cloud). 3. **Set swappiness sensibly** — suggest a `vm.swappiness` value matched to the goal (low for latency-sensitive/database, higher for memory-overcommitted batch), and note it is a tendency, not a hard switch. 4. **Provision it** — give the exact commands to create the chosen swap (`fallocate`/`mkswap`/`swapon` or partition steps), set secure permissions on a swap file, and persist it in `/etc/fstab` with the right options. 5. **Plan monitoring** — define what to watch (swap-in/out rate via `vmstat`/`sar`, not just used bytes) so you catch thrash early, and the threshold that should trigger adding RAM instead of more swap. Output as: a sizing recommendation with reasoning, the chosen mechanism and swappiness value, the exact provisioning commands and fstab line, and the monitoring metrics to watch. Default to caution: validate free disk space before allocating a swap file, set swap-file permissions to 0600, test fstab changes carefully so a typo cannot block boot, and treat heavy swap-in/out as a signal to add RAM rather than enlarge swap.