SD cards are the literal worst.

they've expanded to be the size of small hard drives, and devices like the rpi keep using them as boot media, but they:

- use garbage tier low endurance flash cells internally
- have little to no overprovisioning for wear
- perform only the most basic wear levelling
- have no protocol level integrity checking
- have few internal error correction features, if any
- decay comparatively quickly without patrol scrubs
- do not perform patrol scrubs
- cannot do PLP

I've been mad about this pretty much forever. Don't use SD cards for stuff where there's literally any other option.

something I've been thinking about a lot is engineering panic signals into embedded stuff.

put a rail supervisor on the incoming supply, and leave enough capacitance on the low side of your buck(-boost) reg to keep the device running for a few milliseconds when the power is pulled. treat the supervisor IC's PG output as a panic signal and have it trigger an IO interrupt on your MCU/SoC which tells the filesystem code to ensure integrity, flush, and halt IO.

we think of PLP as being a fancy enterprise feature on PCs, but on embedded stuff it's actually legit important 'cos the user often just yanks the power.
@gsuberland it’s true! I know about this and I still yank the power sometimes before I realize it
@mirabilos @gsuberland Power Loss Protection. Basically a flush and sync now panic signal with a few ms (or more) energy buffer.

@gsuberland That and ECC RAM.

Doesn't help that as laptops have gotten popular devs have gotten lazy at filesystem power loss support.

@gsuberland Yep this is something I've started to design into my architecture, prototyping on the trigger crossbar and then scaling to a next-generation system with more capabilities on my next project.

Luckily there's not a whole lot to worry about in terms of data integrity because MicroKVS is a) seldom written and b) designed to not corrupt data if interrupted at any point.

At some point I need to build a board where I can test that assumption: have one MCU that controls power to another and the target is just constantly booting up, doing KVS reads and writes, and being randomly reset/power cycled.

And see if I can make it break.

@gsuberland Why low side? High side seems better wrt brownout detection.
@dascandy rail supervisor on incoming supply is high side?
@gsuberland What if you used ZFS? Honest question.
@apicultor someone else asked the same. it would give you better error detection and correction, and scheduled scrubs would help resolve the decay issues, but those scrubs also increase the write wear rate so you're still left using media that will die pretty quickly.
@gsuberland Are you sure that scrubbing causes writes (other than to update the last scrub timestamp)?

@apicultor what I mean is that if the goal is to get around bitflips and bitrot and write wear issues with ZFS, the correction itself will cause more write wear.

it's better than not using ZFS, but it accelerates the overall inevitability of complete failure.

@gsuberland Ah, legit, yes.

What about SD cards that don't suck — as in, ones that have their flash configured in SLC mode so you get bonkers durability like 5K TBW (for the largest ones, reducing proportionally by capacity):

https://www.transcend-info.com/embedded/product/embedded-memory-cards/usd230i

https://www.transcend-info.com/embedded/product/embedded-memory-cards/usd240i

Not quite as impressive, but still ~2.8K TBW if I did my math right (it's expressed in hours of HD video at 26 Mbit/s):
https://www.transcend-info.com/product/memory-card/usd350v

USD230I | microSD Cards - Transcend Information, Inc.

Transcend's industrial-grade USD230I microSD card operates in SLC mode for superior performance, and comes with wide-temperature (-40°C to 85ºC) support.

@apicultor sure, but the better solution is just to not engineer systems that need you to very specifically pick special cards. an SD with long write longevity still only solves a few items off the list.
@gsuberland I wish they shipped with good eMMC onboard.
@gsuberland
Even then if the sd cards lie when you send the flush command, it won't help. We've had this problem for years with ata drives trying to win benchmarks.
@gsuberland I agree with you, but what other options are there at the moment? I guess USB drives might work.

@kelpana @gsuberland USB-attached SATA or NVMe SSDs.

USB flash drives are the exact same (or often worse). Don't use them either.

@manawyrm @gsuberland I know some USB flash drives are bad, or even just SD cards. But what can be used to interface to a microcontroller project? Is it even possible to interface NVMe in a small system?

@kelpana @gsuberland SD cards can be okay, if you use them read-only-only and buy from reputable vendors (e.g. Samsung).

If you really need R/W storage -- SPI NOR flash is a good option (together with proper wear leveling, etc.).

eMMC storage ICs are also available, often slightly more reliable and available from reputable vendors with a proper datasheet. They might work in some situations.

Otherwise, things get tricky...

No, NVMe won't work.

@kelpana @manawyrm for MCUs it generally makes more sense to use an EEPROM most of the time. that way you can select parts with known longevity properties that you can engineer around. depends what the project requirements are though.
@kelpana @manawyrm @gsuberland wouldn't NAND or NOR flash be better? If you have a Linux kernel, you have UBIFS

@raito @kelpana @gsuberland Yes, if you have a Linux kernel, you suddently have more good options.

"Microcontroller project" just didn't sound like Linux to me :)

@manawyrm @kelpana @gsuberland agreed; as we talked about RPIs in the start, I was considering the large spectrum of microcontroller projects

Nonetheless, I suppose that even ESP32 grade stuff could get access to a good implementation of something like UBIFS?

@raito @kelpana @gsuberland I'm sure you could implement something -- but I'm not sure if it exists yet. Haven't seen it yet.
@kelpana @manawyrm @gsuberland (also: are eMMC as bad as SD cards? I felt like they had better properties)

@raito @kelpana @gsuberland You can at least buy them from a proper source, with a datasheet and can be pretty much guaranteed to not get fake chips.

That helps a lot. Otherwise they're basically the same.

@raito @kelpana @manawyrm eMMC is a bit better, but more than anything the manufacturers are just more transparent about their performance.

@gsuberland @raito @kelpana
Look at this datasheet: https://wmsc.lcsc.com/wmsc/upload/file/pdf/v2/lcsc/2007301503_Samsung-KLM8G1GETF-B041_C499918.pdf

You can also often configure/partition eMMC flash to work in pseudo SLC mode (trading size for reliability).

I know that at least some Micron eMMC has working built-in data refresh... Not sure about other vendors.

@manawyrm oh neat. I know the eMMC protocol has much better guarantees about operation timings and caching, so that helps a lot with designing stuff for data integrity.
@kelpana @manawyrm @gsuberland eMMC or raw qspi connected flash.
@kelpana USB flash drives are better, although still have issues. CompactFlash is ok, SATA/UAS or NVMe is preferred where you can (but obviously much more complex). For stuff where you only need firmware and a small amount of persistent storage (e.g. for a config block) it can make more sense to use EEPROMs and select parts that meet your longevity needs, and implement your own wear levelling / scrubbing where needed.

@gsuberland i wish they made small capacity (full physical size) sd cards that do proper wear leveling and use good quality and overprovisioned flash
maybe with a built in indicator for how healthy the flash is?

would be very useful to a lot of people

@gsuberland and it only takes a sneeze to snap it in half
@gsuberland also made worse by the pi foundation pretending that unregulated usb power is fine, a platform that draws more current than suitable for non PD usb and SD cards pretty much assuming that power will totally be fine.
@xssfox @gsuberland you mean the raspberry pi company, they haven't been led by a public benefit for a while now.
@gsuberland cosign everything you said--also wanted to add that you can buy marginally-improved somewhat-hardened sd cards: "SanDisk Industrial" https://www.mouser.com/datasheet/2/669/SanDisk_Industrial%20Grade%20SD%20%20MicroSD%20Product%20Brief-805940.pdf
(I continue to distrust them, ofc)
@gsuberland As far as I've ever been able to determine, there's no way for the host to inquire whether the SD card has flushed all of its dirty RAM buffers to flash, such that the host knows e.g. that it's safe to power the card down.
@brouhaha yup, afaik there's no guarantee. all you can do is try to keep it powered as long as possible after the final write command.
@gsuberland what's really annoying is that they could do a lot of that, but don't because it's marginally cheaper and for most consumers, it's difficult to notice the regularity of their failures.

eMMC is essentially the same stuff but soldered on, but because it's always used in a large scale, companies
will notice all of that stuff, and therefore money will be spent on reliability.

realistically, the only thing SD cards couldn't do is PLP
@ignaloidas yeah, it's all entirely doable, just costs money. the PLP stuff can be implemented on the host device but again most don't bother.

@ignaloidas @gsuberland SD and miniSD cards *could* do PLP. Just use a microSD card internally and fill the rest of the space with capacitors.

I know some microSD cards use a proper SSD controller with a single channel to a flash die. They have real wear leveling and page sparing. Not sure if they do internal patrol scrubs. SanDisk Industrial is one of the lines which works this way.

@bob_zim @gsuberland I don't think that kind of PLP is in any way cost efficient, just some helping from host side would be cheaper.

And yeah, you can get proper SD cards if you go for industrial ones, but it's both significantly more expensive, and harder to get. Even in SBC-focused shops, rarely will you see one (even though you'd really want one for SBCs).

@ignaloidas @gsuberland It would be exactly as cost (in)efficient as it is with desktop SSDs, since that’s how they do PLP.

As for the added expensive of high-quality cards, basically every card of a given form factor from 64 GB down is the same price: about $10 US. I was recently shopping for a pile of cards. The most expensive I found were $12.70 for single industrial 16 GB cards. Oddly enough, 32 GB (the max capacity for SDHC) industrial cards were $11.

For 128 GB and higher, yes, high-quality cards start getting more expensive than the low-quality cards. I personally don’t use SD for anything except data which is trivial to reconstruct (like maps in a GPSr), or functionally disposable (like dashcam footage).

@bob_zim @gsuberland eh, there is a fair difference - to fit enough capacitance in SD card, you need extremely high density capacitors, while desktop SSD's can allow themselves to use cheaper, less dense capacitors (or capacitors with larger minimum sizes, like polymer or tantalum or aluminum)

For the price, the last time I looked it would cost significantly more for me to get at hobbyist amounts (<10), partly because at least from my looking, the prices were noticeably higher, partly because I would need to deal with international shipping because there's no big suppliers local to me. I could get the same capacity but crappier cards faster and ~2x cheaper just getting from local stores.

@ignaloidas @gsuberland An SD card only drives the controller and one die. It doesn’t need nearly as much power to complete in-flight writes. M.2 drives (e.g, Micron 7450) already typically have less volume in capacitors than the spare volume in an SD card.

Cost of shipping can definitely be a pain.

@gsuberland what I’m hearing from this is that I should really, really not be using the SD card in my camera as long term storage   guess who’s backing it up as soon as they get home today?
@carbontwelve oh yeah absolutely. you can expect to see bit-rot on SD cards for anything that was written longer than a year or two ago.
@gsuberland oh dear haha. Yeah this is going on five years. Didn’t have a computer to copy it over to until this year and so it’s just sat on the shelf, forgotten!

@carbontwelve oof. fingers crossed for you.

our wedding photographer gave us our photos on a USB stick. every single photo on it is now unreadable. (naturally I made backups immediately once we got them!)

@gsuberland I thought about this recently when I realised that my Playstation 1 memory cards, whose 1MB of storage was last written to 25-28 years ago, all still work perfectly

@gsuberland

Their merit is that they are cheap and easy to replace.

@gsuberland IMO it's SBC maker's problem, not SD card's. You are not even supposed to reformat the card's original partition else risk damaging it.
@gsuberland WD made a go at selling a hot pluggable case for m.2 nvme to pro photographers a while back and I kinda wish hot pluggable m.2 2230 in a trenchcoat competed with microsd, cfexpress, etc., with the drives so cheap and good now