Amazon AWS EC2 Linux Swapfile
The Amazon EC2 Linux instances comes without swap. Sooner or later this will be a problem with service hangups or crashes as a result because you run out of memory. I found a lot of instructions on the web about how to add a swap file but no one takes the storage into concern and you may end up paying a lot for a very little performance gain. This article will guide you through swap files on Amazon EC2 linux hosts.
What is a swap file?
It is Linux/Unix equivalent of the Windows page file. When you run out of memory the system moves data from the memory into the swap/page file and when a process tries to access it the system retrieves it from the file again. As we all know memory is faster then disc access and therefor it will be a performance loss. This mechanism should only be a fail safe so you machine doesn’t run out of memory and starts killing processes. So if you are constantly using more memory you should increase your memory and not rely on a swap file.
There are a few things to consider when setting up a swap files. As mentioned above they are not a substitute for real memory but a fail safe when peak usage comes along. For example running an Apache server, MySQL and APC OP cache on the same t2.micro instance is no problem. Using up to 80-09% of the available memory at any given time. But then your database backups kick in and the MySQL eats up the memory and the system runs out of memory and start killing processes. For the time the system uses the swap it will run slower but not slow enough to be an issue for a small site. If you need more power then you should upgrade to a bigger instance anyhow.
Where to put the swap?
I have read a lot of forums about using separate EBS volume for the swap and other recommends to put the swap on the instance local SSD storage for increased performance. That is by far the best place to put it but it’s not available to all instance sizes. Here is a quick rundown of the types of storage available for swap use.
EBS (Elastic Block Storage) General Purpose SSD
This is the standard general purpose storage available to all instance types and sizes. This storage averages 3 * <size in GB> IOPS and can peak 3000 IOPS for 30 minutes. If your running a small instances this is probably enough for your needs and is also the most cost effective sense the IOPS are included.
EBS (Elastic Block Storage) Provisioned IOPS SSD
This storage is exactly the same as General Purpose with the ability to provisioning more IOPS. With a single volume you will however peak at 4000 IOPS so the difference isn’t that big. At the same time even this storage is so much slower then memory so the 1000 IOPS difference doesn’t justify the cost for the smaller instances and for the larger instances it is not the ultimate solution. The problem here is that the provisioned IOPS cost money and if your instance start sending large amount of data to the swap file the charges can go through the roof quickly.
EBS (Elastic Block Storage) Magnetic
The cheapest storage available as instance storage. Providing 20 -400 IOPS makes it not suitable for this application.
EC2 Instance Storage SSD
For the larger instances there is direct attached SSD storage available that is great for swap file use. The storage is attached directly to the machines running your instance witch will give very low latency. At the same time it’s not really suitable for any other storage sense this storage is purged every time the machine is rebooted. This makes it suitable for temporary storage only, like a swap file.
How large should my swap file be?
I would recommend four times the size of your actual memory to be on the safe side. You will see if the page file needs to be larger but at least start out with two times the memory size. You can easily check the memory with the sudo free -m command.
How do I create my swap file?
It isn’t hard to create a swap file, start by running sudo su command.
[ps]dd if=/dev/zero of=/swapfile bs=1M count=4096[/ps]
Creates an empty file of what ever size you want. dd command copies information from one file to another. Here we copy from /dev/zero that will return as many null characters as are read from it. In the of= we tell the dd command where to put it. In this case we create a swapfile in the root. This can be changed to any other path were other storage are mounted.
This creates the swap file. This command can be used against both files and partitions. If we do not specify anything it will occupy all available space, that is just fine because we already set the size of the swap file.
This will put the swap file to use. This isn’t a persistent command so we will have to make sure that the page file is used again after reboot.
Hit i to insert and on the last line write:
[ps]/swapfile swap swap defaults 0 0[/ps]
Then just hit ESC to input command and write :wq to write and quit the vi editor.
If you now run free -m you will see your swap file and it’s usage on the last row of the output. Now your system will dump excess information from the memory to the swap instead of starting killing processes when you run out of memory.
How do I change the size of swap file?
This should be done with caution sense it can drive the machine to run out of memory and crash processes. But just check all your services status after your done and you should be OK. Start by running sudo su.
This command takes the path to the swap file or partition as parameter. This path is consistent with the example of creating a swap file above but can be changed if your swap is located elsewhere.
[ps]dd if=/dev/zero of=/swapfile bs=1M count=4096[/ps]
Set the count= parameter to the new size that you want for your swap file. Then run mkswap /swapfile to make it into a swap file again. To start it again issue the swapon /swapfile command and you will be up and running. We already put the swap into /ect/fstab so it will start with the new size after a reboot.
Very helpful article, Kristopher. What would be a reason for not going for more swap space (eg 10GB) if you have sufficient EBS SSD storage (eg 50 GB) for a 1Gb (RAM) EC2 instance?
SWAP is always slower then memory. If you require to page so much memory to disk all the time you need more ram. I don’t even think you can swap fifty times the size of your memory!
Kristofer, would you say the use case for using swap is to handles sudden spikes in memory usage so that processes does not get killed when you run out of memory? Is your recommendation of SWAP that is 2-4X Ram based on such a use case?
That would be my recommendation! 🙂 use sudo free -m command to check or even better use cloudwatch to monitor memory over time and know when it’s time to upgrade!
Thanks for your advice!