The Raspberry Pi and the SD Card
Built by the Raspberry Pi Foundation, the Pi is one of the most successful single-board computers ever built. Although it is cheap, it was designed to provide enough power to make it a viable educational platform that schools and colleges could adopt and embrace. Families could afford to have the same type of equipment as the schools.
Almost as soon as it was launched in 2012, the potential of a small, cheap, low-power computer capable of running a genuine Linux distribution saw it adopted by makers and hobbyists. Sales of the Raspberry Pi in its first ten years exceeded 45 million units.
Shortly after the Raspberry Pi was launched, stories started to spread about the corruption, destruction, and short-lifespan of SD cards. The Raspberry Pi contains no internal storage. The owner must provide an SD card—nowadays a microSD card—that acts as the hard drive of the computer. The operating system and applications are installed on the SD card, and the tiny computer boots from the SD card too.
The implication was that the Raspberry Pi was shortening the life of the SD cards. It turned out there were underlying problems with the SD card controllers—small microchips physically sited on the SD card—and issues with SD card drivers. It wasn’t an issue unique to the Raspberry Pi.
However, there are many variations of SD cards and some cards are neither designed nor equipped to withstand frequent write cycles.
Why Do Some SD Cards Fare Better Than Others?
SD cards were designed to be removable and highly portable storage devices. They are best suited to storing data that doesn’t change. That means there are very few write cycles, but as many read cycles as you like.
It wasn’t long before they were adopted as the storage medium of choice for digital cameras, cell phones, drones, and other products that required light, cheap, and replaceable storage. As often happens in technology, the demands of the products drove up the quality and capability of one of their core components. The demands for more speed and more capacity kept pushing the SD card manufacturers to improve the performance and robustness of their cards.
Speed
The SD Association has devised a set of standards that define the characteristics of an SD card. Regarding capacity, SD cards should be labeled with one of the following.
SD: Secure Digital. Up to 2GB capacity. SDHC: Secure Digital High Capacity. Between 2GB and 32GB capacity. SDXC: Secure Digital Extended Capacity. Between 32GB and 2TB capacity. SDUC: Secure Digital Ultra Capacity. Between 2TB and 128TB capacity.
Speed is a bit more complicated. The SD Association uses speed classes to designate the minimum sustained write speeds. The classes are:
Class 2: Minimum sustained write speed of 2MB/s. Class 4: Minimum sustained write speed of 4MB/s. Class 6: Minimum sustained write speed of 6MB/s. Class 10: Minimum sustained write speed of 10MB/s.
That seems straightforward enough. But remember these are minimum values. In reality, you ought to be able to achieve higher write rates. The packaging of some cards will have wording similar to “transfer rates up to XXMB/s” with the “XX” replaced by the best rate that the manufacturer was able to achieve in laboratory conditions. The important point here is the wording. The “up to” means your mileage will vary.
There are two other speed class-related bits of information on the label of the card. It’s easy to get these two confused.
The UHS Speed Class is shown as a digit in a U-shaped container. It refers specifically to recording video.
UHS Speed Class 1: Will support up to 10MB/s write speed. UHS Speed Class 3: Will support up to 30MBs write speed.
The final bit of speed information uses Roman numerals. Speed class-rated SD cards will use one of two interfaces or buses. Note that these speeds refer to read speeds. These are not sustained speeds, they’re peak speeds.
UHS-I: Will support up to 104MB/s read speed. UHS-II: Will support up to 312MB/s read speed.
Wear Levelling
Continually writing to the same location on an SD card will eventually shorten its life. Most known-brand SD cards contain an implementation of wear leveling. Wear leveling shares the write actions across the surface of the SD card.
If some blocks on the SD card are being written with sufficient frequency to trigger the wear leveling algorithm, the write actions are moved to a different region of the card. This stops a few areas from being bombarded with write actions. It’s like having a laser beam pointed at a metal door. If it is left in one spot it might penetrate the door. If it is moved all over the surface of the door, it won’t burn through anywhere.
Of course, for this to be effective there has to be enough unused space on your SD card to permit the write actions to be moved around. This brings us to capacity.
Capacity
Don’t purchase an SD card that just about meets the minimum requirements of the Raspberry Pi image you’re going to burn to it. Buy one with some breathing space. As well as letting wear leveling do its thing, spare capacity allows you to install applications and data, and it lets system essentials such as swap operate as they should.
Industrial Grade Cards
These are variously marketed as “High Endurance” or “Industrial” grade cards. They’re designed for grueling high-write environments. But read the small print to see what “High Endurance” or “Industrial” actually translates to. Is it just marketing babble?
Genuine industrial-grade cards are very expensive. Their robustness is reflected in their price., which is far outside the budget of the casual hobbyist.
Tricks to Prolong SD Card Life
In a Raspberry Pi, the SD card takes the place of the hard drive. This type of usage is more intense than in, say, a digital camera. There are lots of small write actions taking place all the time. And it’s actually worse than it first appears.
When a file is created, updated, or accessed, the data is written or read from the SD card. These actions cause timestamps in the file system to be updated. So, counterintuitively, even reading from a file will cause a write action that updates the access time for that file.
The small print in the warranties of some SD cards specifically states that using the SD card in “high write-rate” scenarios is unsupported. Here are some things you can do that will help.
Shutdown Cleanly
Always shut your Raspberry Pi down just like you shutdown your desktop or laptop. Don’t just pull the power cord out. If you don’t have a keyboard and screen attached to it, use SSH to access it and perform a controlled shutdown.
The noatime Mount Flag
If you use the noatime mount flag in your “/etc/fstab” file, the file access timestamp atime won’t be updated each time a file is accessed. If you don’t need to keep track of this metric, you can turn it off.
As well as preserving your SD card it can give you speed gains too, depending on what you’re using your Raspberry Pi for.
RELATED: How to Write an fstab File on Linux
Use an External USB Drive
You can configure your Raspberry Pi to boot from the SD card, but use an external USB drive as additional storage. If you move your “/home” directory to the external storage you’ll reduce the wear and tear on the SD card dramatically.
Disable Logging
Turning off unwanted logging or sending the logs to an external USB drive will reduce the impact on your SD card.
Use a Decent PSU
Don’t run your Raspberry Pi on an ancient cell phone charger. Your Raspberry Pi can require up to 2.5A at 5V. Anything less can lead to many different problems including instability and SD card corruption. Don’t fall foul of false economy. Invest in a decent power supply unit.
What About Swap?
You’ll hear people advocating turning off swap. You can do this, but we don’t recommend it. Instead, you could create a swap partition on an external USB drive and move swap off the SD card.
Swap is an important part of running a sane Linux installation. It isn’t just used for paging out memory blocks from RAM when demand for physical memory outstrips what your Raspberry Pi is equipped with. Swap is also used for updating applications.
If you install a newer version of an application while the old version is still running, the old version can’t be unloaded from memory because it no longer has a place on the hard drive—or SD card—from which it can be re-read.
The kernel’s memory management routines relocate the old application image to swap so that it can be memory managed with some out-of-RAM storage. When the old application terminates the swap and RAM pages are freed. The next time that application runs, the new version is used which has file system-backed storage allowing normal memory management processes to be followed.
And trying to stave off when swap will cut in by manipulating the swappiness value is pointless. That’s not what the swappiness setting does.
Leave swap running and, if you really want to, use a swap partition on external storage.
Go Expensive or Go Cheap?
The first thing to do is decide on the capacity of the SD card you need. It’ll probably fall between 8GB and 32GB for most average domestic uses. If you need more storage than that you really should be including an external USB drive in your project design.
Cards in that 8GB to 32GB capacity range are reasonably priced and affordable to most people. Given that’s the case, why go cheaper? It’s not as though the savings are significant. If they are significant, the cheap card is likely to be a fake.
The market is awash with counterfeit SD cards. The card and its packaging can look exactly like genuine cards from reputable manufacturers, like SanDisk or Samsung. Another scam is to relabel genuine cards so that they appear to be of higher capacity than they are. So buy your SD card from a reputable trader. Beware too-good-to-be-true bargains, especially if you’re buying online. They’re not bargains, they’re scams.
Even genuine, lower-priced brands use cheaper and less stringent manufacturing processes and give you reduced performance. Given that a Raspberry Pi is a punishing use-case for an SD card, you don’t want to skimp.
A known-brand class 10 card with UHS Speed Class 3, and UHS-I bus speed will suit most applications. If you’re using a camera with your Raspberry Pi and will be recording a lot of images or video, select a card with a UHS-III bus speed.
Get a card of sufficient capacity to meet your needs, including some spare capacity to let the wear leveling functionality share write actions across the spare surface of the card.
But cheap SD cards? No. You’re just buying future problems.