Enabling swap space for Odoo on AWS server

Introduction #

Swap space is used to store inactive data from RAM on the disk of your server, which allows your OS and Odoo to run even when RAM is full, preventing Odoo slowdowns or crashes.

While most cloud providers enable swap space by default, AWS does not enable swap on their servers (EC2 instances). This is because their EC2 instances are designed for applications that require quick data access and high performance, like Odoo. Relying on swap space can significantly degrade performance as retrieving data from your disk space is much slower than retrieving it from RAM. It can potentially also increase costs as EC2 instances typically use Elastic Block Storage (EBS) volumes for storage, which incur costs on I/O operations.

For these reasons, AWS recommends it’s better to scale up to a larger instance type with more RAM to maintain performance levels instead of resorting to swap space.

However, enabling swap is still beneficial for the occassional memory overflows of Odoo that don’t justify moving to a larger, more expensive instance. In such cases, swap can provide a buffer to handle memory spikes without affecting your Odoo instance.

How much swap space should I use for Odoo? #

You can use the following rule of thumb to calculate the recommended swap space for an Odoo server:

  • If RAM on your server is below 2GB: Set the swap space on your disk to twice the size of RAM in case the RAM amount is below 2 GB.
  • If RAM amounts to more than 2 GB: Set swap space to the size of RAM + 2 GB.

For example, when your server has 1GB of RAM, set the swap space on your disk to 2GB. If your server has 4GB of RAM, set your swap space to 6GB.

How do I enable swap for Odoo on my AWS EC2 instance? #

To enable swap on an EC2 instance, you can create a swap file on the instance’s EBS volume or attach a dedicated EBS volume for swap. Here’s how you can create a swap file:

1. First log in to your server’s console using SSH.

2. Create a swap file using the dd command:

Bash
sudo dd if=/dev/zero of=/swapfile bs=1G count=4

The count parameter specifies the size of the swap file. In this example, it’s set to 4GB.

3. Set the correct permissions for the swap file:

Bash
sudo chmod 600 /swapfile

4. Make the file usable as swap:

Bash
sudo mkswap /swapfile

5. Activate the swap file:

Bash
sudo swapon /swapfile

6. Check if the procedure was successful:

Bash
sudo swapon -s

7. To make the change permanent, add ‘/swapfile swap swap defaults 0 0’ to the end of the /etc/fstab file:

Bash
sudo vi /etc/fstab
/swapfile swap swap defaults 0 0

You now have a swap file enabled on your server. Any inactive data from your ram will now be stored on your swap space.