0
0
DbmsConceptBeginner · 4 min read

Durability in DBMS: Definition, Example, and Use Cases

In a DBMS, durability means that once a transaction is committed, its changes are permanently saved and will survive system failures. This ensures data is not lost even if the system crashes immediately after the transaction.
⚙️

How It Works

Durability in a database system works like a promise that once you save your work, it won't disappear, no matter what happens next. Imagine writing a letter and putting it in a fireproof safe. Even if the house catches fire, your letter stays safe inside. Similarly, when a database commits a transaction, it writes the changes to a stable storage like a hard drive or SSD.

This process usually involves logging the changes in a special file called a transaction log before confirming the transaction is complete. If the system crashes, the database uses this log to recover and restore all committed changes, ensuring nothing is lost.

💻

Example

This example shows a simple simulation of durability using a transaction log file in Python. It writes a transaction to the log before confirming it is saved.

python
import os

class SimpleDB:
    def __init__(self, log_file='transaction.log'):
        self.log_file = log_file
        self.data = {}

    def commit_transaction(self, key, value):
        # Write to log first (simulate durability)
        with open(self.log_file, 'a') as log:
            log.write(f'{key}:{value}\n')
        # Then update data
        self.data[key] = value
        print(f'Transaction committed: {key} = {value}')

    def recover(self):
        if not os.path.exists(self.log_file):
            return
        with open(self.log_file, 'r') as log:
            for line in log:
                key, value = line.strip().split(':', 1)
                self.data[key] = value
        print('Recovery complete. Data:', self.data)

# Usage
simple_db = SimpleDB()
simple_db.commit_transaction('user1', 'Alice')
simple_db.commit_transaction('user2', 'Bob')

# Simulate recovery after crash
recovered_db = SimpleDB()
recovered_db.recover()
Output
Transaction committed: user1 = Alice Transaction committed: user2 = Bob Recovery complete. Data: {'user1': 'Alice', 'user2': 'Bob'}
🎯

When to Use

Durability is essential whenever you need to trust that your data is safe and permanent after saving. For example, banks use durability to ensure money transfers are not lost even if their systems crash. Online shopping carts, booking systems, and any application where data loss would cause problems also rely on durability.

Use durability in systems where data integrity and reliability are critical, especially in financial, healthcare, and business applications.

Key Points

  • Durability guarantees committed data is saved permanently.
  • It protects data against power failures and crashes.
  • Transaction logs help recover data after failures.
  • Durability is part of the ACID properties in databases.

Key Takeaways

Durability ensures data changes survive system crashes after commit.
It uses transaction logs or stable storage to protect data.
Critical for applications where data loss is unacceptable.
Part of the ACID principles that make databases reliable.