0% found this document useful (0 votes)
228 views5 pages

DBMS Recovery and Atomicity Techniques

The document discusses recovery and atomicity in database management systems (DBMS), emphasizing the importance of maintaining atomicity during system crashes and the techniques used for recovery. It outlines log-based recovery, which involves logging changes to the database to ensure consistency, and shadow paging, which uses two page tables to manage transactions. Additionally, it covers recovery methods for concurrent transactions, including interaction with concurrency control, transaction rollback, checkpoints, and restart recovery.

Uploaded by

Jeevan Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
228 views5 pages

DBMS Recovery and Atomicity Techniques

The document discusses recovery and atomicity in database management systems (DBMS), emphasizing the importance of maintaining atomicity during system crashes and the techniques used for recovery. It outlines log-based recovery, which involves logging changes to the database to ensure consistency, and shadow paging, which uses two page tables to manage transactions. Additionally, it covers recovery methods for concurrent transactions, including interaction with concurrency control, transaction rollback, checkpoints, and restart recovery.

Uploaded by

Jeevan Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Recovery and Atomicity

• When a system crashes, it may have several transactions being executed and various
files opened for them to modify the data items.
• But according to ACID properties of DBMS, atomicity of transactions as a whole must
be maintained, that is, either all the operations are executed or none.
• Database recovery means recovering the data when it get deleted, hacked or
damaged accidentally.
• Atomicity is must whether is transaction is over or not it should reflect in the database
permanently or it should not effect the database at all.
When a DBMS recovers from a crash, it should maintain the following −

• It should check the states of all the transactions, which were being executed.
• A transaction may be in the middle of some operation; the DBMS must ensure the
atomicity of the transaction in this case.
• It should check whether the transaction can be completed now or it needs to be rolled
back.
• No transactions would be allowed to leave the DBMS in an inconsistent state.
There are two types of techniques, which can help a DBMS in recovering as well as
maintaining the atomicity of a transaction −

• Maintaining the logs of each transaction, and writing them onto some stable storage
before actually modifying the database.
• Maintaining shadow paging, where the changes are done on a volatile memory, and
later, the actual database is updated
Log-Based Recovery

Log-based recovery is a widely used approach in database management systems to recover


from system failures and maintain atomicity and durability of transactions. The fundamental
idea behind log-based recovery is to keep a log of all changes made to the database, so that
after a failure, the system can use the log to restore the database to a consistent state.
How Log-Based Recovery Works

1. Transaction Logging:

For every transaction that modifies the database, an entry is made in the log. This entry
typically includes:

• Transaction ID: A unique identifier for the transaction.


• Data item identifier: Identifier for the specific item being modified.
• OLD value: The value of the data item before the modification.
• NEW value: The value of the data item after the modification.
We represent an update log record as <\(T_i\) , \(X_j\) , \(V_1\), \(V_2\)>, indicating that
transaction \(T_i\) has performed a write on data item \(X_j\). \(X_j\) had value \(V_1\) before
the write, and has value \(V_2\) after the write. Other special log records exist to record
significant events during transaction processing, such as the start of a transaction and the
commit or abort of a transaction. Among the types of log records are:

• <\(T_i\) start>. Transaction Ti has started.


• <\(T_i\) commit>. Transaction Ti has committed.
• <\(T_i\) abort>. Transaction Ti has aborted.
2. Writing to the Log

Before any change is written to the actual database (on disk), the corresponding log entry is
stored. This is called the Write-Ahead Logging (WAL) principle. By ensuring that the log is
written first, the system can later recover and apply or undo any changes.
3. Checkpointing

Periodically, the DBMS might decide to take a checkpoint. A checkpoint is a point of


synchronization between the database and its log. At the time of a checkpoint:

• All the changes in main memory (buffer) up to that point are written to disk.
• A special entry is made in the log indicating a checkpoint. This helps in reducing the
amount of log that needs to be scanned during recovery.
4. Recovery Process

• Redo: If a transaction is identified (from the log) as having committed but its changes
have not been reflected in the database (due to a crash before the changes could be
written to disk), then the changes are reapplied using the 'After Image' from the log.
• Undo: If a transaction is identified as not having committed at the time of the crash,
any changes it made are reversed using the 'Before Image' in the log to ensure
atomicity.
5. Commit/Rollback

Once a transaction is fully complete, a commit record is written to the log. If a transaction is
aborted, a rollback record is written, and using the log, the system undoes any changes made
by this transaction.
Benefits of Log-Based Recovery

• Atomicity: Guarantees that even if a system fails in the middle of a transaction, the
transaction can be rolled back using the log.
• Durability: Ensures that once a transaction is committed, its effects are permanent
and can be reconstructed even after a system failure.
• Efficiency: Since logging typically involves sequential writes, it is generally faster than
random access writes to a database.
Shadow paging - Its Working principle

Shadow Paging is an alternative disk recovery technique to the more common logging
mechanisms. It's particularly suitable for database systems. The fundamental concept behind
shadow paging is to maintain two page tables during the lifetime of a transaction: the current
page table and the shadow page table.
Here's a step-by-step breakdown of the working principle of shadow paging:
Initialization

When the transaction begins, the database system creates a copy of the current page table.
This copy is called the shadow page table.
The actual data pages on disk are not duplicated; only the page table entries are. This means
both the current and shadow page tables point to the same data pages initially.
During Transaction Execution

When a transaction modifies a page for the first time, a copy of the page is made. The current
page table is updated to point to this new page.
Importantly, the shadow page table remains unaltered and continues pointing to the original,
unmodified page.
Any subsequent changes by the same transaction are made to the copied page, and the
current page table continues to point to this copied page.
On Transaction Commit

Once the transaction reaches a commit point, the shadow page table is discarded, and the
current page table becomes the new "truth" for the database state.
The old data pages that were modified during the transaction (and which the shadow page
table pointed to) can be reclaimed.
Recovery after a Crash

If a crash occurs before the transaction commits, recovery is straightforward. Since the
original data pages (those referenced by the shadow page table) were never modified, they
still represent a consistent database state.
The system simply discards the changes made during the transaction (i.e., discards the current
page table) and reverts to the shadow page table.
Recovery with Concurrent Transactions in DBMS

Concurrency control means that multiple transactions can be executed at the same time and then
the interleaved logs occur. But there may be changes in transaction results so maintain the order of
execution of those transactions.

During recovery, it would be very difficult for the recovery system to backtrack all the logs and then
start recovering.
Recovery with concurrent transactions can be done in the following four ways.

Interaction with concurrency control

Transaction rollback

Checkpoints

Restart recovery
INTERACTION WITH CONCURRENCY CONTROL:

This pertains to how the recovery system interacts with the concurrency control component of the
DBMS.

Recovery must be aware of concurrency control mechanisms such as locks to ensure that, during
recovery, operations are redone or undone in a manner consistent with the original schedule of
transactions.

For example, if strict two-phase locking is used, it can simplify the recovery process. Under this
protocol, once a transaction releases its first lock, it cannot acquire any new locks. This ensures that
if a transaction commits, its effects are permanent and won't be overridden by any other transaction
that was running concurrently.

TRANSACTION ROLLBACK

Sometimes, instead of recovering the whole system, it's more efficient to just rollback a particular
transaction that has caused inconsistency or when a deadlock occurs.

When errors are detected during transaction execution (like constraint violations) or if a user issues a
rollback command, the system uses the logs to undo the actions of that specific transaction, ensuring
the database remains consistent.

The system keeps track of all the operations performed by a transaction. If a rollback is necessary,
these operations are reversed using the log records.

CHECKPOINTS

Checkpointing is a technique where the DBMS periodically takes a snapshot of the current state of
the database and writes all changes to the disk. This reduces the amount of work during recovery.

By creating checkpoints, recovery operations don't need to start from the very beginning. Instead,
they can begin from the most recent checkpoint, thereby considerably speeding up the recovery
process.

During the checkpoint, ongoing transactions might be temporarily suspended, or their logs might be
force-written to the stable storage, depending on the implementation.

RESTART RECOVERY

In the case of a system crash, the recovery manager uses the logs to restore the database to the
most recent consistent state. This process is called restart recovery.

Initially, an analysis phase determines the state of all transactions at the time of the crash.
Committed transactions and those that had not started are ignored.
The redo phase then ensures that all logged updates of committed transactions are reflected in the
database. Lastly, the undo phase rolls back the transactions that were active during the crash to
ensure atomicity.

The presence of checkpoints can make the restart recovery process faster since the system can start
the redo phase from the most recent checkpoint rather than from the beginning of the log.

Common questions

Powered by AI

To ensure atomicity and recoverability, a DBMS can use mechanisms such as log-based recovery and shadow paging. Log-based recovery uses transaction logs that store 'before' and 'after' images of data to replay or undo transactions based on their commit status. Shadow paging maintains separate current and shadow page tables to preserve consistent states, reverting to the shadow page table if a transaction fails to commit .

Log-based recovery and shadow paging both ensure database consistency but operate differently. Log-based recovery uses detailed logs to track changes, enabling the system to redo or undo transactions to recover the database to a consistent state. This method is flexible and supports complex transactions but requires careful management of logs. Shadow paging maintains two page tables, copying only when changes occur, allowing quick reversion to a consistent state if a crash happens. It simplifies recovery but can be less efficient with storage as it may involve duplicating data paged during transactions .

Maintaining transactional atomicity is crucial because it ensures that a transaction's operations are either fully completed or not executed at all, preventing partial updates that could lead to data inconsistency. Recovery systems facilitate atomicity through mechanisms like logging and shadow paging. Logging records all changes before they occur, enabling rollback if needed, while shadow paging allows reversion to a pre-transaction state by discarding uncommitted changes .

Shadow paging benefits include straightforward recovery processes since the shadow page table always points to a consistent state, even after failures, allowing quick rollback. However, it challenges database systems with potential storage overhead as each updated page involves creating new copies, affecting performance with frequent page cloning operations during transactions. Furthermore, shadow paging might not handle concurrent transaction recovery as efficiently as log-based methods with robust mechanisms .

Log-based recovery offers several advantages: it ensures atomicity by allowing transactions to be rolled back if a system fails mid-transaction. It also guarantees durability so that once a transaction is committed, its effects are permanent and can be reconstructed even after a system failure. Additionally, log-based recovery is efficient because logging involves sequential writes, which are generally faster than random access writes necessary in other recovery methods .

Shadow paging ensures data consistency by maintaining two page tables: the current and the shadow page table. During transaction execution, changes are made to copies of pages, and the current page table is updated to reflect these changes, while the shadow page table remains unaltered, pointing to the original data. In the event of a crash before the commit, the system can discard the current page table and revert to using the shadow page table, which still points to a consistent database state .

Write-ahead logging (WAL) enhances recovery by ensuring that all changes are logged before being made to the database. This guarantees that the log, which contains all necessary information for recovery, is always available to apply or undo changes, even if a system crash occurs before the database is updated. This approach protects against data loss and maintains atomicity and durability of transactions .

Concurrency control influences recovery mechanisms by managing the order and consistency of transactions. For example, under strict two-phase locking, transactions that have acquired locks ensure that their operations are correctly serialized, simplifying recovery because committed transaction states are assured to be valid. Recovery processes must consider locks during redo/undo operations to avoid conflicts that could arise from improperly applying logs out of the original schedule .

Checkpoints play a crucial role in database recovery by serving as synchronization points between the database and the log. During a checkpoint, all changes made up to that point are written to disk, and a special log entry is made. This allows recovery operations to start from the most recent checkpoint instead of the beginning of the log, reducing the amount of log data to be processed and significantly speeding up the recovery process .

During restart recovery, the database system follows three main steps: 1) Analysis Phase: It assesses the state of all transactions at the time of the crash. Only those that are incomplete or active are considered. 2) Redo Phase: This ensures that all changes made by committed transactions are reflected in the database by replaying the redo log from the last checkpoint. 3) Undo Phase: Transactions that were active at the time of the crash are rolled back to maintain atomicity using the undo log .

You might also like