Database Management SystemUnit 109 min read
Database Recovery Techniques: Failures, Log-Based Recovery, and Checkpoints
Unit 10 of Database Management System: This unit covers the fundamental concepts of database recovery, including failure classification, storage types, log-based recovery techniques like deferred and immediate updates, the importance of checkpoints, and shadow paging to ensure atomicity and durability.
Key points
- Understand the types of failures and how they affect database consistency.
- Learn the role of the transaction log and Write-Ahead Logging (WAL) in recovery.
- Distinguish between deferred update (No-Undo/Redo) and immediate update (Undo/Redo) protocols.
- Master the checkpointing mechanism to reduce recovery time after a system crash.
- Explore shadow paging as an alternative recovery technique to log-based methods.
Introduction to Database Recovery
Database recovery is the process of restoring the database to a correct and consistent state after a failure has occurred. In a multi-user environment, transactions are executed concurrently, and failures can lead to partial updates, which violate the ACID properties—specifically Atomicity (all or nothing) and Durability (once committed, changes are permanent).
Recovery techniques are designed to ensure that even if the system crashes, the database can recover to the last consistent state.
Failure Classification
To understand recovery, we must first classify the types of failures that can occur:
- Transaction Failure:
- Logical Error: The transaction cannot complete due to some internal condition (e.g., insufficient balance, division by zero).
- System Error: The DBMS terminates an active transaction because of an error condition (e.g., deadlock) or because the user aborted it.
- System Crash:
- Hardware or software failure causes the system to stop.
- The contents of the main memory (volatile storage) are lost, but the data on the disk (non-volatile storage) remains intact.
- Disk Failure (Media Failure):
- A physical failure of the disk (e.g., head crash) results in the loss of data stored on the disk. This is the most severe type of failure and requires backups for recovery.
Storage Structure
Recovery depends on how data is stored across different media:
- Volatile Storage: Fast, but data is lost during power failure or system crash (e.g., RAM, Cache).
- Non-volatile Storage: Slower, but survives system crashes (e.g., Hard Disks, SSDs).
- Stable Storage: A theoretical storage that survives all types of failures, including disk crashes. It is implemented using techniques like Disk Mirroring or RAID, where data is replicated across multiple non-volatile devices.
Recovery Concepts and the Log
The most widely used recovery mechanism is the Transaction Log (or Journal). The log is a sequence of records kept on stable storage that tracks all updates made to the database.
Log Record Structure
A typical log record contains:
<T, Start>: Transaction has started.<T, X, V1, V2>: Transaction changed data item from old value to new value .<T, Commit>: Transaction has successfully completed.<T, Abort>: Transaction has been rolled back.
Write-Ahead Logging (WAL)
The WAL protocol states that:
- Before a data item in the database is modified on disk, the corresponding log record (containing the old value) must be written to stable storage.
- A transaction is not considered "committed" until its commit log record is written to stable storage.
Log-Based Recovery Techniques
There are two primary approaches to log-based recovery: Deferred Update and Immediate Update.
1. Deferred Update (No-Undo/Redo)
In this technique, the database is not updated on the disk until the transaction reaches its commit point. All updates are recorded in the log and kept in local buffers.
- During Execution: Only the log is updated; the actual database remains unchanged.
- At Commit: The log records are written to stable storage, and then the database is updated.
- Recovery Procedure:
- If a transaction has a
<T, Start>and a<T, Commit>record, it is Redone. - If a transaction has a
<T, Start>but no<T, Commit>record, nothing needs to be done (No-Undo) because the disk was never updated.
- If a transaction has a
2. Immediate Update (Undo/Redo)
In this technique, the database can be updated on the disk even before the transaction commits. However, the WAL protocol must be followed.
- During Execution: Updates are written to the log and may be written to the disk.
- Recovery Procedure:
- If a transaction has a
<T, Start>and a<T, Commit>record, it is Redone (to ensure durability). - If a transaction has a
<T, Start>but no<T, Commit>record, it must be Undone (to ensure atomicity). The old values from the log are used to restore the data.
- If a transaction has a
Comparison Table: Deferred vs. Immediate Update
| Feature | Deferred Update | Immediate Update |
|---|---|---|
| When disk is updated | Only after Commit | During execution (before Commit) |
| Recovery Operations | REDO only | UNDO and REDO |
| Buffer Requirement | High (must store all changes) | Moderate |
| Complexity | Simpler recovery | More complex recovery |
| Atomicity | Guaranteed by not writing | Guaranteed by UNDOing |
Checkpoints
In a real-world system, the log file grows indefinitely. During recovery, scanning the entire log is time-consuming. To optimize this, the DBMS uses Checkpoints.
A checkpoint is a point in time where the DBMS ensures that all log records and modified buffer blocks are physically written to the disk.
Recovery with Checkpoints:
- Find the last
[checkpoint]record in the log. - Only transactions that were active at the time of the checkpoint or started after the checkpoint need to be processed.
- Transactions that committed before the checkpoint are already safely on the disk and do not need to be redone.
Shadow Paging
Shadow paging is an alternative to log-based recovery. It considers the database to be made up of fixed-size logical units called pages.
How it works:
- The system maintains two page tables: the Current Page Table and the Shadow Page Table.
- When a transaction starts, both tables are identical.
- When a page is modified, a new page is created on the disk. The Current Page Table is updated to point to this new page, while the Shadow Page Table continues to point to the original (old) page.
- Commit: The Current Page Table is saved to disk, becoming the new Shadow Page Table.
- Recovery: If the system crashes, the Current Page Table in volatile memory is lost. The system simply reloads the Shadow Page Table from the disk. Since the shadow table points to the state before the transaction started, the database is automatically in a consistent state.
Shadow Paging Diagram:
[Page Table] [Disk Pages]
+-------+-------+ +-----------+
| Index | Addr | ------> | Page 0 |
+-------+-------+ +-----------+
| Index | Addr | ------> | Page 1 | (Original)
+-------+-------+ +-----------+
| Page 1' | (Modified Copy)
+-----------+
* Current Table points to Page 1'
* Shadow Table points to Page 1
Advantages:
- No overhead of maintaining log files.
- Recovery is very fast (no Undo/Redo).
Disadvantages:
- Data Fragmentation: Pages become scattered on the disk.
- Garbage Collection: Old pages need to be cleaned up.
- Commit Overhead: Writing the entire page table can be expensive for large databases.
Worked Example: Log-Based Recovery (Immediate Update)
Consider the following log at the time of a system crash:
<T1, Start><T1, A, 100, 200><T2, Start><T1, Commit><T2, B, 500, 600><T3, Start><T3, C, 10, 20>--- CRASH ---
Recovery Steps:
- Identify Transactions:
- : Started and Committed.
- : Started but not Committed.
- : Started but not Committed.
- Undo Phase:
- Undo : Restore to 10.
- Undo : Restore to 500.
- Redo Phase:
- Redo : Set to 200.
Final State: .
ARIES Recovery Algorithm
ARIES (Algorithm for Recovery and Isolation Exploiting Semantics) is a sophisticated recovery algorithm used in many modern DBMSs (like IBM DB2 and SQL Server). It operates in three phases:
- Analysis Phase: Identifies which transactions were active at the time of the crash and which pages in the buffer were "dirty" (modified but not written to disk).
- Redo Phase: Repeats all history found in the log to bring the database to the state it was in at the moment of the crash.
- Undo Phase: Rolls back all transactions that were active at the time of the crash in reverse chronological order.
Advantages and Disadvantages of Recovery Techniques
| Technique | Advantages | Disadvantages |
|---|---|---|
| Log-Based | Supports high concurrency; sequential I/O for logs is fast. | Recovery can be slow if the log is long; requires WAL. |
| Deferred Update | No need to Undo; simple recovery. | Cannot handle long transactions easily; high memory usage. |
| Immediate Update | Supports long transactions; less memory pressure. | Requires both Undo and Redo; complex logic. |
| Shadow Paging | Instant recovery; no log overhead. | Causes disk fragmentation; difficult to implement with concurrency. |
Exam Tip
In TU/PU exams, this unit is frequently tested through the following:
- ACID Properties: Always mention how recovery ensures Atomicity and Durability.
- Deferred vs. Immediate Update: This is a favorite "Differentiate between" question. Use a table and explain the Undo/Redo logic clearly.
- Numerical/Trace Problems: You might be given a log sequence (like the example above) and asked to identify which transactions are Undone and which are Redone.
- Checkpoints: Explain how checkpoints reduce the work of the recovery manager.
- Shadow Paging: Often asked as a short note. Mention the "Current" and "Shadow" page tables.
When answering "Explain any one recovery technique," choose Log-based recovery with Immediate Update as it allows you to demonstrate your knowledge of both Undo and Redo operations, which usually earns higher marks.
Based on the TU BSc CSIT syllabus for Database Management System (CSC265), unit 10.
Discussion
Loading…