Database Fundamentals

0% completed

Previous
Next
Durability

What is Durability?

Durability, one of the ACID properties, ensures that once a transaction is successfully committed, its changes are permanently stored in the database. Even in the event of system crashes, power failures, or hardware malfunctions, the database guarantees that committed data is not lost.

Once saved, your data is safe and will not be lost after failures.

Image

Importance of Durability

  1. Data Reliability:

    • Applications depend on durability to ensure that important information is not lost after a transaction is completed.
    • Example: A bank transaction deducting $100 from an account must remain recorded even if the system crashes immediately afterward.
  2. Fault Tolerance:

    • Durability ensures that a database can recover to a consistent state after failures, with all committed transactions intact.
  3. User Trust:

    • Durability builds user trust by providing confidence that their data is safe and persistent.

How Durability is Achieved

Modern databases use a combination of techniques to ensure durability:

1. Write-Ahead Logging (WAL)

  • Before applying changes to the database, a log of the changes is written to a persistent storage medium (like a disk).
  • In case of a failure, the log is replayed to recover the committed transactions.

Example:

  • Transaction T1 commits, and its changes are logged.
  • System crashes before changes are written to the database.
  • On recovery, the log is replayed to reapply T1's changes, ensuring durability.

2. Checkpointing

  • Periodically, the database takes a "snapshot" of its current state and saves it to disk.
  • Checkpoints reduce recovery time by limiting the number of logs that need to be replayed during recovery.

3. Synchronous Disk Writes

  • Changes are written to disk immediately upon committing a transaction.
  • Ensures that the database doesn't rely on volatile memory (e.g., RAM) to store committed data.

4. Replication

  • Changes are replicated to multiple nodes or servers to ensure data redundancy.
  • Even if one node fails, other nodes maintain a consistent copy of the data.

Example:

  • A distributed database like MongoDB replicates changes across its replica set.
  • A failure in the primary node doesn’t affect data availability because replicas retain the committed data.

Examples of Durability in Action

Scenario 1: Bank Transaction

  1. User transfers $1,000 from Account A to Account B.
  2. Transaction commits, and the changes are written to the transaction log.
  3. A power failure occurs immediately after the commit.
  4. On recovery, the database uses the log to ensure that the $1,000 transfer is preserved.

Scenario 2: E-commerce Order

  1. A user places an order for a product.
  2. The transaction commits, and the database saves the order details and payment information.
  3. A system crash occurs right after the commit.
  4. The user’s order is still recorded because the database ensures durability.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next