SS file updates - will I wear out the card with frequent file overwrites?

Davidelvig

Well-known member
I have a JSON config file that could be updated frequently.
I mirror the json doc in memory at runtime, and need to push it back to the SD card… either with each little change to configuration, or otherwise require the user to make some explicit Save action. (I could take a time-based approach as well, I suppose… committing if there are changes every few seconds or minutes)

if I do the first option , frequent replacement of the file with each configuration detail, am I going to wear out the SD faster?
 
The answer must be yes, but by how much I don't know. What about adding some SPI FRAM which can be written very fast many many times?
 
Seems the writes could be spread across the media by pre-allocating a file and just putting updates on the end - followed by a flush or write in 512 blocks?

Not sure of all the details - but that should keep the directory entry from getting hit each time and not have the data end up quickly reusing the same file space.

On startup just read through all the record until the last one is found.?
 
Modern flash memory basically always has wear leveling built into the firmware of the storage media. So, frequent writes will cause it to write to different spots on the card and level out the wear and tear. But, they may not do wear leveling on the blocks taken up by other files. This is why having a solid state drive near capacity is problematic. It can't wear level on things that don't ever change so those blocks are allocated and unavailable forever. If you then write to a file lots of times, it can't find very many other blocks to use for wear leveling and some blocks get worn out early. This is death for the media.

TL;DR - Use a card with lots of free space and probably also use time based writing to slow down write frequency a bit. The card will last a long time (several years at least)
 
The best anyone can do is estimates based on some guesswork, because SD cards don't come with detailed technical specs about the endurance of the underlying flash memory, nor details of how their wear leveling works. So this situation always involves dealing with unknowns.

But the biggest unknown (at least to me) is the meaning of "frequent". You mentioned "every few seconds or minutes". Other words like "require the user to make some explicit Save action" seem to suggest this might be based on human interaction.

For flash longevity, there is a world of difference between an automated process which writes to the media every few seconds running 24/7 versus human interaction which might change settings perhaps a dozen times per day?
 
But talking only about the SD card, here's some guesswork.

Let's assume writing the file causes 256K of underlying media to be erased and written, even if the JSON file is much smaller. Some write activity is probably occurring due to updating the filesystem metadata (directories, file allocation tables, etc). The card also does some sort of media management internally. 256K of media actually cycled is sheer guesswork and perhaps pessimistic.

If the card is 32G size, and let's assume it has well implemented (ideal) wear leveling, then you could expect 125,000 writes to erase/program every underlying flash sector once. Wear leveling across a large storage device gives you a *LOT* of writes...

The next big unknown is the underlying flash endurance. It's not going to be 100K guaranteed writes like we get with NOR flash. Let's assume it's only 1000 writes before errors begin to appear. That would imply you can write the file 125,000,000 times.

However, the controller inside the SD card (probably) implements some sort of bad block detection and remapping. Maybe. If it does, you could expect to get more life from the flash. But how much more, who knows. Maybe 2X or 3X?

But let's go with the 125M writes estimate. In the "every few seconds" seconds scenario, that's 1200 writes per hour. If sustained, it becomes 28000 writes per day, or 10.28M per year. If the guesswork of 125M "small" file writes wears out the card, you could expect it to last about 12 years.

Of course, low quality cards might have controllers which don't wear level well (or at all) and they might also have second rate flash memory capable of less than 1000 program/erase cycles per sector. It's all a lot of guesswork.

But the general idea of writing every 3 seconds, sustained 24/7 forever, means you've probably got several years. If you write several times per second, your card might wear out within months. But if the writing is only once per minute, or if the writes are in response to human activity rather than an automated process that runs 24/7 for many years, I would imagine any good quality card will ultimately fail due to aging processes rather than reaching its flash write endurance.
 
Last edited:
I would not rely on a SD card for an important file written so often. External flash chip is maybe more reliable, I don't know.
The best solution is to use a FRAM.
 
Back
Top