0
0
DbmsConceptBeginner · 3 min read

Undo Recovery in DBMS: What It Is and How It Works

In a database management system, undo recovery is the process of reversing changes made by incomplete or failed transactions to restore the database to a consistent state. It uses undo logs to track and rollback these changes automatically after a crash or error.
⚙️

How It Works

Undo recovery works like a safety net for databases. Imagine you are writing a document and accidentally delete a paragraph. Undo recovery is like the undo button that helps you go back to the previous version before the mistake.

In databases, when a transaction (a set of operations) starts, the system records the original data before any changes in a special place called the undo log. If the transaction fails or the system crashes before it finishes, the database uses this log to reverse the partial changes, ensuring no incorrect or incomplete data remains.

This process keeps the database reliable and consistent, so users always see correct data even after unexpected failures.

💻

Example

This example shows a simple simulation of undo recovery using Python to illustrate the concept.

python
class Database:
    def __init__(self):
        self.data = {'balance': 100}
        self.undo_log = []

    def start_transaction(self):
        # Save current state for undo
        self.undo_log.append(self.data.copy())

    def update_balance(self, amount):
        self.data['balance'] += amount

    def rollback(self):
        # Undo last change
        if self.undo_log:
            self.data = self.undo_log.pop()

# Simulate transaction
db = Database()
db.start_transaction()
db.update_balance(-50)  # Withdraw 50
print('Before rollback:', db.data)

# Something goes wrong, rollback
db.rollback()
print('After rollback:', db.data)
Output
Before rollback: {'balance': 50} After rollback: {'balance': 100}
🎯

When to Use

Undo recovery is essential whenever a database must maintain accuracy despite errors or crashes. It is used:

  • During system crashes to revert incomplete transactions.
  • When a transaction encounters an error and must be canceled.
  • To maintain data consistency in multi-user environments where many transactions happen simultaneously.

For example, in banking systems, undo recovery ensures that if a money transfer fails halfway, the account balances do not get corrupted.

Key Points

  • Undo recovery reverses changes from incomplete or failed transactions.
  • It relies on undo logs that store original data before changes.
  • Ensures database consistency and reliability after crashes or errors.
  • Commonly used in transactional systems like banking and e-commerce.

Key Takeaways

Undo recovery restores the database to a consistent state by reversing incomplete transactions.
It uses undo logs to track original data before changes.
Undo recovery is critical after crashes or transaction failures.
It helps maintain data accuracy in multi-user and transactional environments.