refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / vps-addswap / README.md
1 ---
2 title: VPS Add Swap
3 homepage: https://webinstall.dev/vps-addswap
4 tagline: |
5   VPS Add Swap: because a little RAM can go a long way.
6 linux: true
7 ---
8
9 ## Cheat Sheet
10
11 > Creates permanent swap space that will be activated on each boot.
12
13 `vps-addswap` will
14
15 1. create a permanent swapfile `/var/swapfile`
16 2. format and activate the swapfile
17 3. add '/var/swapfile none swap sw 0 0' to `/etc/fstab`
18
19 ### What is `swap`?
20
21 Simply put, swap space is a substitute for RAM that lives on a storage drive.
22
23 ### Why use permanent `swap`?
24
25 In a word: Money.
26
27 Whereas once upon a time RAM was expensive in real life, now it is very cheap.
28
29 In "the cloud", however, RAM is not just expensive - it's very, _very_
30 expensive - as in _hundreds_-of-dollars-per-month expensive.
31
32 For most applications this isn't a problem because you don't need much RAM - or
33 when you do, you need it according to workload, which is easy (and cheap) to
34 distribute across many otherwise underpowered server instances.
35
36 However, when you need to use Python, R, etc to process large batches of data
37 sequentially - such as the multi-gigabyte datasets common in Machine Learning,
38 Artificial Intelligience, Data Mining, etc - or when you need a very rare burst
39 of RAM for a very short-lived task - such as an `npm run bild`, swap can be a
40 big cost saver.
41
42 The good news is that quite often you can also have your cake and eat it to:
43 most language runtimes (i.e. Node.js, Go, Java, etc) allow you to tune
44 parameters that limit the amount of RAM they are allowed to allocate
45 dynamically, which means that you can take advantage of large swap space for
46 sequential processing tasks without causing your apps to be slow.
47
48 ### How to allocate swap space?
49
50 Typically you should place swap files in `/var`, which is the volume that will
51 be optimized for fast writes (on servers that do so).
52
53 ```bash
54 sudo fallocate -l 2G /var/swapfile
55 sudo chmod 0600 /var/swapfile
56 sudo mkswap /var/swapfile
57 ```
58
59 This method is preferrably to `truncate` and `dd` for SSDs as it will NOT
60 actually write the file to its full size, and therefore will be instant.
61
62 On an HDD (rotational drive), `dd` may be a better choice, as you need to
63 allocate contiguous space all at once.
64
65 ```bash
66 sudo dd if=/dev/zero of=/var/swapfile bs=2G count=1
67 ```
68
69 ### How much swap to use?
70
71 If you need to run common tasks with `npm run build` - such as compiling
72 `sass` - 2G would be a good place to start.
73
74 If you need to process large datasets, somewhere between 1x-2x the size of the
75 largest dataset should do - noting that the dataset will likely take up more
76 space in RAM/swap than as a file on disk.
77
78 ### How to activate swap space?
79
80 ```bash
81 sudo swapon /var/swapfile
82 ```
83
84 If you get an errors about "holes" or allocations, you'll either need to defrag
85 or try allocating far more space than you need with `dd`, delete the dd file,
86 and create the swap file again.
87
88 ### How to activate swap on boot?
89
90 You need to open `/etc/fstab` and add a line like this:
91
92 ```txt
93 /var/swapfile none swap sw 0 0
94 ```