Getting started with Database Transaction

What is a transaction?

In short, a transaction is a group of database tasks that are treated as a single unit of work. Either you have the transaction or no transaction. Remember the song: “all or nothing” by O-town? That’s the spirit!

What is ACID?

You often hear this database is ACID-compliant, but that database isn’t … so what is ACID?

ACID is the acronym for the four properties of a transaction.

Here are each property and its short description:

Atomicity: a transaction is either complete or fail. If a transaction consists of 10 changes, either all 10 applied or none applied.

Consistency: Data is in a consistent state before and after a transaction according to certain business requirements.

One example of this property is in a warehouse management application, given there were 10 items in stock, 5 were sold then in stock there are 5 items left.

Isolation: Since transactions can happen concurrently. This property ensures that the state of the database is the same as when the transactions are executed sequentially.

Durability: Once the transaction is committed, it will remain committed even in the case of system failure (power outage/ crash…)

An example of a Non-ACID transaction

Let’s consider the classic example in database history: sending money between two people.

Consider Alice and Bob both have bank accounts at Gorilla bank. In the beginning, they both have 1,000 credits (BTC for example).

Suppose Alice wants to send Bob 100 credits, here are the expected steps:

Transaction happy route
Transaction happy route

As you can see, when things work as expected, everybody is happy. Alice got her money sent, Bob got more money and the bank had its reputation increased!

However, what if after subtracting credits from Alice’s account, the bank system suffers a crash? Thus, step 2 was not executed. Bob never gets his deserved money!

Transaction incomplete due to system crash
Transaction incomplete due to system crash

Everybody is unhappy. Bob didn’t get the money. Alice got yelled at by Bob for late payment. The bank got an angry call from Alice.

This situation could’ve been prevented if there was a transaction.

What is so magical about a transaction? What happens if the guy at Gorrila bank wrapped the money transfer step in a transaction?

Bank transfer with a transaction
Bank transfer with a transaction

As you can see, after the crash, the deduction from Alice’s account was rollbacked. Alice wasn’t able to send money to Bob. She was indeed unhappy but not as unhappy as when she got the credit deducted AND Bob didn’t get the money.


In business applications, data is the most precious thing. Handling transactions correctly ensure data’s integrity

Leave a Reply

Your email address will not be published. Required fields are marked *