š§±Modern databases are built to stay consistent, reliable, and resilient ā even when systems crash or multiple users access data at once.
Three major mechanisms that make this possible are Transactions, Deadlocks, and Log-Based Recovery.
In this guide, weāll explore how each of these works in practice using a simple DB_Assignment table as an example.
š§± Step 1: Set Up the DB_Assignment Table
- Weāll start by creating a basic table that mimics a real banking environment where multiple accounts interact.
- Once inserted, verify your data using a simple SELECT * FROM DB_Assignment; query.
ā
Output:
This table will serve as the foundation for testing transactions and recovery concepts.
š¹ 1ļøā£Transaction ā Atomicity & Rollback
- A transaction is a collection of SQL commands treated as a single logical operation.
- It upholds the ACID principles ā Atomicity, Consistency, Isolation, and Durability.
- Here, weāll highlight Atomicity, which guarantees that either all steps in a transaction are applied or none are ā thereās no halfway point.
- šø Example: Money Transfer
- Suppose Alice transfers ā¹500 to Bob.
- Weāll begin the transfer but then roll it back before committing to observe atomicity.
START TRANSACTION;
UPDATE DB_Assignment SET balance = balance – 500 WHERE acc_no = 1;
UPDATE DB_Assignment SET balance = balance + 500 WHERE acc_no = 2;
ROLLBACK;
SELECT * FROM DB_Assignment;
ā
Result:
After rollback, both account balances stay the same ā confirming that no partial update occurred.
š” Why Itās Important:
If a power outage or software failure happens midway, the rollback mechanism prevents incomplete changes from corrupting the database.
š¹ 2ļøā£Deadlock Simulation:
- A deadlock happens when two transactions each hold a lock that the other needs, causing both to wait forever.
- This issue often surfaces when multiple users access and modify shared data simultaneously.
š¼ Real-World Example:
Think of two cashiers trying to update two linked bank accounts ā one has locked Account A and the other Account B.
Each is waiting for the other to release their lock ā thatās a deadlock.
— Session 1
BEGIN;
UPDATE DB_Assignment SET balance = balance – 500 WHERE acc_no = 1; — Locks Alice
UPDATE DB_Assignment SET balance = balance + 500 WHERE acc_no = 2; — Waiting on Bob
— Session 2
BEGIN;
UPDATE DB_Assignment SET balance = balance – 300 WHERE acc_no = 2; — Locks Bob
UPDATE DB_Assignment SET balance = balance + 300 WHERE acc_no = 1; — Waiting on Alice
ā³ Both transactions are now stuck waiting ā forming a deadlock.
š§ How Databases Handle It:
- Modern DBMS (like MySQL, PostgreSQL, Oracle) automatically detect deadlocks and abort one transaction, allowing the other to complete successfully.
- This maintains database consistency without manual intervention.
š”Tips to Prevent Deadlocks:
- Always access tables and rows in a consistent order.
- Keep transactions short and efficient.
- Avoid unnecessary or long-held locks.
š¹ 3ļøā£Log-Based Recovery:
- Even with transactions and deadlock control, unexpected failures can still occur.
- This is where log-based recovery ensures the Durability part of ACID ā guaranteeing that committed data survives system crashes.
āļø What It Does:
- Databases maintain transaction logs ā for instance, Binary Logs in MySQL or Write-Ahead Logs (WAL) in PostgreSQL ā that track every change made to the database.
- If a crash happens, the DBMS replays the log to redo committed transactions and undo incomplete ones.
START TRANSACTION;
UPDATE DB_Assignment SET balance = balance + 100 WHERE acc_no = 3;
ROLLBACK;
ā
Result:
The rollback entry gets recorded in the log.
If the system crashes, the recovery process reads this log and restores the database to the last consistent state.
š”Why It Matters:
Without logging, recovering from crashes or incomplete updates would be nearly impossible.
Thatās why mission-critical systems like banking and e-commerce rely heavily on log-based recovery.
š” Final Thoughts
Database reliability isnāt just about storing data ā itās about maintaining integrity during every possible failure or concurrency issue.
Transactions ensure atomic and consistent operations.
Deadlocks teach us how to manage concurrent processes safely.
Log-Based Recovery guarantees data durability even after unexpected crashes.
By practicing these concepts hands-on, youāll understand how professional-grade databases achieve high fault tolerance and data safety in real-world scenarios. ā”