π§ DBMS Recovery and Types of Failures
β
What is Recovery?
In DBMS, recovery means bringing the database back to a consistent and correct state
after a failure like a system crash or power failure.
It ensures:
- Atomicity: All steps of a transaction are completed or none.
- Durability: Once a transaction is committed, its changes stay even after a
crash.
Recovery uses techniques like undo (to cancel incomplete changes) and
redo (to reapply completed ones).
𧨠1. Types of Failures β Real-Life Examples
πΈ a. Transaction Failure
One transaction fails but others continue.
Example: While transferring money from A to B, power goes off. Money is deducted
from A but not added to B.
πΈ b. System Crash
The whole system crashes due to power cut or OS failure.
Example: Multiple users are using the database, but system suddenly shuts down. Some
data is lost.
πΈ c. Disk Failure
Database files on the disk are physically damaged.
Example: The disk storing customer records is corrupted and canβt be read.
πWhat is Undo and Redo in DBMS?
Action |
Meaning |
When Used |
UNDO |
Cancel the changes of incomplete transactions |
If a transaction was not committed before the crash |
REDO |
Re-apply the changes of completed transactions |
If a committed transaction's changes were not saved to disk |
π―Simple Example for Undo and Redo
Let's say account A = βΉ1000, B = βΉ500.
Case: Transaction T1
T1 transfers βΉ200 from A to B
<START T1>
<T1, A, 1000, 800>
<T1, B, 500, 700>
<COMMIT T1>
If system crashes after this log is written but before data is saved:
- β
T1 is committed β Apply REDO
- Result after recovery: A = 800, B = 700
Case: Transaction T2
T2 tries to transfer βΉ300 from A to C
<START T2>
<T2, A, 800, 500>
**(Crash here)**
Since T2 is not committed, we use UNDO
πLog-Based Recovery (Detailed)
DBMS stores every change into a log file before applying it to the actual database.
This helps in recovery.
πΈLog Entry Format
<Transaction ID, Data Item, Old Value, New Value>
β
Example:
<T1, A, 100, 200>
β means T1 changed A from 100 to 200
β
Checkpoint in DBMS
πWhat is Checkpoint?
Checkpoint is a safe point saved by DBMS.
It means: "All changes before this are safely saved on disk."
This reduces recovery time after crash.
β
Example with Checkpoint
Let's say we have the following log:
<START T1>
<T1, A, 100, 150>
<COMMIT T1>
<START T2>
<T2, B, 200, 300>
<CHECKPOINT>
<T2, B, 300, 400>
<T2, C, 400, 500>
<START T3>
<T3, A, 150, 180>
<CRASH>
π₯Crash Happens Now. What to do?
β‘οΈ After crash, DBMS checks the log and the last checkpoint.
At checkpoint:
- T1 is committed β β
No need to process
- T2 and T3 are active β DBMS checks them
π§ Recovery Actions:
Transaction |
Status |
Action |
T1 |
Committed before checkpoint |
Nothing to do |
T2 |
Not committed |
β Undo |
T3 |
Not committed |
β Undo |
πͺShadow Paging (Alternative to Logs)
π‘Idea: Work on a duplicate version until done
πΈReal-life Example:
You write an article. Instead of editing the original, you edit a copy. If all
edits are good, you replace the original. If not, discard the copy.
πShadow Paging in DBMS
- Shadow Page Table: Original data (never touched)
- Current Page Table: Used during transaction
- If commit: Copy current to shadow
- If crash: Discard current and use shadow
πΈExample:
Let's say:
- A = βΉ100
- Transaction changes A to βΉ200
3
Crash before commit β Discard current
4
Final value = 100 β
(safe)
πRecovery Phases in DBMS
After crash, recovery goes in these steps:
1
Analysis Phase
β Find which transactions were active during crash.
2
Redo Phase
β Reapply changes of committed transactions.
3
Undo Phase
β Rollback changes of uncommitted transactions.
π§ΎFULL FINAL EXAMPLE: COMBINING ALL
Situation: Three transactions
Initial Values:
Log File:
<START T1>
<T1, A, 100, 150>
<T1, B, 200, 250>
<COMMIT T1>
<START T2>
<T2, C, 300, 350>
<CHECKPOINT>
<T2, C, 350, 400>
<START T3>
<T3, A, 150, 180>
<CRASH>
Now Recovery Happens
Transaction |
Status |
Action |
T1 |
Committed |
β
REDO |
T2 |
Not committed |
β UNDO |
T3 |
Not committed |
β UNDO |
Final Values after Recovery:
- A = 150 (from T1 REDO)
- B = 250 (from T1 REDO)
- C = 300 (T2 undone)
β
Safe, consistent data.
πSummary
πUndo
Meaning: Cancel uncommitted changes
Use: Recover from crash
πRedo
Meaning: Reapply committed changes
Use: Ensure durability
β
Checkpoint
Meaning: Safe save point
Use: Faster recovery
πͺShadow Paging
Meaning: Copy-modify method
Use: Simple recovery, no logs
πLog File
Meaning: History of all actions
Use: Used for recovery
πRecovery Phases
Meaning: Analyze β Redo β Undo
Use: Full process