2

I'm running a site on DigitalOcean with CentOS 6.5.

As has been noted elsewhere, DigitalOcean servers are by default configured without any sort of swap, and I'm wondering if I should add one.

I know just enough about this stuff to think that the answer is "well, of course", but I don't really have a foundation for going beyond that.

  • Will my system just die the first time it gets seriously loaded up with users and memory demands?
  • Will swap save me from that, but only by imposing an unpleasant performance hit?

Any advice in this area, specific to DigitalOcean or not, would be greatly appreciated.

Vasili Syrakis
  • 4,435
  • 3
  • 21
  • 29
Jim Miller
  • 713
  • 2
  • 11
  • 23
  • 2
    Related: [Why don't EC2 ubuntu images have swap?](http://serverfault.com/q/218750/126632); [How much SWAP space on a high memory system?](http://serverfault.com/q/5841/126632) – Michael Hampton Feb 28 '14 at 01:45
  • 4
    DigitalOcean appears to have some good material here: https://www.digitalocean.com/community/articles/how-to-configure-virtual-memory-swap-file-on-a-vps – Mike B Feb 28 '14 at 01:51
  • @MikeB Regarding the linked article, note that DO now recommends not enabling swap – Chris Apr 07 '16 at 02:18

3 Answers3

12

Is there any reason to NOT have a swap file on CentOS

Yes:

  • You have some type of horizontal scaling in place which increases your number of servers based on their memory usage, thus eliminating the possibility of swap being used.

  • You have a specific performance requirement that means you cannot allow your program to use swap for processing as it is significantly slower than memory, so rather than use swap, you choose to monitor memory usage closely and increase memory as needed.

Will my system just die the first time it gets seriously loaded up with users and memory demands?

If you have no idea how much load your server is going to experience, and you don't have enough memory, and you have no swap space, your application will stop.

Will swap save me from that, but only by imposing an unpleasant performance hit?

Correct.

What exactly happens when the application stops? Which application stops?
If I had 3 processes and 1 was leaking memory, would all 3 die or the 1 that was leaking die?
Would 1 force the other 2 to die?

Think of it like this:

If you make a program and tell the program to fill up a variable with an infinite amount of integers, very soon that application will encounter a memory error and it will exit.

It's the same. Whichever application is trying to access more memory for it's operations is going to fail. So if you have 1 byte left and your application creates a 32 bit integer, you need 2 bytes, the operation fails because there's not enough memory, and depending on how well the application handles memory errors, it will either fix itself or exit.

The same goes for 3 applications at once. If by coincidence they all request more memory when the system has only 1 byte of memory left, they should technically all fail at once.

Somebody can correct me on this if they see any errors.

Vasili Syrakis
  • 4,435
  • 3
  • 21
  • 29
  • I get the sentiment, but the first bullet point is kinda misleading in how its phrased. – Sirex Feb 28 '14 at 03:10
  • What exactly happens when the application stops? Which application stops? If I had 3 processes and 1 was leaking memory, would all 3 die or the 1 that was leaking die? Would 1 force the other 2 to die? – CMCDragonkai May 29 '14 at 09:06
  • I expanded my answer for you – Vasili Syrakis May 29 '14 at 10:20
  • It's not that the application attempts to allocate memory and it fails, but rather the Linux kernel has a dedicated process to kill processes when memory is low: http://prefetch.net/blog/index.php/2009/09/30/how-the-linux-oom-killer-works/ – Chris Smith Aug 30 '17 at 21:32
1
Will swap save me from that, but only by imposing an unpleasant performance hit?

Check out this page regarding the swappiness setting: http://www.scottalanmiller.com/linux/2012/09/03/controlling-swappiness-in-linux/

You can change your swappiness so that your system isn't using swap space too often, and thus not taking as much of a performance hit.

David A
  • 31
  • 3
0

First install sysstat (if not already installed) and monitor the resource usage for few days. Run "sar -r" and monitor the memory usage frequently. Most of the time memory usage will be near 90%, watch the "kbcached" and "kbbuffers" column. If they are using most of the memory, then you are fine as it means the applications are cached. But if those values are less, it means caching is less due to more actual memory usage which might also indicate your applications needs more RAM. So you can increase the RAM or setup swap space.

Now to swap, I believe digital ocean disks are on SSD. If that is the case, setup a swap file and you should not mostly see any performance hit since SSDs are faster than HDDs.

HTH.

Prasad
  • 11
  • 2
  • Yes, DigitalOcean disks are SSD. FWIF, since you can't partition the file system on their "droplets", swap must be enabled through files. – Jim Miller Feb 28 '14 at 17:02