Do we need Swap partition on a LAMP server?

Am I right can I turn off the swap for production server?

No. Always have some swap space.

I tried running a production server without swap once and about a week later, after a Wordpress update, PHP started eating far more RAM than we'd accounted for. When you run out of RAM and you have swap enabled, things slow down (sometimes a lot, sometimes just a little bit, depending on what gets shoved in there) but you're able to log in, find the problem and try to fix it.

When you run out of RAM and have no swap, processes die, things stall and a lot of the time your only option is a reboot. But until you do that reboot, things will probably break.

In my world, broken is far worse than slow.

Of course if you find your system is constantly using large portions of swap (it will very often use some just as a way of moving out old cached stuff), you obviously have a problem ("insert RAM please"), but having it as a safety net is definitely recommended.


In response to the comment from SpamapS:

In the world of "successful websites", you have hot failovers, load balancing and other tools that allow a machine to explode and have zero effect on the rest of the site. But that takes a lot of cash. Having redundant hardware isn't economical for most sites, even if they pull in money.

I completely disagree with your comment on uptime. In a traditional e-commerce set-up if people can't see your site, they can't buy from you. This isn't just e-commerce, all online commercial interests take a lot more flak if you're down for any sort of period. I know because I host sites and services for companies and run my own sites. Slow = grumpy but Down = fury. Even if you only go down for a minute at a time, if a user sees a "down-for-maintenance" notice more than a couple of times, they assume you can't keep the site up.

A slow server is less than ideal but swap isn't there to be run on all the time, it's a last resort to allow things to keep running while you fix them.

You also assume there is only one service running on the machine. Again this might be true if you have megabucks to split out everything but in the real world, things get lumped together. Multiple websites, ssh daemons, ftp servers, email servers, etc. One process leaking into swap might not even effect another service. Without swap, everything has an equal chance of instant, random termination. You have no control over it.

Of course swap isn't the only answer. You need monitoring to alert you when you're out of ram, but just pulling the plug and rebooting isn't the answer for the majority of people. I'm sure this works for whichever multi-national website you're responsible for but for us mere mortals (that make up the majority of the internet), doing that is commercial suicide.


I have to disagree with having swap on production servers.

In my experience, rotational disk swap makes your system less predictable and more prone to frustrating whole system failure. A high load, popular server that is doing anything with a local slow disk will quickly spiral into something far worse than a fail state. Response times will rise to 100x their normal level, and simple things like logging in via the console or ssh may take minutes.

SSD swap is a special case and would at least remove the seek time slow down that usually kills the system. However, writes are still slow, so you'll still end up waiting a long, long time to recover from an out of control process.

Without swap, your LAMP server will simply kill off processes to free up RAM. Proper monitoring should alert you to this, and remove servers from production if critical processes are killed. The worst case here is that your login methods are all killed off and you have to do a hard reset/power cycle. This worst case is still just as likely with an out of control swapping machine, but far harder to detect.

If you're using PHP, enable memory limits, and monitor your logs for their failures. Here's a trick, set the limit lower on your dev server than in production. If you're using mod_php under apache, set MaxRequestsPerChild to a few thousand, so that httpd's die before growing too large over time. Above all, monitor memory usage! Often times memory creeps up over time, and you just need to restart a leaky service periodically while you debug the issue.


Swap space is used when your system decides that it needs physical memory for active processes and there is insufficient unused physical memory available. If the system happens to need more memory resources or space, inactive pages in physical memory are then moved to the swap space therefore freeing up that physical memory for other uses.

This situation will happen many times in server.

a). A non-optimized script can consume large amount of memory
b). Scripts like backup will always consume huge memory
c). heavy traffic

So it is a good practice to have some swap space.

More details: https://help.ubuntu.com/community/SwapFaq

Tags:

Lamp

Swap

Server