Databases are only as good as their data is consistent and reliable. So developers created a set of principles known as ACID: Atomicity, Consistency, Isolation and Durability. You’ll see ACID invoked in everything from e-commerce platforms to financial systems, and if you’re going to be delving into transactional databases in a major way, it is crucial to have a comprehensive understanding of ACID.
Atomicity refers to treating each database transaction as an indivisible unit. That means that if there’s a failure, a transaction is either fully completed or fully rolled back, without any partial effects. This is to ensure the database remains in a consistent state, even if there are system failures or errors.
So if a bank customer transfers money from one account to another, atomicity means that if the debit operation succeeds but the credit operation fails because of a technical issue, the entire transaction is rolled back, leaving both accounts unchanged.
Consistency means that a database transitions from one valid state to another valid state after each transaction. A transaction’s execution is supposed to adhere to the predefined rules and constraints of the database. If a transaction breaks any of the rules, it’s aborted. The database remains unchanged.
Let’s say an online booking system has a user who tries to book a flight seat already reserved by another user. Consistency would prevent such conflicts.
Isolation means multiple transactions can take place concurrently. Transactions are executed in a way that they appear to be executed sequentially, even though they might be running at the exact same time. Isolation prevents interference between transactions.
If, for example, two users simultaneously attempt to book the same plane seat, isolation means that the database handles their requests one after the other, preventing race conditions and ensuring accurate updates to the data.
Durability means that once a transaction is committed, its effects are permanent and will survive system failures. To achieve this, developers use techniques like write-ahead logging, where changes made during a transaction are recorded in a log before being applied to the database. If there’s a crash, the database can use the log to recover the state before the crash.
Let’s say there’s an e-commerce platform that confirms an order and deducts the corresponding amount from the user’s account. Durability means that this deduction remains intact, even if the system crashes immediately after the transaction.
How does ACID help you develop transactional databases?
ACID forms the basis for transaction management, guaranteeing data integrity, consistency and reliability. But it’s important to note that though ACID properties are powerful, they can sometimes affect performance because they put constraints on concurrent execution. In certain cases, developers may choose slightly relaxed consistency models like BASE (Basically Available, Soft state, Eventually consistent) in distributed systems where real-time consistency is not always possible.