Database Transaction and ACID Revisit

Transaction

A transaction is a coherent and reliable unit of work performed on a database and should be independent of other transactions. The purpose of transaction abstraction is to deal with two problems

  1. To provide a reliable way to recover from error and keep system integrity in case of system fault.
  2. To provide isolation among programmers accessing database concurrently. Without the isolation, concurrency usually cause erroneous, sometimes indeterministic outcome.

Atomicity

Atomicity is designed for error handling purpose. Atomicity guarantees that one transaction can be fully committed, aborted or roll backed. In other words, the process of a transaction can not be partially accomplished. Additionally, atomicity also make sure that system can roll back a transaction without side-effect, which can be considered as abortability. If the execution of a transaction has some side effect, such as sending external request to downstream application and cannot be reverted, the transaction is not considered as atomic since it does not guarantee abortability.

Consistency

In the context of ACID, consistency makes sure that a transaction can only bring database from one valid state to another, maintaining database invariant.The legitimacy of data is usually not a database concern, which is usually defined by the application logic. In other words, consistency should be considered as a handy feature offered by some databases rather than a hard requirement for database transaction.

Isolation

The isolation property guarantees that the processing of a transaction will not be interrupted by other transactions. With strong isolation level, every transaction can consider itself as the only one in the system. In other words, each transaction is independent and even invisible to others. If there are multiple transactions running concurrently, database system should handle it as they are running sequentially. This isolation level is called as serializability, which is usually considered as the strongest isolation level. However, serializability comes with expensive performance penalty, therefore some system also provides weak isolation level to achieve better performance. Some weak isolation levels are read committed, snapshot isolation.

Durability

The durability means committed transactions will survive permanently. We can consider it as a guarantee against fault tolerance. We can consider it from two perspective. From hardware perspective, it usually means the the transaction has been stored on a persistent storage, such hard disk. On the other hand, people looks at the durability problem from a system perspective and claim the transaction is durable if it is properly replicated.

Example

Let us review these concepts through a real life example. In a banking application, a common use case is fund transfer. Assuming Bob wants to transfer $100 from his checking account to his saving account. Before the transfer, there are $500 balance in his checking account and $300 in saving account. After he issues the fund transfer in the portal, there is a transaction issue to underlying database. There is a fund subtraction from checking account and a fund addition to the saving account.

  1. The execution is not in the context of database and therefore it is not the semantic of database transaction.
  2. Even we extent the transaction to overall banking system, the SMS and notification can not be retracted easily. Therefore it breaks the abortability.

What is next

This wraps up our initial discussion about ACID. Since 2000s, large scale distributed database gains more popularity among the industry. It is not easy to achieve ACID with performance in consideration. Therefore some relaxed alternatives have been proposed. In the next article, we will discuss some other popular concepts in the distributed data system community.

Reference

  1. Designing Data-Intensive Application Martin Kleppmann’s, O’Relly, 2017

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store