I'm exploring how I might build a Write Ahead Log. My Use case is for operations like:
- Log Messages (INFO, DEBUG, etc)
- File Uploads
Where I would write the some information representing the Log Message or a File Upload to the WAL, then attempt flush that message to it's final destination. Effectively what I am after is a persistent queue. I figured that a WAL fits here and should be easily implemented without additional dependencies, maybe I'm wrong so feedback is appreciated, as my application should be able to continue / restore in the case of a power failure or some other crash.
My device in this case is a Kiosk, so it will basically be alone in the world by itself just humming along until some bad thing happens: someone accidentally unplugs it for example and it eventually gets powered back on. I figured the WAL fits the scenario for recovery on application start up.
To that end I am looking at System.IO.MemoryMappedFiles.MemoryMappedFile
as a potential implementation solution.
What I have sketched out so far is running 2 mmap's handles.
- the WAL
- an index on the WAL
The index's header would store the current HEAD and the current COMMIT. I'm using COMMIT here to mean "yes, everything up to this point has been dealt with successfully"
Each index point after the HEAD and COMMIT would store the location of WAL data in the WAL file.
so the index would look something like (each index entry representing a 4 byte int):
Head Commit Index 0 Index 1
v v v v
00 00 00 02 00 00 00 01 00 00 00 00 00 00 00 58
Log Entry 0 begins at byte 0 in the WAL file
Log Entry 1 begins at byte 88 in the WAL file
Thus to read an entry from the WAL would be a matter of reading index N to get the start of the range and then reading index N + 1 to get the end (aka the next start).
Behind the scenes I'd have some interval to run Checkpoints and clean up the Index and the WAL. I'm getting ahead of myself there as my main purpose with this question is to get feedback on weather or not this is even a good idea and if so, if using a MemoryMappedFile
for the WAL and the WAL index is the best way to handle it.
In a case like capturing log messages, there could be quite a bit of writing happening in a short about of time.