Skip to main content

Why do we need an undo / rollback log?

An undo (or rollback) log exists to undo the changes a transaction made if it ends in an error or gets rolled back (ROLLBACK).

It stores old versions of the data, the state before the changes began.

How it works

  1. When a transaction changes data (e.g. via UPDATE), the DBMS writes the rows' previous state into the undo log.
  2. If the transaction completes successfully (COMMIT), the log entries can be cleared.
  3. If the transaction fails, the DBMS uses the undo log to restore the old values and return the database to a consistent state.

Example

sql
UPDATE users SET balance = balance - 100 WHERE id = 1;

If the server suddenly shuts down before COMMIT, the undo log lets the debit be undone and the old balance restored.

Besides rollback, the undo log also helps:

  • provide transaction atomicity (the A in ACID),
  • implement multiversion concurrency control (MVCC), reading "old" data while other transactions are still changing it.

Summary: an undo log is insurance for your data, storing "what it was before" and letting you safely undo any unfinished changes.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.