Introduction to Transactions

This lesson explains the concept of a transaction in the context of databases and distributed systems.

Transaction

Let’s consider the cliche example of a bank transaction. Say Trump is making an online transfer of $100 into Biden’s bank account. To reflect this transaction successfully in both the accounts, the bank software has to deduct $100 from Trump’s account and add $100 to Biden’s account. These are two writes to the database and we want either both of them to succeed or both of them to fail in order to keep the records straight. If one of the writes fails and the other succeeds the bank accounts of the participants will reflect incorrect values.

Transactions are used to address this need. A transaction consists of a group of reads and writes that form a logical unit and are executed as a whole. Either all the operations in the transaction succeed or none are allowed to complete successfully when at least one operation experiences failure. In the case one operation in the transaction fails, the rest are rolled-back or aborted. Transactions provide certain safety guarantees known as ACID. Transactions simplify error handling. The application developer doesn’t need to worry about partial failures and can retry the entire transaction.

Get hands-on with 1200+ tech skills courses.