Add swap to your Otto server
Give your Otto server a swap file, so that when its sessions and builds run it out of memory it slows down instead of killing processes.
Before you start
Without swap, a server that runs out of memory has the kernel's OOM killer end a process at once: a session's Claude, a build, or Otto's own daemon. With swap, the server pages out memory it has not used lately and carries on, slower. Otto stops starting new sessions while the server is short of memory (its 1.5 GB memory floor counts RAM only, never swap), so the slow spell passes as the busy sessions finish.
A server you moved with server-move.sh after this change got its swap during the import, and needs none of this. A deploy says this server has no swap when yours has none.
The script makes these choices, each of which you can change:
- Size: half the RAM, between 2 and 8 GB. 8 GB on a 16 GB server. Swap here is a buffer for spikes, such as an image build beside a dozen sessions, not room to run more sessions. More swap would let a runaway process page for a long time, the server too slow to answer, before the OOM killer acts.
- Swappiness 10, instead of the kernel's 60. The kernel drops file cache first and swaps only under real pressure, so idle sessions are not paged out while memory is left.
- The file
/swapfile, with a line in/etc/fstabso it is on after a reboot, and the swappiness in/etc/sysctl.d/99-otto-swap.conf.
It needs the swap's size plus 10 GB of free disk. Below, replace SERVER with the name your Mac uses to SSH to your Otto server.
Steps
-
See what it would do
ssh SERVER sudo bash /opt/otto/deploy/session-host/swap.sh --dry-runIt prints the size it chose for the server's RAM and each change it would make. If swap is on already, for example a partition your provider set up, it adds no file and only sets the swappiness.
-
Add the swap
sudo bash /opt/otto/deploy/session-host/swap.shFor another size, add
--size <GB>; for another swappiness,--swappiness <0–100>. Nothing needs to stop: the sessions keep running. -
Check it
swapon --show cat /proc/sys/vm/swappiness/swapfileis listed with its size, and the swappiness is 10. In Otto, Settings → Session hosts now shows a Swap meter for the server.
You're done. Running the script again changes nothing; it says what is already in place.
Resize the swap file
The script never resizes a swap file that is on: switching it off moves everything in it back to RAM, which fails when memory is short. When the Swap meter shows little use:
sudo swapoff /swapfile && sudo rm /swapfile
sudo bash /opt/otto/deploy/session-host/swap.sh --size 4
If it fails
only … of disk free: free disk, or pass a smaller--size.swapon /swapfile failed: the root filesystem does not take a plain swap file. On btrfs, make it withbtrfs filesystem mkswapfile --size 8g /swapfile, then run the script again: it switches on the file and does the rest. ZFS does not support swap files.- Inside a container, swap cannot be added at all: run the script on the server itself.
Remove the swap
sudo swapoff /swapfile && sudo rm /swapfile
sudo sed -i '\#^/swapfile #d' /etc/fstab
sudo rm /etc/sysctl.d/99-otto-swap.conf && sudo sysctl -w vm.swappiness=60