Last modified: July 29, 2020
This article is written in: 🇺🇸
Durability is a fundamental principle in database systems that ensures once a transaction has been committed, its effects are permanent and will survive any subsequent system failures. This means that the data changes made by a transaction are safely stored and can be recovered even if the system crashes or experiences a power loss immediately afterward.
Imagine that every time you save a file on your computer, you expect it to be there the next time you turn it on—even if there was an unexpected shutdown. Similarly, durability guarantees that committed transactions in a database are preserved, providing reliability and trust in the system.
Once a transaction is committed, its changes are permanently recorded, even in the event of a system failure or crash:
+--------------------------+
| Transaction Successfully |
| Committed |
| (Changes Finalized) |
+------------+-------------+
|
v
+--------------------------+
| Write-Ahead Log (WAL) |
| (Persistent Log Entry) |
+------------+-------------+
|
v
+--------------------------+
| Persistent Storage |
| (Disk / SSD) |
| (Data Remains Intact) |
+--------------------------+
After reading the material, you should be able to answer the following questions:
Durability plays a crucial role in maintaining the integrity and reliability of a database. By ensuring that committed transactions are not lost, it provides confidence that the data remains consistent and accurate over time.
Once a transaction is committed, durability guarantees that its changes are permanently recorded. This means that even in the face of hardware failures or system crashes, the data modifications are not lost and can be retrieved upon system recovery.
In the event of a system failure, durability allows the database to recover to a consistent state by reapplying or confirming the committed transactions. This ensures that the database does not revert to an earlier state, preventing data loss and maintaining continuity.
To better understand how durability impacts everyday applications, let's explore some scenarios where this property is essential.
Consider an e-commerce platform where customers place orders and the system updates inventory levels accordingly.
Imagine a banking system where funds are transferred between accounts.
Databases implement several mechanisms to guarantee that committed transactions remain durable, even in the face of unexpected failures.
Write-Ahead Logging is a method where changes are first recorded in a log before being applied to the database itself.
Checkpointing involves periodically saving the current state of the database to stable storage.
Replication involves maintaining copies of the database on multiple servers or storage systems.
Alright, here’s the upgraded and clarified version of “Visualizing Durability Mechanisms”, with added detail, clearer structure, real-world analogies, and concrete SQL/logging output. Tone stays direct and to-the-point, like a friend walking you through what’s actually happening under the hood.
Durability guarantees that once a transaction is committed, its results are permanent—even if the system crashes seconds later. If the database says, “Done,” it better mean it.
Let’s look at how that works behind the scenes:
[Start Transaction]
|
[Write Changes to Log]
|
[Apply Changes to Database]
|
[Commit Transaction]
|
[Durability Ensured]
Each step exists to protect your data from disappearing into the void. Here's how it plays out:
I. Start Transaction
At this point, nothing’s permanent. You’re just signaling that some changes are about to happen.
BEGIN;
II. Write-Ahead Logging (WAL)
Before the actual data is changed, all actions are recorded in a transaction log. This is critical. The log is stored on disk immediately.
LOG: UPDATE accounts SET balance = balance - 100 WHERE id = 1
LOG: UPDATE accounts SET balance = balance + 100 WHERE id = 2
If the system crashes after this point but before applying changes to the actual data, the recovery system will use the log to redo the transaction.
📝 Why this matters: Logging comes before any changes are made. That’s why it’s called Write-Ahead Logging (WAL).
III. Apply Changes to the Database
Now the actual tables are updated.
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
These changes happen in memory first. They’ll be flushed to disk shortly, but not necessarily immediately.
IV. Commit Transaction
This is the point of no return.
COMMIT;
The system writes a special commit record to the log. If that commit log entry exists, then the transaction is considered durable.
Imagine the system crashes right after the commit. What happens on recovery?
Even if the data changes weren’t fully flushed to disk, the log was, and that’s enough to recover.
Think of this like editing a document: - You make changes. - You hit Ctrl+S (which writes to the disk). - Then you close the app.
Even if your laptop dies after closing, that save ensures your edits aren't lost. That’s durability.