‘Rollback’ is a term used in computing, particularly in the context of databases and transaction management systems, which refers to the process of rolling back a set of operations to a previous state. This is done to ensure the integrity and consistency of the data in the event of an error, failure or any other situation that requires operations to be rolled back.
If an error occurs during a transaction, a rollback allows all operations carried out up to that point to be reversed to correct this error and maintain the original consistency of the data.
Rollback features
Some key features of the rollback include:
- Atomicity: Rollback ensures that transactions are atomic; in other words, either all operations within a transaction are completed, or none of them are.
- Consistency: Ensures that the system returns to a consistent state following a failed transaction, thereby preventing data corruption.
- Insulation: The effects of a transaction are not visible to other transactions until the transaction is complete. If a rollback is performed, the changes made in that transaction will not be visible to other ongoing transactions.
- Durability: Although the rollback itself reverses the changes, the rollback process is designed to be durable, so that the changes are not applied and the system returns to a safe and consistent state.
Applications of rollback
Rollback has the following applications:
- Financial system: A rollback is the mechanism that ensures a transaction is atomic. If an error occurs whilst a banking transaction is being carried out, a rollback allows us to reverse all the changes made up to that point.
- Databases: In relational database systems, a rollback is an essential operation for handling failed transactions. For example, if a transaction involving multiple insert, update or delete operations fails partway through, the system can revert all the operations carried out up to that point.
- Version control systems: In software development, version control systems such as Git allow you to perform rollbacks to revert changes and return the code to a previous state.
- Software update: When managing system and application updates, a rollback allows you to revert a failed update to a previous version that was working correctly.
Example of a rollback statement in SQL
Let’s suppose we have a database with two simple tables: customers and transactions. We want to update a customer’s balance and record a transaction. However, if something goes wrong during the process, we need to revert all the changes made.
Table structure
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(100),
balance DECIMAL(10, 2)
);
CREATE TABLE transactions (
transaction_id INT PRIMARY KEY,
customer_id INT,
amount DECIMAL(10, 2),
date DATE
);
- We are starting the transaction
BEGIN TRANSACTION;
- We carried out an operation
— Let’s suppose that the customer with ID 1 makes a deposit of 200
UPDATE customers
SET balance = balance + 200
WHERE customer_id = 1;
— We insert the transaction record
INSERT INTO transactions (transaction_id, customer_id, amount, date)
VALUES (1, 1, 200, GETDATE());
- We make a mistake and perform a rollback
IF @@ERROR != 0
BEGIN
— If an error occurs, we roll back all transactions
ROLLBACK TRANSACTION;
PRINT ‘Transaction failed, changes reversed.’;
END
ELSE
BEGIN
— If there are no errors, we will confirm the transaction
COMMIT TRANSACTION;
PRINT ‘Transaction completed successfully.’;
END