Jump to content

Digital Ocean Droplet Crashing - Fix


Recommended Posts

Hey. Found this awesome post a while ago.

Think it might help some of you who are using DO.

One problem I had with Digital Ocean is that every once in a while droplets would crash for (apparently) no reason. I thought it was the VM not having enough resources, and allocating more actually did stop the crashes, but they would still occur, albeit less frequently.

Once I figured it out what the reason was, I felt pretty stupid.


Ubuntu (and perhaps other distributions?) are setup with no swap file by default.

Of course, when the system ran out of memory MySQL and other services would crash, and eventually the whole thing would stop working.

The fix is pretty simple, though: just add a swap file (duh).

How much swap to use with x amount of RAM?

A few years ago I used to set servers with swap = RAM * 2, but it’s been a while since I found out that that’s total crap. http://www.cyberciti.biz/tips/linux-swap-space.html (and from experience it makes sense) say:

Swap space = Equal RAM size (if RAM < 2GB)

Swap space = 2GB size (if RAM > 2GB)

My friend who is a true Oracle GURU recommends something as follows for heavy duty Oracle server with fast storage such as RAID 10:

Swap space == Equal RAM size (if RAM < 8GB)

For Digital Ocean, I have machines with just 512MB of RAM, so the swap file will be 512MB as well.

How to add swap

Login as root or (sudo -s).

First thing, make sure that you have enough disk space:

# df -h

If yes, you can use the dd command to create a storage file. dd uses /dev/zero to produce null values and create a file at the path specified. Block size is 1024, so for 512MB do:

# dd if=/dev/zero of=/swapfile bs=1024 count=512k

Then, set up a Linux swap area:

# mkswap /swapfile

Make sure file permissions are correct (only root should be able to read/write to the file):

# chown -v root:root /swapfile

# chmod -v 0600 /swapfile

Activate swap:

# swapon /swapfile

You can check that the above worked with:

# swapon -s

Now, make sure that swap is activated even after we reboot the machine:

# nano /etc/fstab

Add at the end of the file (ctrl+x, “y” and then Enter to save):

/swapfile swap swap defaults 0 0

Swappiness in the file should be set to 10, to avoid poor performance and prevent out-of-memory crashes:

# echo 10 | sudo tee /proc/sys/vm/swappiness

# echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

To check if swap is enabled, you can just use:

# free -m

Reboot (if you can)—you don’t have to, but just to make sure, and run the above again.

Your droplets should now work better and not crash. :-)

Source: http://www.nbrogi.com/2014/08/digital-ocean-droplet-crashing/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...