SQLite introduced wal (write-ahead-logging) in 3.7.0, the full name of the Wal is Write Ahead Logging, It is a mechanism for implementing atomic transactions in many databases, and SQLite uses the rollback journal mechanism to implement atomic transactions before introducing the Wal mechanism. The principle of the
Rollback journal mechanism is that before modifying the data in a database file, the data in the modified page is backed up in another place before the modifications are written to the database file If the transaction fails, copy the backup data back, undo the modification, and if the transaction succeeds, delete the backup data and commit the changes. The principle of the
wal mechanism is that the modification is not written directly to the database file, but to another file called the Wal, and if the transaction fails, the record in the Wal is ignored, the modification is revoked, and if the transaction succeeds, It will be written back to the database file at a later time to commit the changes.
Synchronize the behavior of the Wal file and database file is called Checkpoint (checkpoint), it is automatically executed by SQLite, the default is when the Wal file accumulated to 1000 pages of modification; At the appropriate time, you can also manually execute checkpoint,sqlite to provide the relevant interface. After executing the checkpoint, the Wal file will be emptied.
When reading, SQLite will search in the Wal file, find the last write point, remember it, and ignore the write point after this (this ensures that read and write and read can be executed in parallel); It determines whether the page in which the data is to be read is in the Wal file, and if so, reads the data in the Wal file, and if not, reads the data in the database file directly.
When writing, SQLite writes it to the Wal file, but must guarantee exclusive writes, so writes cannot be executed in parallel.
wal uses shared memory technology in the implementation process, so all the read and write processes must be on the same machine, otherwise, data consistency cannot be guaranteed.
WAL (write-ahead log, pre-write) file format (1)
WAL (write-ahead log, pre-write) file format (2)
Write-ahead Transaction Log
SQLite Pre-write log