Course Content
Database Management System (DBMS)
Log-Based Recovery

In any database system, ensuring data consistency and durability after a system crash or power failure is critical. Log-based recovery is one of the most widely used techniques to restore the database to a consistent state using a record of all transactions and actions performed. This process is rooted in the concept of write-ahead logging (WAL), where changes are recorded in a log before being applied to the database.


What is a Log?

A log is a persistent file that stores a history of all the actions carried out by transactions in the system. Each log entry usually contains:

 

  • Transaction ID
  • Type of operation (BEGIN, UPDATE, COMMIT, ABORT)
  • Affected data item(s)
  • Old value and new value (for UPDATE operations)
  • Timestamps or sequence numbers

 

This ensures that every significant action is tracked, even if the main memory is lost due to a crash.


Write-Ahead Logging (WAL)

The core rule of log-based recovery is the Write-Ahead Logging (WAL) protocol, which says:

“The log must be written to stable storage before the actual database is modified.”

This ensures that there is always a history of changes that can be referred to during recovery, even if the database is only partially updated when a crash occurs.


Phases of Log-Based Recovery

Recovery is done in two main phases after a crash:

 

1. Analysis Phase

The system scans the log to identify the list of committed and uncommitted transactions at the time of failure.


2. Redo Phase

The system goes through all operations of committed transactions and re-applies them to ensure all intended changes are preserved in the database.


3. Undo Phase

The system reverses the effects of any uncommitted transactions, restoring the data items they changed back to their old values using the log.


Types of Log Records

  • <START T> – Marks the beginning of transaction T.
  • <T, X, old_value, new_value> – Transaction T changed value of item X.
  • <COMMIT T> – Transaction T successfully completed.
  • <ABORT T> – Transaction T was rolled back.

 

These records enable the database to replay or reverse actions accurately.


Importance of Log-Based Recovery

 

Log-based recovery is vital for maintaining the ACID properties of a transaction system, especially:

  • Atomicity (ensuring partial transactions are undone)
  • Durability (ensuring completed transactions remain committed)

 

This technique provides reliability in high-throughput systems, especially in banking, e-commerce, and real-time transaction platforms.

0% Complete
WhatsApp Icon

Hi Instagram Fam!
Get a FREE Cheat Sheet on System Design.

Hi LinkedIn Fam!
Get a FREE Cheat Sheet on System Design

Loved Our YouTube Videos? Get a FREE Cheat Sheet on System Design.