Raspberry Pi SD Card Wear Optimization
Raspberry Pi is a perfect choice for a tiny home server for low load levels, especially the RPi 5, which is powerful enough, cheap, and small in size. The only problem is that it works with SD cards, which die very fast if you use them on a server. Let’s see what we can do to reduce SD card wear when using the RPi as a server.
SD card trimming
We need to use the TRIM command to tell our SD card which blocks are not in use anymore (the files were deleted) to make the card proportionally use all the space and not trigger the same blocks, which will kill them extremely fast. Unfortunately, not every SD card supports the TRIM command, so good luck. First things first, let’s check our luck, if our SD card supports TRIM:
Look at the DISC-MAX value. If you see 0B only, it’s bad luck; you can’t use TRIM, and it’s better to use another SD card. If you see something else, let’s configure trimming.
Here we will enable the service, start it, and check the status. If everything works, our SD card will be automatically trimmed once per week and data blocks will wear proportionally.
You can see the last and next trimming time with this command:
Moving logs to RAM
On the server, we have tons of logs, which will exterminate the SD card very quickly. So, let’s move our /var/log to memory, so the card is not triggered every time any logs appear in the files.
Adding repository and installing log2ram:
Basically, it’s configured out of the box. If you have really a lot of logs, open the file /etc/log2ram.conf and change SIZE=40M to SIZE=128M or SIZE=256M. It will increase the memory available to store your logs. Keep in mind, this piece of memory will be reserved for logs, so be realistic; you probably will not have gigabytes of logs. By default, it will sync your logs with files daily. You can check the schedule with this command:
If you have tons of logs and/or want to reduce memory usage, reduce the SIZE parameter in the config and change the timer to sync it hourly instead of daily:
Check if everything works; you will see the size of your memory reserved for logs and current usage:
Swap optimization
The swap file is one of the main killers of the SD card, so we don’t need it. If you have a 4/8GB RPi configuration and this amount of RAM is not enough for you, it’s time to think about options. Basically, in modern RaspberryOS, the swap file is disabled out of the box: instead of using the swap file, it will just compress the memory. But let’s check anyway.
If you see /dev/zram0 - you are OK. It will just be compressed in the RAM. If you see something else, disable the swap:
and edit your /etc/fstab, just comment the line with type swap.
Optimization of MariaDB/MySQL
The database is a real SD exterminator; an extensively used DB will send you to the store to buy a new card in just a few weeks. There are a number of optimizations here. Basically, it’s better not to use an SD card for MariaDB at all, but we will try to just tune it to reduce wearing.
- Disable binary logging
I know, it’s useful, but game is game, so if you want to use a $100 computer as a server, you have to be ready for compromises. Let’s disable it then. Edit your file /etc/mysql/mariadb.conf.d/50-server.cnf and comment out these lines:
In the [mysqld] section of the config, add this line:
- InnoDB flush tuning
The storage engine immediately flushes EVERY transaction to the disk. We will fix it… In /etc/mysql/mariadb.conf.d/50-server.cnf add these lines to the [mysqld] section:
It will make the DB flush to the disk just once per second (instead of every transaction), change the size of the in-memory buffer, and disable double writing. Depending on your amount of RAM and how intensively you use the DB, set the parameter innodb_buffer_pool_size from 1G to 5G. For loaded servers, it is recommended to use 3/4 (75%) of the server RAM. Basically, 2G is almost always enough.
- Moving temp files to RAM
To avoid performing tons of reading/writing operations, we will move the temp files to RAM. We will use our log2ram that we already installed before. Create the folder in /tmp/:
and edit the temp files directory location in the file /etc/mysql/mariadb.conf.d/50-server.cnf:
Results
After all of these optimizations, you will dramatically reduce SD card wear. It will last WAY longer. Good luck with your server on RPi!