In a Database Management System (DBMS), data integrity and consistency are the most important aspects. Databases are widely used in banking, airlines, e-commerce, and other critical applications where even a small data loss can lead to huge problems. But what happens when a system crashes, a power failure occurs, or an unexpected error corrupts the data?
This is where the Recovery System in DBMS plays a vital role.
Introduction to Recovery System
A Recovery System in DBMS is responsible for restoring the database to a correct, consistent, and reliable state after a failure. The main goal of the recovery system is to make sure that:
- No committed transaction is lost.
- No uncommitted transaction is left in the database.
This ensures that the ACID properties (Atomicity, Consistency, Isolation, and Durability) of transactions are preserved.
Types of Failures in DBMS
To understand the recovery system, let’s first look at the common types of failures that can occur in a DBMS:
- Transaction Failure
- A transaction may fail due to logical errors (e.g., invalid data input) or system errors (e.g., division by zero).
- System Crash
- Power outages, hardware malfunctions, or operating system crashes may halt the database unexpectedly.
- Disk Failure
- Problems like disk head crashes or data corruption may lead to permanent data loss.
- Disk Update Failure
- Errors during writing or transferring data between memory and disk.
Each of these failures can damage the database, making a recovery mechanism necessary.
Need for Recovery System
Without recovery, databases may:
- Contain partial transactions, leading to inconsistent states.
- Lose important data permanently.
- Fail to provide reliability for business operations.
Thus, the recovery system ensures that the database always moves from one consistent state to another, even if a failure happens.
Recovery Techniques in DBMS
There are several methods that DBMS uses to recover from failures. The main techniques include:
1. Backup
- Keeping a copy of the database at regular intervals.
- In case of a crash, the latest backup can be used to restore data.
2. Logging
- A log file is maintained where all transactions and their changes are recorded before execution.
- If a failure occurs, the log helps in redoing or undoing transactions.
Types of logging:
- Redo: Reapply operations of committed transactions.
- Undo: Rollback operations of uncommitted transactions.
3. Checkpointing
- A checkpoint is a snapshot of the database state at a specific point in time.
- When recovery is needed, DBMS starts from the latest checkpoint instead of scanning the entire log, saving time.
4. Shadow Paging
- Instead of updating the actual data page, DBMS keeps a shadow copy.
- If a transaction commits, the shadow page is replaced with the actual page.
- If a crash occurs, the shadow page remains intact, ensuring consistency.
Recovery Algorithms
Recovery algorithms decide how transactions should be undone or redone after a failure.
- Deferred Update (No-Undo/Redo)
- Changes are applied only after a transaction commits.
- If a crash happens before commit, no changes are made.
- Immediate Update (Undo/Redo)
- Changes are applied immediately, but logs are kept.
- If a crash occurs, DBMS uses logs to undo uncommitted transactions and redo committed ones.
- Shadow Paging (No-Undo/No-Redo)
- No need for undo or redo because shadow copies ensure consistent recovery.
Example of Recovery
Suppose we have a banking transaction:
T1: Transfer 500 from Account A to Account B
Steps:
1. Read(A), A = A - 500, Write(A)
2. Read(B), B = B + 500, Write(B)
3. Commit
- If a system crashes before Commit, the recovery system undoes all changes.
- If it crashes after Commit, the recovery system redoes the changes to ensure durability.
This ensures data integrity even in case of failure.
Advantages of Recovery System
- Ensures data consistency after a crash.
- Protects against data loss.
- Preserves ACID properties of transactions.
- Provides reliability and trust for applications like banking, e-commerce, and airlines.
Conclusion
The Recovery System in DBMS is essential to handle unexpected failures and maintain the correctness of the database. By using techniques like logging, checkpoints, and shadow paging, the system ensures that committed transactions are never lost and uncommitted ones are rolled back.
In simple words, the recovery system acts as a safety net that guarantees reliability, even in the face of errors, failures, or crashed.