15

Do we need actually swap partition on the ubuntu-server with LAMP? I think I don't need it, but better to know for sure if it will not cause some unpredicted behaviour.
Actually my thoughts were:

  • The server never hibernates
  • If it is swapping one need to think about loadbalancing/traffic shaping, etc...

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

Thanks!

5 Answers 5

14

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.

11
  • Same experiences here, kinda... it was a mistake in my side not a deliberated decision. A server without swap is a hell to repair, specially if it decides to kill sshd. Nov 29, 2010 at 10:31
  • I have a about 16Gb RAM, the half of it is cached for fast IO, the rest is for LAMP, The swap is always free, or some times few megs is there, but i think it is always off...
    – Arman
    Nov 29, 2010 at 10:33
  • 3
    In the world of successful websites, unresponsive is worse than broken. Users will actually appreciate a FAST failure(which, your javascript frontend code should be handling gracefully btw), but they will HATE you for being slow. Ditch the swap, it just delays the inevitable. -1
    – SpamapS
    Dec 6, 2010 at 15:20
  • 1
    @Oli: Running N+1 no longer takes megabucks, or even a lot of bucks. In fact it hardly even takes special skills. Its inevitable that a server will go down for any number of reasons, and not that hard to prevent this from being a non-issue. If you have a stand-alone LAMP server doing everything, then what costs more; Setting up two more and a load balancer (on EC2 with t1.micros and EBS snapshots, this can be VERY cheap), or your site being unusably slow on its biggest day? Check the data from google... I think its clear bit.ly/hB1AD1
    – SpamapS
    Dec 6, 2010 at 17:08
  • 1
    Great answer, covering real-world server cases. Adding redundant hardware, LB, monitoring, RAM caches, etc are all incredibly important, and you'll have time to set up and debug them if you're not futzing around because you cheaped out on swap space. Dec 6, 2010 at 22:43
5

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.

2
  • 1
    Thanks for sharing your experience. I was accounting with the similar problems, when ssh was taking Inf. I just forced the processes to have limited memory, that was allowing me to get the ssh running and fixing buggy scripts.
    – Arman
    Dec 7, 2010 at 8:12
  • One of the most interesting discussions on swap space in production that I have seen on the internet. (the whole thread, pro & cons)
    – dpb
    Dec 18, 2013 at 19:04
3

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

2
  • Thanks for explanation. If one has a buggy script then in any case you will crash the server, the system limits should control your scripts, Is not it?
    – Arman
    Nov 29, 2010 at 10:31
  • aneeshep, if you are swapping during heavy traffic, your system is going to be 100x slower than it should normally be. Thats generally not acceptable.
    – SpamapS
    Dec 6, 2010 at 15:37
2

Using swap will give you one additional safeguard against server instability. There may well be times when RAM is running out, and on server without swap that could result in a soft crash.

I'm probably in the minority now when I say, it still makes sense, like they used to recommend, to have twice as much swap as you have main memory. Even on a system with 96GB of RAM.

It doesn't cost much and, if you need it one day, you'll be glad you got it. The reason for enabling swap is just a very straightforward cost-benefit analysis. Do it! :-)

0
0

I'd like to thank Oli for the beautiful - i know old - answer. I find partitioning is an always-green topic! I totally agree with the line of Oli's post and i'd like to share this - of course improvable - script i use to monitor my servers's swap usage.

I always configure servers and services to operate with no swap occurrence at all. When it will, be sure that something is either going wrong, or at best, something's getting over your initial plans.

I crontab this script every half an hour in production environment. It will send me a notification if Swap usage != 0k. I will be able to promptly investigate/take actions on the issue, most of the times, before things gets really wrong.

Expects you to have: bash, top, echo, awk, and a working mail command, without performing any checks.

Hope that helps.

#!/bin/bash
CURRSWAP=$(top -b -n1 |grep Swap |awk '{print $4}')
ECOMM="echo $CURRSWAP means healthy, I wont take any action."
CURRDATE=$(date)
MAILDST="[email protected]"

case $CURRSWAP in
  [0]k) $ECOMM
        exit 0
        ;;
  *)    echo -e "Server: $HOSTNAME \n Date: $CURRDATE \n Current Swap partition usage: $CURRSWAP" | mail -s "Warning from $HOSTNAME" -- $MAILDST
        exit 0
        ;;
esac
exit 0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .