πŸ”„ DBMS Recovery Systems

Undo, Redo, Checkpoint & Shadow Paging

🧠 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

  • Revert A back to β‚Ή800

πŸ“˜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

βœ… Final State:

  • A = 150
  • B = 200
  • C = 400

πŸͺžShadow Paging (Alternative to Logs)

πŸ’‘Idea: Work on a duplicate version until done

Shadow Paging Diagram

πŸ”Έ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
1
Shadow Table: A = 100
2
Current Table: A = 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:

  • A = 100
  • B = 200
  • C = 300

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