0
0
DbmsConceptBeginner · 3 min read

What is Recovery in DBMS: Definition and Examples

In a DBMS, recovery is the process of restoring the database to a correct state after a failure like a crash or power loss. It ensures that no data is lost or corrupted by undoing incomplete transactions and redoing committed ones.
⚙️

How It Works

Imagine you are writing a long letter, and suddenly the power goes out. Recovery in a DBMS works like saving drafts automatically so you don't lose your work. When a failure happens, the system looks at saved logs to find what was done and what was not finished.

The DBMS uses these logs to undo any changes from incomplete tasks and redo changes from completed tasks. This way, the database returns to a consistent and reliable state, just like your letter would be saved correctly after power returns.

💻

Example

This simple example shows how a recovery log might record transactions and how recovery uses it to fix the database after a crash.

python
class RecoveryLog:
    def __init__(self):
        self.log = []

    def record(self, transaction_id, action):
        self.log.append((transaction_id, action))

    def recover(self):
        committed = set()
        started = set()
        for tid, action in self.log:
            if action == 'commit':
                committed.add(tid)
            elif action == 'start':
                started.add(tid)

        # Undo transactions that were started but not committed
        for tid in started:
            if tid not in committed:
                print(f"Undoing transaction {tid}")

        # Redo committed transactions
        for tid in committed:
            print(f"Redoing transaction {tid}")

# Simulate logging
log = RecoveryLog()
log.record(1, 'start')
log.record(1, 'commit')
log.record(2, 'start')
# Crash happens before commit of transaction 2

# Recovery process
log.recover()
Output
Undoing transaction 2 Redoing transaction 1
🎯

When to Use

Recovery is used whenever a database faces unexpected failures like power outages, software crashes, or hardware problems. It is essential for systems that require data accuracy and availability, such as banking, online shopping, and airline booking systems.

Without recovery, data could be lost or corrupted, causing serious problems. Recovery ensures users can trust the database to keep their information safe and correct even after failures.

Key Points

  • Recovery restores the database to a consistent state after failures.
  • It uses logs to undo incomplete transactions and redo committed ones.
  • Recovery protects data integrity and prevents loss.
  • It is critical for reliable and trustworthy database systems.

Key Takeaways

Recovery in DBMS restores data after failures to keep it accurate and consistent.
It works by undoing incomplete transactions and redoing committed ones using logs.
Recovery is essential for systems where data loss or corruption is unacceptable.
Without recovery, databases risk losing data during crashes or power failures.