If you were to go down the ZRAM path, I’d recommend not using Docker and just use openHABian. I’m not sure how/whether docker plays nicely with Zram. But if it does, you are kind of on your own to get it set up and configured because the way it works in openHABian is not going to be relevant to a Docker installation.
If you use an SSD, I’d put anything that you mount into the container as a volume onto the SSD. If possible (I vaguely remember there was a problem booting from external drives on RPi 4, maybe that’s fixed now) I’d move everything to the SSD.
But be aware, a power failure is far more likely to occur and is just as destructive to the file systems on flash based storage. You’re efforts would be better spent making sure your RPi shutdown cleanly whenever possible (UPS) first before worrying too much about SD card wearing.
I’d second Rich’ answer as I doubt anyone ever tried to combine Docker and ZRAM in an openHAB context so it might work or not but it’s for sure that you would be on your own with any problems should they arise.
Rich, any hints on this ? Maybe something I could add to openHABian, too …
Sadly it’'s not an easy problem. First you need to set up an UPS, but not just any old spare battery. You need one that can communicate with the RPi so the RPi knows it needs to shut down. And for that it will probably end up consuming one of the RPi’s USB ports or some GPIO pins. I’ve no direct experience with any but I know they exist. You’ll have to do your own research to find something with the right cost. Searching for “raspberry pi ups” and/or “raspberry pi power management board” should get you to products to buy and tutorials to follow.
Once it’s communicating with the RPi you can either use NUT or the software the UPS to monitor the UPS. When the RPi loses power it will switch over to the battery. Here is where the software comes in though. When the battery gets below a certain charge level (usually hours later for an RPi) it will issue a shutdown -h command (or equivalent) and cleanly shutdown the RPi.
This should get you pretty far, but there may be times when the RPi has fallen off the network or is somehow otherwise semi-responsive. Rather than just yanking the power, you could wire up a button to some GPIO pins that you are a family member can press which in turn kicks off a reboot command. Search for “raspberry pi reboot button” for lots of tutorials on that.
That will give you two layers of protection from sudden power failures. Only in situations where the machine is truly frozen (e.g. kernel panic) will you need to resort to yanking the power cord. But in that case, if it’s truly unresponsive it probably isn’t writing to the file system anyway so the risk from the power loss is low.
And of course, moving stuff that writes a lot to zram is another mitigation from this. If there isn’t much writing to the file system going on, there is a lower risk of losing power in the middle of a write. But you will lose all the file system changes that only exist in zram, so while it will mitigate file system corruption, it does so at the cost of potential loss of data.
Unfortunately, I don’t run OH or anything else that I can’t afford to crash and have some down time periodically on RPis so I’ve not gone down this path with any of my RPis. My main OH server and network router/firewall (pfSense) and wifi AP are backed up by an UPS and I use NUT to cleanly shutdown all my VMs when the battery gets too low. I can get about an hour’s worth of uptime after a power outage and then, if the power is still off everything shuts down properly. At a minimum it saves the half hour it takes for my OMV virtual machine to do a file system check on all the drives when it comes back up.
What do you mean by “even more than SSD?” SSD is flashed based. If the SSD loses power in the middle of a write, not only the file being written to but any other parts of files that reside in that same sector (not sure of the proper terminology) will be lost. And with wear leveling, even files that haven’t changed since you first installed might be in that sector so you can end up with corruption of critical files like the kernel.
NOTE: on Linux, loss of power during a write is a bad idea even with HDDs, but the problem is particularly bad with SSDs and really really bad with SD and USB thumb drives.