Tuesday, February 19, 2019

Using an SSD as a Linux swap device isn't (usually) as destructive as you thought it would be

It's been a while since I've posted something on this blog but there's something I'd really like to get out of my head.

I've read, and continue to read, claims from others on the Internet that putting swap space on an SSD is bad for it and will quickly kill it. However, these statements rest on the assumption that the swap space will be used heavily or that the SSD's endurance is low enough that this would cause it to fail well before the end of its designed service life. In the vast majority of situations, neither of these are true. Even on bottom-bin hardware like Bifrons (a cheap Lenovo netbook with 2 GB of memory and 64 GB of eMMC flash storage), where endurance is more likely to be an issue, it's still very likely that the flash memory will last the intended life of the system.

Several months back, I took an old 64 GB Plextor M5M mSATA SSD previously used in one of my old laptops, put it in a StarTech USB 3.1 Gen 2 enclosure, and plugged it into the USB 3.0 port on this laptop. I then secure-erased it, partitioned 8 GB of that drive as a swap partition, and enabled it as swap space. Using the Linux iostat command, I monitored the amount of data written to the drive over time as I used the system for everyday web browsing and productivity. The worst I saw was 170 GB of writes to the drive over just shy of nine days of usage, or about 20 GB of writes per day from swapping. Remember that this system has just 2 GB of memory so some swapping is likely to be unavoidable. It's also noteworthy that swappiness was set to 100, which makes the system swap more aggressively than normal (the default is 60).

20 GB per day is well within the endurance limits of most modern SSDs. A good name-brand SSD like the Samsung SSD 860 EVO will be rated for far more than that over its warranty period; the 250 GB version of the 860 EVO is warranted for five years or 150 TBW (total terabytes written). That's 84 GB per day over five years, and 20 GB is less than a quarter of this value. Even if you add another 10 GB per day (because a typical user would put the system/boot partition and the swap partition on the same disk), you're still not using even half of the drive's rated endurance within its warranty period. Even cheaper SATA SSDs with lower endurance ratings are unlikely to fail under this kind of workload.

This does not mean that swap is completely harmless, as endurance problems can still occur if you're swapping to a low-cost, low-capacity flash memory device. The eMMC flash module on Bifrons is rated by its manufacturer (SanDisk) for 44 TBW of endurance. This is 40 GB per day over three years, or 24 GB per day over five years, which should be enough to last the service life of the device. However, eMMC modules are permanently installed in the device and cannot be replaced without highly-specialized equipment (they are soldered onto the motherboard). Furthermore, the limited capacity of the device means that the typical consumer will use most of its capacity, which increases write amplification and reduces the effective endurance of the NAND, possibly below its specifications, and many netbooks of this kind have just 32 GB of storage which further compounds the endurance issue. (The same also applies to low-cost external flash memory devices like SD cards or USB flash drives.) This is, in fact, the reason I put the swap onto an external SSD and not on the system's onboard flash memory.

But this isn't the case for the vast majority of system configurations, where the benefits of placing swap on an SSD outweigh the risks. Most flash memory devices are far faster than any electromechanical hard drive; even if swapping significantly degrades system performance, the performance impact of swapping to an SSD is much lower than swapping to spinning rust. The above endurance issues only truly apply if 1) you're swapping to a cheap and small flash memory device like an eMMC chip or USB flash drive; and 2) your system has low memory and your usage patterns are such that you need to swap more than occasionally. Most systems have enough memory that swapping only happens rarely, if at all, rendering the whole endurance issue moot. And even when swapping is necessary, the drive isn't going to be written to so much that it'll exhaust the endurance of the drive and cause it to fail prematurely. Swapping isn't going to cause the drive to get hundreds of gigabytes or terabytes of writes per day. The 20 GB per day figure is the highest that one might encounter in a typical day of usage, and most users aren't going to see this much swapping.

If endurance is still a concern, there are solutions like zram which can effectively compress the contents of system memory and therefore allow more data to be stored in RAM before the system needs to swap to disk. While this comes at the cost of increased CPU usage, you'll still get better performance than if you're swapping to disk, and fitting more data into memory means that less data needs to be swapped out and written to the flash memory.

Ultimately, for the vast majority of use cases, putting swap on your SSD isn't going to kill it.

No comments:

Post a Comment