Database Management SystemUnit 1011 min read
Transactions, Concurrency & Recovery: Logs, Schedules & ACID
Unit 10 of Database Management System covers transaction management, concurrency control, recovery techniques, and serializability. Learn how databases ensure ACID properties, handle conflicts, and recover from failures using logs, locks, and checkpointing—with real-world examples from banking, e-commerce, and mobile a
TAKEAWAYS:
- Transactions guarantee ACID (Atomicity, Consistency, Isolation, Durability) to maintain data integrity.
- Concurrency control (locking, timestamping, MVCC) prevents lost updates and dirty reads in multi-user systems.
- Recovery uses logs (before-image/after-image) and checkpoints to restore databases after crashes.
- Serializability ensures schedules behave like serial transactions; conflict serializability and view serializability are key tests.
- Deadlocks occur in circular-wait scenarios; detection and resolution (timeout, rollback) are critical.
- Real-world impact: Banks (loan processing), eSewa (payment transactions), and Daraz (order fulfillment) rely on these mechanisms.
1. Transactions: The Building Blocks of Reliable Data Operations
A transaction is a sequence of operations that must execute atomically (all or nothing) to maintain database consistency. Think of it as a single logical unit of work—like transferring money from one bank account to another.
ACID Properties: The Pillars of Trust
Transactions must satisfy ACID to be reliable:
- Atomicity: Either all operations complete or none do (e.g., a bank transfer fails if the debit succeeds but credit fails).
- Consistency: The database moves from one valid state to another (e.g., total money in accounts remains unchanged).
- Isolation: Concurrent transactions do not interfere (e.g., two users checking the same bank balance see the same value).
- Durability: Once committed, changes survive system failures (e.g., a committed order on Daraz remains even after a server crash).
stateDiagram-v2
[*] --> Active: Transaction starts
Active --> Partially_committed: All operations executed
Partially_committed --> Committed: Commit succeeds
Partially_committed --> Aborted: Rollback triggered
Committed --> [*]
Aborted --> [*]2. Concurrency Control: Managing Multiple Transactions
When multiple transactions run simultaneously, concurrency conflicts arise:
- Lost Update: Two transactions read the same data, then overwrite it, losing one update.
- Dirty Read: A transaction reads data written by an uncommitted transaction (e.g., seeing a "pending" Daraz order as "shipped" before confirmation).
- Inconsistent Analysis: A transaction reads data twice, but another transaction modifies it in between (e.g., checking stock availability twice in an online store).
Concurrency Control Techniques
| Method | How It Works | Pros | Cons |
|---|---|---|---|
| Locking (Pessimistic) | Transactions acquire locks (shared/exclusive) on data items. | Simple, ensures strict isolation. | Can cause deadlocks, low concurrency. |
| Optimistic Concurrency | Assumes conflicts are rare; validates at commit time. | High concurrency, no locks. | High rollback overhead. |
| Timestamping | Each transaction gets a timestamp; checks for conflicts before execution. | No deadlocks, simple. | Requires system clocks to be synchronized. |
| Multi-Version Concurrency (MVCC) | Maintains multiple versions of data; readers see committed versions. | High read concurrency, no locks for reads. | Complex, storage overhead. |
Example: Bank Transfer Conflict
Scenario: Two users (A and B) transfer money from the same account to different accounts.
sequenceDiagram
participant A as User A
participant B as User B
participant DB as Database
A->>DB: Read(Account_X = 1000)
B->>DB: Read(Account_X = 1000)
A->>DB: Write(Account_X = 800) [Transfer 200 to Y]
B->>DB: Write(Account_X = 600) [Transfer 400 to Z]
Note right of B: Lost Update! Final balance: 600 (should be 400)Solution: Use exclusive locks to prevent overlapping writes.
3. Serializability: Ensuring Correct Schedules
A schedule is an interleaving of transactions. For correctness, it must be serializable—equivalent to executing transactions one after another.
Types of Serializability
| Type | Definition | Example |
|---|---|---|
| Conflict Serializable | No conflicting operations (read/write on same data) are out of order. | Schedule S: T1 → T2 is equivalent to T2 → T1 if no conflicts. |
| View Serializable | Transactions see the same data as in some serial schedule. | A reader in T3 sees the same final state as if T1 and T2 ran serially. |
| Strict/Strong Serializable | Writes are only visible after commit. | Ensures no "dirty" data is read. |
Detecting Serializability
Use a precedence graph:
- Draw a node for each transaction.
- Add an edge Tᵢ → Tⱼ if Tᵢ writes to data that Tⱼ reads/writes later.
- If the graph has a cycle, the schedule is not conflict-serializable.
Example:
graph TD
T1["T1"] --> T2["T2"]
T2 --> T3["T3"]
T3 --> T14. Deadlocks: The Circular Wait Problem
A deadlock occurs when transactions wait indefinitely for locks held by each other.
Four Necessary Conditions for Deadlock:
- Mutual Exclusion: Only one transaction can hold a lock.
- Hold and Wait: A transaction holds a lock while waiting for another.
- No Preemption: Locks cannot be forcibly taken.
- Circular Wait: A cycle exists in the wait-for graph.
Two transactions deadlocking over Account_X and Account_Y. (Image: Kkaaii, CC BY-SA 4.0, via Wikimedia Commons)
Deadlock Handling Strategies
| Strategy | How It Works | Example |
|---|---|---|
| Deadlock Prevention | Break one of the four conditions (e.g., no hold-and-wait). | Force transactions to request all locks at once. |
| Deadlock Avoidance | Use algorithms (e.g., Banker’s) to check for safe states before granting locks. | Wait-for graph analysis before granting locks. |
| Deadlock Detection | Periodically check for cycles in the wait-for graph. | Roll back one transaction in a cycle. |
| Deadlock Recovery | Abort and restart transactions or preempt locks. | Kill T2 if it’s holding a lock needed by T1. |
Real-World Example: In eSewa, if two users try to transfer money simultaneously and deadlock occurs, the system may timeout and roll back one transaction to resolve the conflict.
5. Recovery Systems: Bringing the Database Back to Life
Databases use logs and checkpoints to recover from crashes.
Log-Based Recovery
A log records changes before (before-image) and after (after-image) they are applied.
Log Record Format:
<Transaction ID, Operation, Data Item, Before-Image, After-Image, Timestamp>
Example:
T1, Write, Balance, 1000, 800, 12:00:00
T2, Read, Balance, -, -, 12:00:01
Recovery Steps:
- Redo: Reapply committed transactions from the log.
- Undo: Roll back uncommitted transactions.
sequenceDiagram
participant DB as Database
participant Log as Log File
DB->>Log: Write log record (T1, Write, Balance, 1000, 800)
DB-->>Log: Crash occurs
Log->>DB: Redo committed transactions
Log->>DB: Undo uncommitted transactionsCheckpointing
- Checkpoint: A snapshot of the database where all logs up to that point are flushed to disk.
- Benefits: Reduces recovery time by minimizing the need to redo/undo from the start.
Example:
Checkpoint at time T:
- All logs up to T are written to disk.
- Active transactions: T1 (committed), T2 (aborted).
- Recovery starts from T instead of the beginning.
6. Real-World Applications
1. Banking (Nabil Bank, Global IME)
- Transactions: Loan approvals, fund transfers.
- Concurrency: Multiple users checking/updating accounts simultaneously.
- Recovery: Logs ensure that if a server crashes during a transfer, the system can undo or redo the transaction.
2. eSewa & Khalti (Digital Payments)
- Transactions: Money transfers between users.
- Conflict: Two users transferring money from the same account at the same time.
- Solution: Locking the account balance during the transfer to prevent lost updates.
3. Daraz (E-Commerce Orders)
- Transactions: Order placement, stock updates.
- Concurrency: Multiple users buying the same product.
- Solution: MVCC allows readers to see the stock before it’s updated, while writers lock the stock.
4. NTC & Ncell (Billing Systems)
- Transactions: Updating customer bills, processing payments.
- Recovery: Logs ensure that if a power outage occurs, bills are not lost or duplicated.
Exam Tip
What Examiners Look For
- Definitions: Clearly define ACID, serializability, deadlock, and recovery.
- Examples: Always support answers with schedules, precedence graphs, or log records.
- For serializability, draw a graph and explain cycles.
- For recovery, show a log example and steps for redo/undo.
- Real-World Links: Connect concepts to banks, eSewa, or Daraz (e.g., "Like in eSewa, transactions must be atomic to avoid partial money transfers").
- Diagrams: Use state diagrams for transaction life cycles, sequence diagrams for concurrency conflicts, and graphs for deadlocks.
- Common Pitfalls:
- Lost Update ≠ Dirty Read: Know the difference!
- Conflict Serializable ≠ View Serializable: Examiners test this distinction.
- Deadlock Prevention vs. Detection: Don’t confuse the two.
High-Score Answer Structure
For questions like:
"Explain log-based recovery with an example."
Do This:
- Define log-based recovery (redo/undo).
- Show a log record (with before/after images).
- Trace recovery steps (e.g., "After crash, redo T1 and undo T2").
- Link to real-world: "Like in Nabil Bank, logs ensure transactions are not lost during power cuts."
Avoid:
- Vague explanations ("Logs help recover data").
- Missing steps (e.g., forgetting to mention checkpoints).
- No diagrams (always draw a log record table or recovery flow).
Practice Questions (Self-Check)
- Draw a precedence graph for the following schedule and check for serializability:
T1: Read(A), Write(B) T2: Read(B), Write(A) - Explain how MVCC prevents dirty reads in a high-concurrency system like Daraz.
- Write a log record for a transaction that reads
Salary = 50000and then writesSalary = 55000. - Describe how a bank would detect and resolve a deadlock between two loan-processing transactions.
Final Note: Master ACID, serializability, and log recovery—these are the core of reliable database systems. Use diagrams to visualize conflicts, deadlocks, and recovery steps. Real-world examples (banks, eSewa) will help you remember concepts during exams!
Based on the PU BE Computer (PU) syllabus for Database Management System, unit 10.
Discussion
Loading…