Persistence - mark222/SSDir GitHub Wiki
Persistence
The directory data is persisted to disk in a single file stored in JSON format. JSON was chosen as the persistence format for a number of reasons:
- JSON is more compact and generally readable than XML
- Java object serialization results in a binary file that is not human readable or easily edited
- The client protocol also uses JSON formatted data so there was synergy with the client strategy
Optionally the JSON file can be encrypted (writeEncrypted
config property) so that the directory data is secure even if an intruder gains access to the server disk files. The JSON file is read and written to/from POJOs using the Jackson library. The directory data is loaded when the server starts and it is written only if an update is made (either by running the PowerChurch synchronization, or by a user making an update to their profile).
When a server thread has made an update to the directory, it is not immediately written to disk on that thread. Instead, a "dirty" flag is set TRUE when the update is complete. An auto-save thread examines the directory periodically and writes it to disk if the "dirty" flag is set (keeping it locked against additional updates while the write is done). This is done by the AutoSaveThread
class that is started on an independent thread in the server during server startup (ServletEventListener
class). The auto-save timing interval is configurable (autoSaveIntervalMin
config property) and defaults to 5 minutes.
In theory a change could be lost if the server were to crash in the (average) 2.5 minutes between when an update is made and the file is physically written to disk. Given the infrequent nature of user updates to their profile data, and the high reliability of cloud-based compute servers, the probability of actual data loss is very small. A synchronization update from PowerChurch does not wait for the auto-save thread, those changes are written to disk immediately (CommitPCUpdates
class).