What Is a Transaction?

Learn about database transactions, which are the core guarantee of the database.

A database transaction represents a logical unit of work. It is a sequence of operations executed as a single unit so that the database either executes all the operations successfully or rolls back. A transaction keeps the database state consistent with no partial updates. It also ensures that concurrent updates on the same piece of data do not leave the data in an inconsistent state.

When the database executes all the operations successfully, it is called a commit. Similarly, when the database rolls back all the operations, it is called an abort.

Transactions maintain the integrity of the data in the database and assure developers that they can always safely retry on failures without causing any side effects.

Why is a transaction required?

Let’s consider an example where person A wants to transfer $10 to person B electronically. Let’s assume that the banking software doesn’t use database transactions. The sequence of operations required for this transaction to be successful should be:

Step 1: Debit $10 from person A's account.

Step 2: Credit $10 to person B's account.

Now, let’s say there was a network timeout after Step 1. Person A sees a reduced balance, but person B does not see a balance credit, which is undesirable. This example shows why database transactions are significant in real-world scenarios.

Get hands-on with 1200+ tech skills courses.