Recovery restores a consistent database after failures. The DBMS logs every update before writing the data (write-ahead logging), then undoes uncommitted work and redoes committed work; checkpoints bound that effort, shadow paging avoids logs, and ARIES is the industrial algorithm.
A shop records every sale in its cash book before the money goes into the drawer. If the drawer is jumbled in a blackout, the shopkeeper replays the book: finished sales are re-entered, half-finished ones are reversed. A database log is that cash book, and a checkpoint is the line ruled at the end of each day saying 'everything above this line is already settled — start checking from here'.
Key points — the night-before list
Failures: transaction (logical error, deadlock victim), system crash (main memory lost, disk intact), media failure (disk lost — restore a backup and redo the archived log).
Log records: <T start>, <T, X, old, new>, <T commit>, <T abort>, kept on stable storage; old values enable UNDO, new values REDO.
Write-ahead logging: a page's log records reach stable storage before the page; all of T's records, including <T commit>, are forced before commit completes.
Deferred update: database changed only after commit → no UNDO, REDO only. Immediate update: changes may precede commit → UNDO and REDO.
Restart: REDO every transaction with both start and commit records; UNDO every transaction with a start record but no commit/abort. Undo runs backwards, redo forwards.
Checkpoint: flush log and dirty pages, write <checkpoint L> (L = active transactions); transactions committed before it need no redo.
Shadow paging: copy-on-write pages plus shadow and current page tables; commit = atomically switching the page-table pointer — no undo/redo, but fragmentation and garbage collection.
Logical error (bad input, constraint violation) or system error (deadlock victim)
Only that transaction's work
Roll it back using the log
System crash
Power cut, OS or DBMS bug, hardware fault
Main memory: buffer contents and uncommitted state; disk is intact (fail-stop assumption)
Restart recovery: undo losers, redo winners
Media (disk) failure
Head crash, controller failure, disk destroyed
Data on the failed disk
Restore the last backup, then redo from the archived log
Failure classification
Storage is classified by what survives. Volatile storage (main memory, caches) loses its contents in a crash. Non-volatile storage (disk, SSD) survives crashes but can itself fail. Stable storage is an idealisation that never loses data, approximated by keeping several copies on independent devices or sites; the log is written to stable storage.
Transactions change data in buffer pages in memory; the buffer manager writes pages to disk later. Two policy choices decide what recovery must do. Steal lets the buffer write a page containing uncommitted changes to disk (to free a frame); force writes every page a transaction changed to disk at commit.
Policy
Consequence for recovery
Steal
Uncommitted changes may be on disk at a crash → UNDO needed
No-steal
Uncommitted changes never reach disk → no UNDO, but the buffer must hold every page of every active transaction
Force
Committed changes are on disk at commit → no REDO, but commit is slow (many random writes)
No-force
Committed changes may still be only in the buffer → REDO needed, but commit is fast
Real systems choose steal + no-force for performance — and therefore need both UNDO and REDO
02Log-based recovery and write-ahead logging
The log (journal) is an append-only sequence of records describing every database change, written to stable storage. Typical records: <Ti start> when Ti begins; <Ti, X, V1, V2> when Ti changes item X from old value V1 to new value V2; <Ti commit>; <Ti abort>. The old value is the undo (before-) image and the new value the redo (after-) image.
TextThe log of a ₹500 transfer (A: 2000 → 1500, B: 1000 → 1500)
Before a data page is written to disk, every log record describing changes on that page must already be on stable storage — otherwise a crash could leave a change on disk with no undo information.
Log before commit
A transaction is committed only when all its log records, including <T commit>, are on stable storage. Writing the commit record is the commit point.
Log in order
Log records are written to stable storage in the order they were created; a log buffer collects them and is flushed (forced) as one sequential write.
Sequential log writes are much cheaper than the random writes needed to force data pages, which is why WAL with a no-force policy commits fast. Group commit batches the commit records of several transactions into one log flush to cut disk writes further. The bank-transfer sequence in the transactions topic shows these steps in order.
03Deferred versus immediate update
Deferred database modification
All writes are recorded in the log; the database itself is updated only after the transaction commits
Log records need only the new value: <T, X, V2>
Uncommitted transactions never touched the database → NO-UNDO
After a crash: REDO every committed transaction; ignore the rest
Resembles a no-steal policy
Immediate database modification
The database may be updated while the transaction is still active (after the log record is written)
Log records need old and new values: <T, X, V1, V2>
Uncommitted changes may be on disk → UNDO needed
Committed changes may be only in the buffer → REDO needed
Used by practically all real systems (steal/no-force)
Log at crash (immediate update):
<T1 start> <T1, A, 1000, 950> <T1, B, 2000, 2050> <T1 commit>
<T2 start> <T2, C, 700, 600> ← crash
T1 has start and commit → REDO T1: A = 950, B = 2050
T2 has start, no commit → UNDO T2: C = 700 (restore the old value)
If the crash had come after <T2 commit> → REDO both: C = 600
If it had come before <T1 commit> → UNDO T1 too: A = 1000, B = 2000
Restart decisions depend only on which commit records reached the log
UNDO processes the log backwards (so an item changed twice by a loser ends with its oldest value) and REDO processes it forwards (so an item changed twice by winners ends with its latest value). Both operations are idempotent — applying them twice gives the same result as once — which matters because the system may crash again during recovery.
04Checkpoints
Without checkpoints, restart would scan the entire log from the beginning and redo transactions whose changes reached disk long ago. A checkpoint periodically brings disk up to date with the log so that older history can be skipped.
A simple (quiescent) checkpoint
Pause updates
Temporarily stop new update operations.
Flush the log
Write all log records in the log buffer to stable storage.
Flush the data
Write all dirty buffer pages to disk.
Record it
Write <checkpoint L> to stable storage, where L lists the transactions active at that moment; then resume.
Transaction
Relative to the checkpoint and the crash
Action at restart
T1
Started and committed before the checkpoint
None — its changes were flushed at the checkpoint
T2
Started before the checkpoint, committed after it (before the crash)
REDO
T3
Started after the checkpoint, committed before the crash
REDO
T4
Started before the checkpoint, still active at the crash
UNDO (including its changes before the checkpoint)
T5
Started after the checkpoint, still active at the crash
UNDO
The classic checkpoint classification
Restart algorithm: find the last <checkpoint L>; set undo-list = L; scan forward to the end of the log adding each newly started transaction to the undo-list and moving each transaction whose <commit> (or <abort>) appears to the redo-list (or dropping it). Then undo the undo-list backwards — continuing past the checkpoint until each loser's <start> is found — write <T abort> for each, and redo the redo-list forwards from the checkpoint. Fuzzy checkpoints avoid pausing the system by recording the dirty-page list and flushing pages in the background, as ARIES does.
05Shadow paging
Shadow paging (used in IBM's System R) avoids a log for recovery. The database is a set of fixed-size pages reached through a page table. At the start of a transaction the page table is copied: the shadow page table, on disk, is never modified during the transaction; the current page table is used for all reads and writes.
How it works
First write to a page
Copy the page to an unused disk page, modify the copy, and point the current page table's entry at the copy (copy-on-write). The original page is untouched.
Commit
Flush the modified pages, write the current page table to disk, then atomically overwrite the single on-disk pointer (master record) so that it designates the current table as the new shadow.
Abort or crash before the switch
Simply discard the current page table: the shadow table still describes the unmodified database.
After the switch
The old pages and old table are garbage and are reclaimed.
Advantages
No undo and no redo: recovery after a crash is almost instantaneous
No log records to write for single-user operation
Commit is a single atomic pointer switch
Disadvantages
Copying the page table is expensive for large databases (mitigated by tree-structured tables)
Updated pages move, destroying physical clustering — data fragmentation
Garbage collection of old pages is needed
Concurrent transactions still require logging, so logs are not eliminated
06ARIES in brief
ARIES (Algorithm for Recovery and Isolation Exploiting Semantics), published by C. Mohan and colleagues at IBM in 1992, is the recovery method behind DB2, SQL Server and many other systems. It supports steal/no-force buffering, fine-grained locking and fuzzy checkpoints.
Every log record has a log sequence number (LSN); every page records the LSN of the last change applied to it (pageLSN), so redo can skip changes the page already contains.
The log also stores a per-transaction backward chain (prevLSN), a transaction table and a dirty page table captured at fuzzy checkpoints.
Analysis pass: scan forward from the last checkpoint to rebuild the dirty page table and find the transactions active at the crash (losers).
Redo pass: starting from the smallest recLSN in the dirty page table, repeat history — reapply every logged update, including those of losers, whose LSN exceeds the pageLSN.
Undo pass: roll back the losers in reverse LSN order, writing compensation log records (CLRs) for every undone change; a CLR's UndoNextLSN ensures that a crash during recovery never undoes the same change twice.
07Backups and media recovery
A disk failure destroys data that no in-place log can rebuild, so systems also keep backups (dumps) on separate media. A full backup copies everything; an incremental backup copies what changed since the last backup of any kind; a differential backup copies what changed since the last full backup. Hot (online) backups run while the database is in use; cold backups require shutting it down.
Media recovery = restore the most recent backup, then redo the archived log from the time of the backup up to the failure (or to a chosen moment — point-in-time recovery). RAID protects against a single disk failing but not against accidental deletion, corruption or site disasters, so it is no substitute for backups. Remote backup systems ship the log to a standby site that takes over if the primary site fails.
Note
SQLite in practice: by default it uses a rollback journal — before changing a page it copies the original page to a journal file (an undo log) and deletes the journal at commit; in WAL mode new versions are appended to a write-ahead log and checkpointed into the database later, so readers do not block writers.
Worked examples
Try the problem first, then reveal the working one step at a time.
EX 1Restart after a crash with a checkpoint
Immediate-update log at the crash: <T1 start><T1, A, 100, 200><T1 commit><T2 start><T2, B, 300, 400><T4 start><T4, C, 500, 600><checkpoint {T2, T4}><T2, D, 10, 20><T2 commit><T3 start><T3, E, 1, 2><T3 commit><T5 start><T5, F, 7, 8><T4, G, 30, 40> — crash. Which transactions are undone and redone, and what are the final values of A to G?
EX 2Deferred update — the same situation
Under deferred update the log is <T1 start><T1, A, 950><T1, B, 2050><T1 commit><T2 start><T2, C, 600> and the system crashes. What does recovery do, and why are only new values logged?
EX 3Shadow paging trace
A database has pages 1–5, all listed in the shadow page table. Transaction T updates pages 2 and 4, and free disk pages 6 and 7 are available. Describe the page tables after the updates, at commit, and after a crash just before the pointer switch.
Practice session
Work each problem on paper first, then check. Marking one solved earns 10 XP.0/5 solved
Q1
Log at a crash (immediate update): <T1 start><T1 commit><T2 start><checkpoint {T2}><T2 commit><T3 start><T4 start><T4 commit><T5 start> — which transactions are redone, which undone and which need nothing?
Q2
A DBMS uses a steal, no-force buffer policy. Which recovery operations does it need, and why? What would a no-steal, force policy need?
Q3
Under deferred update, the log shows <T7 start> and two write records for T7 but no <T7 commit>. What must recovery do for T7?
Q4
Explain with an example why UNDO must scan the log backwards and REDO forwards.
Q5
A transaction's <T commit> record is in the log buffer, not yet on disk, when the power fails. Is the transaction committed? What happens at restart?
Exam traps
WAL means the log record goes to stable storage before the data page (and before commit completes) — not after.
Deferred update needs no UNDO (only REDO); immediate update needs both UNDO and REDO.
UNDO scans the log backwards and REDO forwards; both must be idempotent because recovery itself can crash.
Transactions that committed before the last checkpoint need no redo; losers active at the checkpoint must be undone even for changes made before it.
Shadow paging needs no undo/redo, but it fragments data, needs garbage collection, and concurrent systems still need logs.
A commit record still in the log buffer does not count: without it on stable storage the transaction is undone.
Quiz — KPSC style
10 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.