Rollback

Table of contents

Summarise with:

‘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

);

  1. We are starting the transaction

BEGIN TRANSACTION;

  1. 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());

  1. 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

Share in:

Related articles

Firewall

A firewall is an essential tool in computer security that acts as a barrier between a private network and the vast world of the internet. Its main function is to protect an organisation's or user's systems and data, acting as a gatekeeper.

Insight

By insight we mean a deep and clear understanding of a situation, behaviour or problem that is not immediately obvious and that may reveal significant patterns, trends or relationships of interest. They are critical as they allow us to make more strategic decisions when making decisions.

Malware

Although in popular parlance viruses is the umbrella term for any type of cybernetic software that threatens the security of a computer system, the reality is that the correct term is malware. A malware is a programme or application that is

Variable

In computing, a variable is a space in the computer's memory that is used to store a value. This value can be of any type, such as a number, a text string, a Boolean or an object. If you are wondering what is

Scroll to Top