Scenario / Questions
Recently my friend told me that it is a good idea to turn off swap on linux webservers with enough memory. My server has 12 GB and currently uses 4GB (not counting cache and buffers) under peak load.
His argument was that in a normal situation server will never use all of its RAM so the only way it can encounter OutOfMemory situation is due to some bug/ddos/etc. So in case swap is turned off system will run out of memory that will eventually crash the program hogging memory (most likely the web server process) and probably some other processes. In case swap is turned on it will eat both RAM and swap and eventually will result in the same crash, but before that it will offload crucial processes like sshd to swap and start to do a lot of swap operations resulting in major slowdown. This way when under ddos system may go into a completely unusable condition due to huge lags and I probably will not be unable to log in and kill webserver process or deny all incoming traffic (all but ssh).
Is this right? Am I missing something (like the fact that swap partition is very useful in some way even if I have enough RAM)? Should I turn it off?
Find below all possible solutions or suggestions for the above questions..
I would say it depends on your use case and the rest of the answers have covered this pretty well. 4G of swap are after all a cheap way to buy some safety. And I feel that this cheapness is what is making people not want to turn it off.
But let me answer with a rhetorical question. If money is not an issue, and you have a choice between two systems – one with 12G of RAM and 4G of swap, and another with 16G of RAM and no swap – which one would you choose? Unfortunately most people would still answer that they’d choose 16G of RAM and still add 4G of swap, which is missing my point.
And on another note, I personally find a swappy system worse than a crashed system. A crashed system would trigger a standby backup server to take over much sooner. And in an active-active (or load balanced setup) a crashed system would be taken out of rotation much sooner. A win for the no-swap system again.
It is NOT recommended to turn off swap even if you have enough memory. If your server needs more memory and it did not get it, it will crash. However, this can be prevented (to some extent) when you have a swap area.
Yes, your server performance will degrade when using swap, but at least it will be operational and accessible. Then, you can plan for adding more memory when needed if your server starts using swap.
I found this page talking about swap. Have a look at 3rd section.
Instead of turning off swap, you can control the swapiness.
No, it is not a good idea. ‘some process went crazy’ means you should have proactively already called
at or before process creation time to set a limit on per process data segment memory — And maybe a limit to the number of threads
per process. ulimit is your friend. Please consider reading one of the memory tuning guides before you turn off swap. You can change kernel parms as well to some things to try to handle DOS attacks or bad programs.
Look at it this way:
The total memory on your system is RAM + swap. If you have 12GB of swap, you just effectively cut the system VM capacity in half, by disabling swap. Bad idea. This isn’t a debate, really, it is simply reading what other people have known from previous bad experiences for years. It is possible your friend needs to do some reading as well.
As others have said, you can effectively stop your server using swap except when absolutely necessary by tinkering with the “swappiness” parameter. This controls how aggressively the kernel will swap out memory pages.
You can see what it’s currently set to with:
and you can edit it “live” with (as root):
# echo "10" > /proc/sys/vm/swappiness
and to make it persist, add the following to /etc/sysctl.conf:
Another good thing that you can do is swap into RAM using zRAM. I think that this is a great idea! For performance, is like not having swap at all, but also prevents the crashes when the system is very loaded!
Look at this:
My experience: In this machine where I’m writing now, I disabled the swap, cause I have 4Gb RAM (in 2009 it was a lot!). I only experienced a couple of problems, one of them was opening like 127 pictures at the same time by mistake!
BUT.. this is a workstation, and I can afford to reboot if it hangs. In a server I think is better to have swap, and the swap-in-RAM sounds good to me.
As has already been made abundantly clear, this is not a good idea. If nothing else, swap gives you some breathing space for when things aren’t quite normal. e.g. On one system I look, which normally has only a few visitors per day, there was a massive traffic spike caused by a page being mentioned in a magazine. This resulted in the web server using swap space for the first time since it was commissioned. Without that swap space things would not have gone very well.
Not a good idea. You can define 2 swap files with different priority. One smaller that is in use
and one larger that will be used in case the first fills.
Also vm.swappiness can help you control how aggressively the disk swapping occurs.
If you inset a 0 to vm.swappiness that doesn’t necessarily mean that the system will not swap. It is a parameter to determine how aggressive kerne’s tendency would be to swap but it doesn’t turn off swap.
Again, swapping is not bad but thrashing is. Take a look of the sysstat data and that should give pretty good pointer on it.
Kubernetes Free Online Tutorial, Kubernetes Beginner Tutorial
DevOps Free Online Tutorial, DevOps Beginner Tutorial
Ansible Free Online Tutorial, Ansible Beginner Tutorial
Docker Free Online Tutorial, Docker Beginner Tutorial
Openstack Free Online Tutorial, Openstack Beginner Tutorial
Disclaimer: This has been sourced from a third party syndicated feed through internet. We are not responsibility or liability for its dependability, trustworthiness, reliability and data of the text. We reserves the sole right to alter, delete or remove (without notice) the content in its absolute discretion for any reason whatsoever.