6. Thinking outside the box - Write Ahead Logging
Write ahead logging is normally used by the operating system and the databases to guarantee durability and atomicity.
Write-ahead logging ensures that updates are reflected in the log before writing to the actual database. As soon as it can be confirmed that the record has been written to the log, the database considers it safe to tell the user that the record has been committed even if the updates haven’t really reached the database.
But here is a strange question. If we can afford to write updates to the log, why is it any different from updating the database itself?
There are two primary reasons for doing this:
-
Adding something to the log is faster than adding an entry to the database. This is because writing to a log involves sequential writes whereas writing to the database requires random writes.
-
In case the system crashes before a transaction is completed, the actual database remains unaffected. The recovery protocol just goes through the log records and decides whether a transaction should be aborted or committed.
All major systems use WAL. Based on the recovery protocol, these logs may also contain redo and undo logs.