This is a comprehensive question about ARIES (Algorithms for Recovery and Isolation Exploiting Semantics), which is a classic database recovery algorithm. Let me break this down into parts:
- ARIES Recovery Algorithm Phases
- Why redo replay history even for transactions that will abort
- Repeating history and how LSN prevents re-applying updates already on disk
ARIES Recovery Algorithm Phases
ARIES has three main phases:
Phase 1: Analysis Phase
- Scans the log forward from the most recent checkpoint
- Determines:
- The set of dirty pages (pages that need to be redone)
- The set of loser transactions (transactions that need to be undone/aborted)
- The redo LSN (where to start redo phase)
Phase 2: Redo Phase
- Repeats history: replays all updates from the redo LSN forward to the end of the log
- This includes updates from:
- Committed transactions
- Aborted transactions
- In-progress transactions
- Uses LSNs to ensure idempotency (an update already on disk won't be re-applied)
Phase 3: Undo Phase
- Undoes the effects of loser transactions (those not committed at time of crash)
- Works backward through the log
- Uses compensation logs (CLRs) to record undo operations
- Restores database to a state as if loser transactions never executed
Why Redo Replay History Even for Transactions That Will Abort?
This is a crucial concept. The key insight is the distinction between:
- Logical state vs Physical state
- What needs to be done during recovery
The reasons:
-
Separation of Concerns:
- During redo phase, we DON'T KNOW which transactions will be undone yet
- We're strictly replaying the log to restore durability
- The decision about which transactions to undo comes later (undo phase)
-
Handling Partial Writes:
- Updates from uncommitted transactions might already exist on disk from a partial flush before the crash
- These must be restored if they were lost, regardless of whether the transaction ultimately commits
- The redo phase ensures all logged changes are materialized
-
Safe Reapplication:
- Reapplying an update that's already on disk causes no harm because LSNs guarantee we won't duplicate the effect
- This uniformity eliminates the need to track transaction status during redo, simplifying the algorithm
-
Uniform Processing:
- Treating all log entries identically during redo avoids complex bookkeeping
- We replay everything first, then selectively undo what shouldn't have happened
-
Preventing Inconsistency:
- Skipping updates we're unsure about risks leaving pages in a corrupted state
- For instance, if transaction T1 modified page P1 before crashing, omitting that redo would leave P1 partially updated and invalid
The redo phase replays all logged updates to restore pages to their pre-crash state, ensuring durability. Each log record carries an LSN, and every page tracks the LSN of its most recent update. When redo processes a log entry, it compares the entry's LSN against the page's current LSN—if the page already reflects that change, the redo is skipped, preventing duplicate application of the same update. me think through how to organize this explanation clearly for the user.
I should structure this to flow from the core mechanism through to practical application. The key is showing how PageLSN acts as a checkpoint—when we encounter a log record during recovery, comparing its LSN against the page's stored LSN tells us whether that update already made it to disk or still needs to be applied. This comparison prevents redundant writes and ensures we don't miss any updates that were in the log but not yet persisted. The concrete example demonstrates this in action: one page already has the update reflected in its LSN so we skip it, while the other page needs the redo operation applied and its LSN updated accordingly.
ARIES Recovery Algorithm
Three Recovery Phases
Phase 1: Analysis
Scans log forward from most recent checkpoint to identify:
- Dirty pages: Pages modified but possibly not flushed
- Loser transactions: Transactions not committed at crash time
- Redo start point: Where to begin replay (earliest affected log record)
Phase 2: Redo (Repeating History)
Replays all updates from the redo point forward to end of log:
FOR each log record from redo_start_LSN to end:
IF page.pageLSN < log.LSN:
Redo this update
Update page.pageLSN = log.LSN
This includes committed, aborted, AND in-progress transactions.
Phase 3: Undo (Rollback)
Rollback only the loser transactions in reverse log order:
- Follow transaction's lastLSN pointers backward
- Generate CLRs (Compensation Log Records) for undo operations
- Never re-redo these CLRs (marked with undoNextLSN)
Why Redo Aborted Transactions?
The Core Insight: Separation of Concerns
During recovery, you must distinguish between:
- Physical recovery: Restoring durable state (redo phase)
- Logical recovery: Removing unwanted effects (undo phase)
Three Key Reasons:
1. Unknown Commit Status During Redo
Recovery doesn't know transaction fate during redo phase:
- Can't distinguish committed vs. aborted yet
- Must treat all operations uniformly
- Decision deferred to undo phase
2. Atomicity Requirement
Problem: Skip redo for "might-be-aborted" T1:
T1: UPDATE Table SET x = 10
T2: UPDATE Table SET x = 20 WHERE x = 10 ← T2 might depend on T1's update
If we skip T1's redo:
- x never becomes 10
- T2 updates fail to find matching rows
- Corruption: partially-applied transactions
Solution: Complete the redo, then undo T1 cleanly
3. Crash-Consistency Preservation
Scenario:
T1: Write updated page P1 to disk buffer
Cache flushes P1 but log not yet forced
Cache crashes before log flush
Recovery must redo:
- Log record exists but wasn't durable before crash
- P1 on disk might be from old transaction
- Redo restores correctness
4. Idempotency Safety
Redoing an already-completed update is safe (via LSN comparison)
Repeating History & LSN-Based Idempotency
The Repeating History Concept
ARIES replays the entire log history "up to the crash":
Original execution:
T1: [Write A=1]→disk [Write B=2]→mem → CRASH
Repeating history:
Redo: [Write A=1]→already on disk [Write B=2]→redo to disk
Undo: Revert T1's writes with CLRs
How LSN Prevents Duplicate Application
LSN = Log Sequence Number (monotonically increasing per log record)
Mechanism:
Data structure on each page:
page.pageLSN = LSN of last update applied to this page
Record redo logic:
function redo(logRecord):
page = readPage(logRecord.pageID)
IF page.pageLSN >= logRecord.LSN:
RETURN // Already on disk, skip
ELSE:
Apply(logRecord)
page.pageLSN = logRecord.LSN
writePage(page)
Example:
Log:
LSN 100: T1 updates Page 17: A := A + 1
LSN 101: T1 updates Page 17: A := A + 1
LSN 102: T2 updates Page 18: B := 99
Before crash:
Page 17 flushed to disk with pageLSN = 100
Page 18 only in memory
Recovery redo phase:
Process LSN 100: Read Page 17, see pageLSN=100
100 >= 100? YES → SKIP ✓
Process LSN 101: Read Page 17, see pageLSN=100
100 >= 101? NO → REDO, set pageLSN=101
Process LSN 102: Read Page 18, see pageLSN=0
0 >= 102? NO → REDO, set pageLSN=102
Why This Works:
- If page already has the update (pageLSN ≥ LSN), it's from a previous redo or from normal operation
- Applying twice = deterministic, so safe to skip
- No cascading effects (updates are physical, not logical)
Complete Recovery Example
Crash log:
LSN 1: T1 START
LSN 2: T1 writes Page X: [100→101]
LSN 3: T2 START
LSN 4: T2 writes Page Y: [50→51]
LSN 5: T1 COMMIT (durably recorded)
LSN 6: T3 START
LSN 7: T3 writes Page X: [101→102]
——— CRASH (T2, T3 uncommitted)
ANALYSIS PHASE:
Dirty pages: {X, Y}
Loser transactions: {T2, T3}
Redo start: LSN 2
REDO PHASE:
LSN 2: Read X (initial state), pageLSN=0. 0<2 → REDO, X=[100→101], pageLSN=2
LSN 4: Read Y (initial state), pageLSN=0. 0<4 → REDO, Y=[50→51], pageLSN=4
LSN 7: Read X (from redo LSN 2), pageLSN=2. 2<7 → REDO, X=[101→102], pageLSN=7
Result: X=102, Y=51 (T1 and T3's updates present)
UNDO PHASE:
Process T3 (lastLSN=7): Generate CLR for LSN 7: X=[102→101], lastLSN=none
Process T2 (lastLSN=4): Generate CLR for LSN 4: Y=[51→50], lastLSN=none
Result: X=101, Y=50 (only T1 committed)
Key Takeaways
| Aspect | Details |
|---|
| Redo aborted txns? | Yes—can't know commit status during redo; required for consistency |
| Idempotency | LSN on page + log ensures safe re-execution |
| Efficiency gain | Uniform redo avoids transaction tracking overhead |
| Correctness | Separation of physical (redo) and logical (undo) recovery |