Do we need Swap partition on a LAMP server?

6,114

Solution 1

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.

Solution 2

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.

Solution 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

Solution 4

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! :-)

Share:
6,114

Related videos on Youtube

Arman
Author by

Arman

I am a researcher. Currently working within the eScience team at AIP. Developing tools for Virtual Observatory and serving SDSS data mirror servers. My Research interests are: Galaxy formation Hydrodynamics N-Body interaction AGN influence on galaxy formation Large scale structure Parallel programming Visualization tools

Updated on September 17, 2022

Comments

  • Arman
    Arman almost 2 years

    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!

  • Arman
    Arman over 13 years
    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?
  • Javier Rivera
    Javier Rivera over 13 years
    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.
  • Arman
    Arman over 13 years
    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...
  • Oli
    Oli over 13 years
    As others have said, you could have hundreds of gigabytes of RAM and a process could still leak all over it. In your case, I'm not sure I'd recommend going for RAM*1.5 but keep a few gigs of swap on, just in case.
  • SpamapS
    SpamapS over 13 years
    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
    SpamapS over 13 years
    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.
  • Oli
    Oli over 13 years
    @SpamapS: A comment would now allow me to vent all my Monday-afternoon rage back on you, so I edited the post. In short: I respectfully disagree.
  • SpamapS
    SpamapS over 13 years
    @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
  • NightwishFan
    NightwishFan over 13 years
    Also, you do not need a swap partition a swap file works fine if that would be better for your needs.
  • Oli
    Oli over 13 years
    @SpamapS It's still at least as much money as your first server, again. Even more if you want to actively balance and replicate. Small operations can't (or don't want to have to) afford it and large hard-to-horizontally-scale applications that might sit on a 16GB-of-RAM server are very expensive. A smidge of swap space, as I've explained twice, isn't there to run on for longer than it takes to fix the issue, but it means you stay up and stay in business. Linking to Google's data on speed is irrelevant as they don't count the cost of downed services (ie they don't support what you say).
  • ImaginaryRobots
    ImaginaryRobots over 13 years
    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.
  • SpamapS
    SpamapS over 13 years
    @Oli: Even for a very small business, they will understand that when users leave, and vow to never return because the site is slow for an hour while you're fixing it, they have lost money. How much? Hard to say, but in the past I have found it easier to convince business owners to take on reasonably low fixed costs than variable unknowns like "when will we accidentally eat up all the RAM on the one server." However, I think this horse is dead, so I'll lay down my bludgeon.
  • poolie
    poolie over 13 years
    @SpamapS: If you want fail-fast, that's great: configure a low timeout in your web server or application. Turning off swap is an incredibly poor way to accomplish it, and it's going to make it harder to recover or even debug if you ever do get into a problem situation. btw I'm pretty sure ec2 images do have swap turned on.
  • Arman
    Arman over 13 years
    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.
  • dpb
    dpb over 10 years
    One of the most interesting discussions on swap space in production that I have seen on the internet. (the whole thread, pro & cons)