๐พ Understanding Transactions, Deadlocks & Log-Based Recovery in DBMS
Databases are the backbone of modern applications โ from banking and e-commerce to social networks. For a system to be reliable, it must handle multiple users, ensure data integrity, and recover gracefully from failures.
In this post, weโll explore three key concepts that make this possible: Transactions, Deadlocks, and Log-Based Recovery.
A transaction in a Database Management System (DBMS) is a sequence of operations performed as a single logical unit of work.
The transaction must follow the ACID properties โ Atomicity, Consistency, Isolation, and Durability.
Either all operations of a transaction are successfully executed, or none of them take effect.
Letโs take a simple bank transfer:
Debit โน1000 from Account A
Credit โน1000 to Account B
If either step fails, the transaction should rollback to its previous state โ ensuring data accuracy.
โ๏ธ Concurrency & Deadlocks
In multi-user environments, multiple transactions may access the same data simultaneously. To maintain correctness, databases use locks to control access.
But sometimes, this leads to a problem called a Deadlock.
A deadlock occurs when two or more transactions are waiting indefinitely for each other to release locks.
Example Scenario
Transaction T1 locks Row A and waits for Row B
Transaction T2 locks Row B and waits for Row A
Both are waiting forever โ creating a deadlock!
๐ก Deadlock Handling Strategies
Databases use different approaches to detect and resolve deadlocks:
Timeouts: Abort a transaction if it waits too long.
Wait-For Graphs: Detect cycles and terminate one transaction.
Consistent Resource Ordering: Access resources in a predefined order to prevent cycles.
A typical MySQL error might look like this:
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
What happens if a system crashes in the middle of a transaction?
This is where log-based recovery comes in.
๐ What Is a Log?
A log file records all modifications made by transactions.
Before any change is applied to the database, the action is written to the log โ a principle called Write-Ahead Logging (WAL).
After a crash:
Undo (Rollback): Revert the effects of incomplete transactions.
Redo: Reapply the effects of committed transactions not yet saved to disk.
This ensures data durability and atomicity โ even after system failures.
โ
Summary
Concept Description
Transaction A logical unit of work that must be fully completed or fully undone.
Deadlock A situation where transactions wait indefinitely for each otherโs resources.
Log-Based Recovery A mechanism to recover the database after a failure using transaction logs.
๐ Final Thoughts
Understanding how transactions, deadlocks, and recovery mechanisms work is crucial for database professionals and developers alike.
These core principles ensure that our systems remain reliable, consistent, and fault-tolerant โ even under heavy load or unexpected crashes.
Top comments (0)