Transactions and Recovery Theory

Deferred and Immediate Database Modification Explained

Deferred and immediate database modification are two ways a DBMS applies transaction changes. Learn when writes happen and what recovery needs after a crash.

On this page
  1. Why a DBMS needs a rule for this
  2. The role of the log
  3. Immediate database modification
  4. Deferred database modification
  5. Comparing the two approaches
  6. Which approach do real systems use?
  7. Related topics
  8. Key takeaways

Deferred and immediate database modification are two ways a database management system (DBMS) can apply a transaction’s changes to the data. With immediate modification, the DBMS writes changes to the database while the transaction is still running, before it commits. With deferred modification, the DBMS holds the changes back and applies them after the transaction commits. Both approaches aim to keep transactions atomic and durable. The main difference is what the system must undo and redo after a failure.

Why a DBMS needs a rule for this

A transaction is a group of operations that should succeed or fail as one unit. Atomicity means that either all of its changes take effect or none of them do. Durability means that once a transaction commits, its changes survive a crash. A transaction interrupted halfway should not leave data partly updated, and a committed transaction should not vanish after a power cut.

Two commands end a transaction. COMMIT makes its changes final, and ROLLBACK cancels them. Both belong to transaction control, one of the command groups described in the DBMS languages list.

The role of the log

A DBMS keeps a transaction log, which is a record of changes written in order. The log is what makes recovery possible. For each change, it can store the new value, which is needed to redo a committed change after a crash, and the old value, which is needed to undo an uncommitted one. Deferred and immediate modification differ mainly in which of these the log must keep.

Many systems follow a write-ahead rule: the log record for a change reaches stable storage before the changed data does. That order means the log has the information needed to recover, even when the data files on disk are behind.

Immediate database modification

With immediate modification, a transaction’s writes go to the database as it runs, so the new values are in the data while the transaction is still open. If the transaction rolls back, or the system crashes before it commits, the DBMS has to reverse the changes it made. For that reason, the log stores old values as well as new ones.

The database can hold changes from transactions that have not committed. A crash can therefore leave uncommitted changes on disk, and recovery must undo them. A crash can also happen after a commit but before the changes reached disk, and recovery must redo them from the log. The step-by-step version of this approach is explained in What is immediate database modification and how does it work?

Deferred database modification

With deferred modification, the DBMS does not change the database until the transaction commits. While the transaction runs, its writes are kept in the log and in a workspace that belongs to that transaction. The transaction must be able to read its own pending writes, so reads are directed to those values rather than to the database.

Because uncommitted changes never reach the database, a failure before commit needs no undo. The DBMS can simply ignore the unfinished transaction. After commit, the DBMS applies the changes. If a crash happens before all of them are written, the log lets the DBMS redo them.

Deferral has a cost. Pending changes take up space until the transaction commits, which matters for large transactions. The work of applying the changes also happens after commit rather than during the transaction.

Comparing the two approaches

Immediate modification Deferred modification
When changes reach the database While the transaction runs After commit
Old values in the log Needed, for undo Not needed for undo
Undo after a crash Needed for uncommitted changes that reached disk Usually not needed
Redo after a crash Needed for committed changes not yet on disk Needed for committed changes not yet applied
Main concern Keeping other transactions from reading uncommitted changes Holding pending changes until commit

Which approach do real systems use?

Textbooks present both approaches, and the comparison helps explain how recovery works. Real database engines are more complicated. Most mainstream engines change data pages while a transaction is still open, and they rely on a write-ahead log for crash recovery. Each engine handles rollback in its own way, such as with undo records or older row versions. In that broad sense, they follow the immediate pattern.

Storage choices can also matter. In MySQL, transaction support depends on the table’s storage engine: InnoDB supports transactions, while MyISAM tables do not. The options are covered in Types of MySQL Tables and Storage Engines.

Both approaches depend on the DBMS saving its progress at regular points, so that recovery does not have to begin at the start of the log. That process is covered in What are checkpoints in a database management system (DBMS)?

Uncommitted changes that other transactions can see create a separate risk. When one transaction reads data that another has changed but not committed, the reader may use a value that later disappears. What is dirty read in DBMS? explains that problem.

Key takeaways

  • Immediate modification writes changes during the transaction, so recovery needs both undo and redo.
  • Deferred modification holds changes until commit, so a failed transaction usually needs no undo, but pending changes must be kept somewhere.
  • Both approaches rely on the log to provide atomicity and durability.

Keep exploring

Guides from other topics

All topics →