![]() |
VOOZH | about |
Log-based recovery in DBMS ensures data can be maintained or restored in the event of a system failure. The DBMS records every transaction on stable storage, allowing for easy data recovery when a failure occurs. For each operation performed on the database, a log file is created. Transactions are logged and verified before being applied to the database, ensuring data integrity.
A log is a sequence of records that document the operations performed during database transactions. Logs are stored in a log file for each transaction, providing a mechanism to recover data in the event of a failure. For every operation executed on the database, a corresponding log record is created. It is critical to store these logs before the actual transaction operations are applied to the database, ensuring data integrity and consistency during recovery processes.
For example, consider a transaction to modify a student's city. This transaction generates the following logs:
1. Start Log: When the transaction begins, a log is created to indicate the start of the transaction.
2. Operation Log: When the city is updated, a log is recorded to capture the old and new values of the operation.
3. Commit Log: Once the transaction is successfully completed, a final log is created to indicate that the transaction has been completed and the changes are now permanent.
These logs play a crucial role in ensuring that the database can recover to a consistent state after a system crash. If a failure occurs, the DBMS can use these logs to either roll back incomplete transactions or redo committed transactions to maintain data consistency.
The undo operation reverses the changes made by an uncommitted transaction.
Even though the transaction did not commit, its dirty pages may have already been written to disk from the buffer.
Undo restores the old values using the log to remove these partial changes.
Example of Undo: Consider a transaction T1 that updates a bank account balance but fails before committing:
Initial State: Account balance = 500.
Transaction T1:
<T1, Balance, 500, 600>
Failure: T1 fails before committing.
Undo Process:
<T1, Abort>
The redo operation re-applies the changes of a committed transaction.
Although the transaction has committed, its updated pages may not have been flushed to disk before the crash.
Redo ensures that committed changes are safely applied to the database.
Example of Redo: Consider a transaction T2 that updates an account balance but the database crashes before changes are permanently reflected:
Initial State: Account balance = 300.
Transaction T2:
<T2, Start><T2, Balance, 300, 400><T2, Commit>
Crash: Changes are not reflected in the database.
Redo Process:
Assume two transactions:
<T1, Start><T1, Balance, 500, 600><T2, Start><T2, Balance, 300, 400><T2, Commit><T1, Abort>
Identify Committed and Uncommitted Transactions:
Undo T1: Revert balance from 600 to 500.
Redo T2: Reapply balance change from 300 to 400.
| Operation | Trigger | Action |
|---|---|---|
| Undo | For uncommitted/failed transactions | Revert changes using the old values in the log. |
| Redo | For committed transactions | Reapply changes using the new values in the log. |
These operations ensure data consistency and integrity in the event of system failures.
In database systems, changes to the database can be made using two main methods: Immediate Modification and Deferred Modification.
In the Immediate Modification method, the database is updated as soon as a change is made during a transaction, even before the transaction is committed. Logs are written before making any changes to ensure recovery is possible in case of a system failure.
Transaction T0:
Transaction T1:
<T_1 start>.<T_1, C, 200, 180>, and C's new value is reflected in memory.<T_1 commit>, and the new value of C is permanently written to storage.In the Deferred Modification method, changes to the database are not applied immediately. Instead, they are logged and stored temporarily. The database is only updated after the transaction is fully committed. This method ensures that no partial changes are made to the database, reducing the risk of inconsistency.
Transaction T0:
<T_0 start>.<T_0, A, 1000, 950> is recorded, but the change is not applied to the database yet. The value of A in the database remains 1000.<T_0, B, 2000, 2050> is recorded, but the change is not applied to the database yet. The value of B in the database remains 2000.Transaction T1:
<T_1 start>.<T_1, C, 700, 600> is recorded, but the change is not applied to the database yet. The value of C in the database remains 700.Log-based recovery is a method used in database systems to restore the database to a consistent state after a crash or failure. The process uses a transaction log, which keeps a record of all operations performed on the database, including updates, inserts, deletes, and transaction states (start, commit, or abort).
Transaction Log:
Recovery Process:
After a system crash, the database uses the log to determine:
Checkpointing is a process used in DBMS to streamline the recovery procedure after a system crash by reducing the amount of log data that needs to be examined. It helps save the current state of the database and active transactions to make recovery faster and more efficient.
For example: <checkpoint L> means the database state and the list of active transactions (L) were saved.
When a crash occurs, recovery involves the following steps:
1. Find the Most Recent Checkpoint: Scan the log backward to locate the last <checkpoint> record.
2. Identify Relevant Transactions:
<Ti start> record of the oldest active transaction at the time of the checkpoint is found.3. Perform Undo Operations: For transactions without a <Ti commit> record, execute undo(Ti) to reverse incomplete changes. (Only needed in the immediate modification approach.)
4. Perform Redo Operations: For transactions with a <Ti commit> record, execute redo(Ti) to reapply their changes if needed.
Example: checkpoint occurred during: T67 and T69
Transactions T0 to T66 and T68: Completed before checkpoint → No action needed
Transactions T67, T69 to T100: Occurred during/after checkpoint → Need to be checked