Describe the phases an ARIES-style recovery process runs through when a relational database restarts after a crash, and what each phase accomplishes.
answer
- analysis, redo, undo
- analysis rebuilds transaction table and dirty page table
- redo start equals minimum recLSN, earlier than checkpoint
- redo repeats history including losers
- undo writes CLRs, restartable
basics
~20 sThree passes. Analysis reads forward from the last checkpoint to rebuild the list of in-flight transactions and dirty pages and to find where redo must start. Redo replays every logged change, committed or not, until the data files match the log. Undo then rolls back the transactions that never committed.
solid answer
~60 sARIES recovery runs **analysis, redo, undo**. **Analysis** starts at the last checkpoint record and scans forward to the end of the log. It reconstructs two structures the crash destroyed: the transaction table (which transactions were active, and their last log record) and the dirty page table (which pages had unwritten changes, and the earliest log record that dirtied each). From the dirty page table it computes the redo start point: the oldest log sequence number that could still be missing from disk. **Redo** scans forward from that point and reapplies every logged change to the pages, regardless of whether the transaction committed. This is the *repeating history* step, and it is idempotent: for each page the engine compares the log record's LSN against the page's stored LSN and skips work already applied. **Undo** then rolls back the losers, the transactions with no commit record, walking their log records backwards and writing compensation log records as it goes so that undo itself is restartable. After undo the database is transactionally consistent and opens for queries.
code
text · 10 linesLSN 050 T1 UPDATE page 7 (page 7 dirtied here -> recLSN 050)
LSN 060 CHECKPOINT {active: T1; dirty: page7@050}
LSN 070 T2 UPDATE page 9 (recLSN 070)
LSN 080 T1 COMMIT
LSN 090 T2 UPDATE page 7
<< crash >>
analysis: start 060 -> losers = {T2}, dirty = {7@050, 9@070}
redo: start at min(recLSN) = 050, replay 050..090
undo: reverse 090, then 070 (T2), writing CLRsgo deeper
Name the three passes in order and give one sentence each: rebuild bookkeeping, reapply everything, roll back the uncommitted.
Be precise about what analysis reconstructs (transaction table and dirty page table), where redo starts (minimum recLSN), and that redo includes uncommitted work.
Explain why the order is forced, how LSN comparison makes redo idempotent, and how CLRs make undo restartable; connect redo length to checkpoint and background-write behaviour.
Discuss the availability tradeoff: what sets recovery time, which parts can overlap with serving traffic, and how the design choice of repeating history buys a single simple code path shared by rollback and recovery.
## Setting: what the crash destroyed While running, the engine holds in memory (a) the buffer pool of cached pages, some of them dirty, (b) a table of active transactions, and (c) a table of which cached pages are dirty and since when. A crash wipes all three. What survives is the transaction log on durable storage plus the data files, which are a partial, inconsistent snapshot: some committed changes are missing from them, some uncommitted changes are present in them. Every log record carries a monotonically increasing **log sequence number (LSN)**, and every data page stores the LSN of the most recent change applied to it (the page LSN). Those two facts drive the whole algorithm. ## Pass 1: analysis Analysis starts at the most recent checkpoint record and reads forward to the physical end of the log. A checkpoint record is essentially a memo the running system wrote about itself: which transactions were active and which pages were dirty at that moment. Analysis loads that memo, then replays the log forward to bring it up to date: transaction start records add entries, commit and end records remove them, update records add pages to the dirty page table if not already present, recording the **recLSN**, the LSN of the first log record that dirtied that page since it was last written to disk. Analysis outputs three things: the set of losers (transactions still active at the end of the log with no commit record), the reconstructed dirty page table, and the **redo start point**, which is the minimum recLSN over the dirty page table. Any change older than that point is guaranteed already in the data files, so redo can safely skip it. Note this start point is typically *earlier* than the checkpoint itself, because a page could have been dirtied long before the checkpoint and still not written. Analysis changes no data; it only rebuilds bookkeeping. ## Pass 2: redo Redo scans the log forward from the redo start point and, for each redoable record, decides whether to apply it. It skips the record quickly if the page is absent from the dirty page table, if the record's LSN is below that page's recLSN, or, after fetching the page, if the page's stored LSN is already at or above the record's LSN, meaning the change is already there. Otherwise it applies the change and stamps the page LSN with the record's LSN. Because the decision is driven by comparing LSNs, redo is idempotent: crashing and re-running it produces the same result. The defining property is that redo **repeats history**: it reapplies changes made by losers too, not just by committed transactions. After redo, the pages look exactly as they did at the instant of the crash, uncommitted garbage included. That is deliberate, because it means undo faces a state it can reason about with ordinary rollback logic instead of a special case. ## Pass 3: undo Undo removes the losers. It processes their log records backwards, in reverse LSN order across all losers at once, following each record's *previous LSN* pointer to walk each transaction's own chain. For each update it applies the before-image and writes a **compensation log record (CLR)** describing the reversal. A CLR carries an *undo-next* pointer to the record that should be undone after it, so if the server crashes during undo, the restarted recovery finds the CLR, knows that step is done, and continues from where it stopped instead of undoing anything twice. CLRs are redo-only and are never undone. When a loser's chain is exhausted, an end record is written for it. When all losers are done, the database is transactionally consistent and opens for connections. ## Why this order Redo before undo is not arbitrary. Undo needs a well-defined starting state, and the only well-defined state available is the state at the moment of the crash, which is what redo reconstructs. Attempting selective redo of winners only would leave the pages in a state that matches no point in real history, and the before-images recorded in the log would no longer line up with what is on the page. ## Practical notes Recovery cost is dominated by redo, whose length is set by how far the dirty page table's oldest recLSN is behind the end of the log, which in turn is set by checkpoint frequency and background page-writing aggressiveness. Some engines run undo concurrently with normal operation, opening the database once redo has finished and letting the surviving locks (rebuilt by analysis) keep other sessions off the rows still being rolled back.
- Why does redo start before the checkpoint record rather than at it?Because the checkpoint records which pages were already dirty at that time, and those pages may have been dirtied much earlier and still not written to disk. The safe starting point is the minimum recLSN across the dirty page table, which is the oldest change that might be missing from the data files. Starting at the checkpoint itself would skip those older unwritten changes and silently lose data.
- What exactly does the analysis pass produce, given that it modifies no data?It reconstructs the two in-memory structures the crash destroyed: the transaction table, telling it which transactions were active and therefore are losers, plus their last LSN for undo; and the dirty page table with each page's recLSN, from which it derives the redo start point. It may also rebuild the lock set for rows belonging to losers, so the database can open before undo finishes.
- Can the database accept queries before undo completes?Some engines allow it. Once redo has finished, the pages match the crash-time state and the locks held by loser transactions can be reacquired from the analysis pass, so other sessions can run safely while rollback proceeds in the background. They will simply block on rows still owned by the transactions being undone. Redo, by contrast, must complete first, because before it the data files are not a coherent state.
saying these in an interview costs you the question
- Claiming redo replays only committed transactions
- Saying analysis performs the rollback or applies changes
- Believing recovery replays the entire log from the beginning of time
- Saying redo can start exactly at the checkpoint record
- Treating undo as unrestartable, so a second crash means starting rollback over