Redo Log vs Undo Log: What is the Difference?
Learn the difference between redo and undo logs, how each supports crash recovery, rollback, and MVCC in databases.
Expected Interview Answer
A redo log records the new value produced by a change so committed effects can be reapplied after a crash, while an undo log records the old value that existed before a change so it can be reversed if a transaction rolls back or a crash leaves it incomplete.
During normal operation, both logs are written for each modification: the redo entry captures enough to reconstruct the after-image of the change, and the undo entry captures the before-image. On crash recovery, the redo log lets the engine reapply every committed change that never made it to disk, restoring durability. The undo log then lets the engine reverse any changes belonging to transactions that were still active (never committed) at crash time, restoring atomicity. Undo logs are also used outside of crash recovery, for ordinary transaction ROLLBACK and, in engines like MySQL's InnoDB, for multi-version concurrency control so readers can see a consistent snapshot without blocking writers.
- Redo log guarantees durability of committed changes
- Undo log guarantees atomicity by allowing clean rollback
- Together they let recovery both replay and reverse as needed
- Undo data also enables consistent, non-blocking reads under MVCC
AI Mentor Explanation
Think of two separate notebooks a scorer keeps: one recording the new total after every run scored, so the scoreboard can be rebuilt forward from any point, and another recording what the total was just before each run, so a wrongly-awarded run can be subtracted back out cleanly. The forward notebook is the redo log; the before-value notebook is the undo log. Databases keep both for the same reason: reapply what should have happened, reverse what should not have.
Step-by-Step Explanation
Step 1
Write both log entries
On each modification, the engine records a redo entry (new value) and an undo entry (old value) before or alongside the change.
Step 2
Use redo during recovery
After a crash, the redo log reapplies every committed change so data pages reflect all durable work, even if it never reached disk.
Step 3
Use undo during recovery
The undo log then reverses any changes from transactions that were still active and never committed at crash time.
Step 4
Use undo during normal rollback
Outside of crash recovery, the same undo records let an explicit ROLLBACK or MVCC snapshot read reverse or ignore uncommitted changes.
What Interviewer Expects
- Precise distinction between before-image (undo) and after-image (redo) data
- Correct mapping of redo to durability and undo to atomicity
- Awareness that undo logs also serve non-crash uses like ROLLBACK and MVCC
- Ability to explain how both logs are used together during recovery
Common Mistakes
- Swapping the definitions, calling the before-image the redo log
- Thinking only one of the two logs is needed for correct recovery
- Forgetting that undo logs power ordinary ROLLBACK, not just crash recovery
- Not mentioning MVCC as a real-world use of undo data in engines like InnoDB
Best Answer (HR Friendly)
“A redo log stores the new value of each change so the database can replay committed work forward after a crash, while an undo log stores the old value so the database can reverse changes from transactions that never finished, whether that is because of a crash or a normal rollback. Redo gives you durability, undo gives you the ability to cleanly cancel incomplete work.”
Code Example
-- Each UPDATE inside a transaction causes InnoDB to write:
-- 1) A redo log entry (new value) into the redo log buffer,
-- later flushed to the ib_logfile* redo log files.
-- 2) An undo log entry (old value) into the undo tablespace,
-- used for ROLLBACK and MVCC consistent reads.
START TRANSACTION;
UPDATE accounts SET balance = balance - 200 WHERE account_id = 1;
-- redo entry: balance = old_balance - 200 (new value)
-- undo entry: balance = old_balance (value before this UPDATE)
ROLLBACK;
-- InnoDB uses the undo entry to restore balance to old_balance,
-- as if the UPDATE never happened.Follow-up Questions
- How does InnoDB use undo logs to implement multi-version concurrency control?
- What happens to undo log space after a long-running transaction commits?
- How do redo and undo logs together guarantee atomicity and durability specifically?
- What is the difference between logical and physical logging for redo and undo?
MCQ Practice
1. What does a redo log entry typically record?
A redo log stores the after-image (new value) so it can reapply the effect of a committed change during recovery.
2. Which log is used to reverse the effect of a transaction that issues ROLLBACK?
The undo log stores before-images that let the engine reverse a transaction’s changes on rollback.
3. Besides crash recovery, what is another common use of undo log data in engines like InnoDB?
InnoDB reuses undo records to serve older row versions to readers under MVCC, avoiding blocking between readers and writers.
Flash Cards
What does a redo log record? — The new (after) value of a change, used to reapply committed work during crash recovery.
What does an undo log record? — The old (before) value of a change, used to reverse it on rollback or incomplete-transaction recovery.
Which log guarantees durability? — The redo log, since it lets committed changes survive a crash.
Which log guarantees atomicity? — The undo log, since it lets incomplete transactions be fully and cleanly reversed.