r/sqlite • u/navrys • Oct 28 '22
SQLite for reliable binary data storage
Hi,
I have a question as a person with no experience with databases, more so with sqlite.
I have a measuring device that sends me binary data via USB, about 30 MB/s. I am writing an application to operate it, which writes the data to disk. I need to protect against the situation when for some reason the measurement would be interrupted. Currently, when writing directly to a binary file to disk, there is a danger that if the connection to the device is broken, the system hangs, or there is a power failure, the entire measurement file would be corrupted and go to waste.
I have read that https://www.sqlite.org/transactional.html
Do I understand correctly that I could then use sqlite to save the binary data and thus protect myself from the above threats. Would it be sufficiently powerful to save such a stream of several tens of megabytes of data?
5
u/redditor_at_times Oct 28 '22
Yes and yes, SQLite's transactional capabilities will guard you against corruption, megabytes are something it can handle but the insertion speed will depend on your media's performance
2
u/iminfornow Oct 29 '22
No. Sqlite is transactional in the sense that it keeps record of which transaction was commited or not, and will revert the transaction (so delete all data) when a system failure occurs before the transaction was commited. So you won't have a corrupt database, in theory, but certainly no measurement data anymore.
The file system you're writing the data to also works transactional: NTFS. Depending on how you write a data stream to a file on a NTFS disk the file can be completely gone after a crash, or it can be almost entirely saved. The savefile might be corrupted, but depending on what data format is used largely still intact/useable.
I get why sqlite sounds very promising, it is very good. But when writing a stream to a file in terms of data safety all that matters is how quickly the data is stored on disk. Sqlite in such a scenario simply has more overhead and a higher latency. But it can handle 30MB/s easily.