DEV Community

YAZHINI M 24CB063
YAZHINI M 24CB063

Posted on

The Unbreakable Contract: Mastering Transactions, Deadlocks, and Database Recovery

Ever wondered what magic keeps your application from corrupting data during crashes or heavy concurrent use? It’s not magic—it's the power of Database Transactions.

Transactions are the foundation of reliable data systems. Today, we'll dive into three critical aspects using practical SQL demos: Atomicity (Rollback), Deadlock Simulation, and the principle of Log-Based Recovery.

Setup: The Foundation
We'll use a simple Accounts table across all demos.

This is a format for a dev.to post covering Transactions, Deadlocks, and Log-Based Recovery, designed to be technical, explanatory, and engaging for a developer audience.

The Unbreakable Contract: Mastering Transactions, Deadlocks, and Database Recovery
Ever wondered what magic keeps your application from corrupting data during crashes or heavy concurrent use? It’s not magic—it's the power of Database Transactions.

Transactions are the foundation of reliable data systems. Today, we'll dive into three critical aspects using practical SQL demos: Atomicity (Rollback), Deadlock Simulation, and the principle of Log-Based Recovery.

Setup: The Foundation
We'll use a simple Accounts table across all demos.

SQL

CREATE TABLE Accounts (
acc_no INT PRIMARY KEY,
name VARCHAR(50),
balance INT
);
INSERT INTO Accounts VALUES
(1, 'Alice', 1000),
(2, 'Bob', 1500),
(3, 'Charlie', 2000);
Screenshot 1: Accounts table data

  1. Atomicity: The All-or-Nothing Guarantee Atomicity ensures a transaction is treated as a single, indivisible unit. If a transfer fails after the debit but before the credit, the whole thing must be undone.

Demo: Rollback a Partial Transfer
We start a transaction to move $500 from Alice to Bob, but we intentionally roll it back before crediting Bob.

Key Takeaway: The ROLLBACK command utilized the Undo Log to revert the partial change, preserving the database's integrity.


  1. Deadlocks: The Standoff Simulation A Deadlock is a classic concurrency problem where Transaction A waits for a resource held by Transaction B, and Transaction B waits for a resource held by Transaction A. The database must intervene and choose a victim to roll back.

To simulate, you need two separate database sessions running the following code concurrently. We use SELECT... FOR UPDATE to explicitly acquire row-level locks.

Session 1 (Txn A)
START TRANSACTION;
SELECT * FROM Accounts WHERE acc_no = 1 FOR UPDATE; -- Locks Alice
-- Now, try to lock Bob:
SELECT * FROM Accounts WHERE acc_no = 2 FOR UPDATE;
-- Waits...

Session 2 (Txn B)
START TRANSACTION;
SELECT * FROM Accounts WHERE acc_no = 2 FOR UPDATE; -- Locks Bob
-- Now, try to lock Alice:
SELECT * FROM Accounts WHERE acc_no = 1 FOR UPDATE;
-- Deadlock!
Observation: One session (the victim) will fail with a Deadlock Error, and its transaction will be automatically rolled back, allowing the other session (the survivor) to proceed.

  1. Log-Based Recovery: The Safety Net Every reliable database uses a Write-Ahead Log (WAL) or Transaction Log to record all changes before they are applied to the main data files. This log is the key to both Atomicity (Undo) and Durability (Redo).

When you execute a ROLLBACK, the database doesn't just "forget" the change; it actively scans the log for the transaction's entries and applies UNDO operations to reverse the changes.

Key Takeaway: While we can't easily read the binary log file in a simple SQL client, the successful rollback proves that the database used the log to achieve transactional integrity. In a crash recovery scenario, this same log would be used to REDO all committed changes.

Conclusion
Understanding the mechanisms behind transactions—from the guarantees of Atomicity to the resolution of Deadlocks and the critical role of Log-Based Recovery—is essential for any developer interacting with a production database.

These features aren't just for DBAs; they directly impact how you structure your application logic to handle concurrent users and potential system failures.

Huge thanks to @santhoshnc sir for being the navigation system that guided me through this project's toughest corners.

Top comments (0)