Should swap and RAID-1 mix?
I have a box with a small RAID-1 array being used for swap space. I only did this since I was setting up mirrors for every other partition in the system.
Today I read this question and it prompted me to wonder: "is having swap on RAID-1 ideal / advisable ?".
You should (probably) not take your swap off the RAID 1.
It's sometimes less important have swap on a RAID 1, but it's fine, usually helpful, and often necessary. Rarely is there any reason to deliberately put your swap elsewhere, if you're already using a RAID 1.
Swap on RAID 1 May Perform Better
SWAP on RAID 1 can be expected to perform better than a single swap partition outside the RAID, but not as well as two swaps set to the same priority.
Writing to a RAID 1 is sometimes slower than writing to a single disk, since all data must be written to both disks. Often it's not really slower--usually the slowdown will be negligible. If you have a RAID 1 of two disks on the same IDE controller, it will be slower. But when accessing one disk doesn't slow down accesses to the other disk, writing to the RAID 1 will not be significantly slower.
Reading from a RAID 1 is almost always faster than reading from a single disk, since when one disk is occupied by a read, more data can be read from the other disk. Reading from a RAID 1 made up of two drives is often almost twice as fast as reading from a single drive.
I'm not aware of any tests to compare two separate swap partitions to a single swap partition on a RAID 1 (or any other RAID). There's no reason to think a single swap partition would perform as well or better than a swap on a RAID 1. The situation is more complicated if you have two swaps on separate disks:
-
If the swaps are given different priorities in
/etc/fstab
, separate pages are swapped to them; the same page is not swapped to both at once. I'd expect two swaps to be faster for accessing unrelated swapped data simultaneously, but slower for accessing a single page. -
If the swaps are given the same priority in
/etc/fstab
, data (even from a single page) will be striped. This is similar to RAID 0 and will most likely perform even better than putting the swap on a RAID 1.Theoretically if you have a hardware RAID 1, software striping could be less efficient, but unless you have unusual applications where reading from swap is overwhelmingly more common than writing to it, I would expect a non-RAID software striped swap to outperform a swap partition on a RAID 1.
(Since the kernel can stripe swapped data between multiple swap partitions, there's no reason to set up a software RAID 0 just for swap.)
See this section of the Wikipdia article "Standard RAID levels", for more information about RAID 1 performance.
Thanks to gertvdijk for providing critically important information about striped swaps.
You Might Really Need Your Swap on the RAID 1
Whether or not you need your swap on a RAID 1 depends on why you need a RAID 1 in the first place. The data in swap is typically highly volatile and losing it usually doesn't represent a significant loss of assets. However.:
-
One benefit of RAID (except RAID 0) is that if a drive fails your computer keeps running (just somewhat slower). If you have programs running that have swapped pages to disk (which virtually always is the case if swap is enabled), the drive containing a swap partition (whether or not it's the only swap partition) fails, and it doesn't have a redundant backup, your system will immediately stop working right (and will likely be entirely unusable until rebooted, depending on how the swap was being used). If you're using the machine as a production server or for any other important task, downtime will result.
If you can tolerate total swap failure from a single commonly encountered adverse event (one drive failing), you should ask yourself why you're using RAID 1 at all. You might have a good answer...and you might not. RAID (except RAID 0) safeguards against disk failure, but RAID is not a backup solution. If you change or delete a file on a RAID, or malware infects a system on a RAID, the changes are made immediately everywhere--the RAID will not enable you to recover from that.
If what you need is a backup rather than redundancy, and you don't already have a highly reliable, easily accessible backup, then you should take one of the drives out of RAID completely and use it for backups. (Of course, sometimes it is also necessary to have an off-site backup.)
-
If you hibernate your computer (i.e., suspend-to-disk), the contents of RAM are copied to the swap partition. So you can actually lose data by losing your swap partition, if your machine is hibernated when disk failure occurs.
Advanced installation section in Ubuntu Server documentation describes installation on a software RAID 1 array. From what I can see, they create a swap partition on RAID too, so it's almost a "recommended" layout.
From purely theoretical point of view, however, using RAID 1 for swap partition may provide less benefits compared to "normal" partitions - the data on the swap partition is more or less volatile, so it is less likely you'll lose much data in case of a disk failure, although a process may crash or the system may reboot if the swap partition suddenly fails.
Using RAID-0 or 2 separate non-RAID partitions may (or may not) make more sense from performance point of view, if you're concerned about performance you may need to do some benchmarking.
Additionally, all of this may affect performance only in the case your system is severely RAM-limited and the swap is heavily used. Adding more RAM and removing swap completely would be an ultimate solution, both from performance and reliability point of view :)