Database Management System (DBMS) / Implementation of atomicity and durability

Log based Recovery Methods

πŸ““ What Is a Log?

A log is a sequence of records stored on a stable storage (disk) that keeps track of:

  • Transaction start

  • Data items modified

  • Old and new values

  • Transaction commit or abort

Log-Based Recovery Methods are essential techniques in DBMS for maintaining Atomicity and Durabilityβ€”two key ACID properties of transactions. These methods use a log file to track all the changes made by transactions, ensuring that the database can recover from crashes or failures.


🧾 Format of Log Records:


πŸ”„ Two Main Log-Based Recovery Methods:

1. Deferred Update (Deferred Write)

βœ… Updates are made to the database only after the transaction commits.

❌ If the transaction fails or the system crashes before commit, no change is made to the DB.

πŸ”§ Steps:

  1. On START, log the event: <START T1>

  2. On update, log only: <T1, X, old, new>

  3. On COMMIT, apply the updates to DB and write <COMMIT T1>

πŸ” Recovery:

  • Redo transactions that have <COMMIT>

  • Ignore transactions without <COMMIT>

βœ… Pros:

  • Simple recovery β€” no need to undo

  • Ideal for systems that commit infrequently

❌ Cons:

  • Delayed performance due to postponed writes


2. Immediate Update

βœ… Updates are made to the database before the transaction commits.

❌ So if a crash occurs, we may need to UNDO the changes.

πŸ”§ Steps:

  1. On START, log: <START T1>

  2. On update, log: <T1, X, old, new> and apply change to DB

  3. On COMMIT, log: <COMMIT T1>

πŸ” Recovery:

  • UNDO uncommitted transactions (using old values)

  • REDO committed transactions (using new values)

βœ… Pros:

  • Faster writes

  • Suitable for real-time updates

❌ Cons:

  • Complex recovery β€” need both UNDO and REDO operations



🧠 Summary Table:

Feature

Deferred Update

Immediate Update

When DB is updated

After COMMIT

Before COMMIT

Log Required

REDO only

UNDO + REDO

Recovery Complexity

Simple

Complex

Speed

Slower

Faster

Failure Before Commit

No effect (nothing written)

Must UNDO


🧰 Types of Recovery Operations

Operation

Purpose

UNDO

Reverse changes made by uncommitted transactions

REDO

Reapply changes made by committed transactions


πŸ’‘ Bonus Tip: Write-Ahead Logging (WAL)

Before applying any changes to the database, the log must be written to disk first. This ensures that:

  • If a failure occurs, the log is there to recover from.

  • Maintains data integrity.