ACID is a set of guiding principles that ensure databases transactions are processed reliably. Changes made to the database need to be performed with at most care to ensure data doesn’t become corrupt. Applying ACID properties to each modification of data is the best way to maintain accuracy and reliability.
Transaction
A transacting is a unit of work performed within a database. A simple database transaction often contains multiple statements to be executed on the database.
# A transaction where USER_A send amount X to USER_B
BEGIN;
REDUCE X FROM BALANCE OF USER_A
INCREMENT BALANCE OF USER_B BY X
COMMIT;
Atomicity
Atomicity guarantees all operations in a transaction with being treated as a single unit of execution/work. Atomicity means all the operations are performed, or none of them are. If one operation of a transaction doesn’t succeed like it’s supposed to, the other will fail as a result—and vice versa.
Consistency
Consistency ensures transaction brings database from one valid state to another valid state, which means that the data will lose its correctness under no circumstance.
The database allows us to define rules that data residing in the database are mandated to adhere to. These could be like
A bank balance should never go negative
Cannot place an order of Item which is not there in inventory
These rules can be defined on a database using Constraints, Cascades, and Triggers. Consistency means that only data which follows configured rules are permitted to be written to the database. If a transaction occurs and results in data that does not follow the rules imposed on data, it will be Rolled back to a previous state of itself that complies with the rules.
Isolation
Isolation refers to the ability to concurrently process multiple transactions in a way that one does not affect another.
Imagine you and your neighbor are both trying to buy PlayStation 5 (PS5) from the same e-commerce platform at the same time. There are 2 items for sale: your neighbor wants one and you want two. Isolation means that one of those transactions would be completed ahead of the other one. In other words, if your neighbor clicks first, they will get one PS5, and only one PS5 will be remaining in stock. So you will only get to buy on PS5. If you click first, you will get both the PS5, only getting none. Thus, isolation ensures that three items aren’t sold when only two exist.
Durability
Durability ensures that once the transactions commit, the changes survive any outages, crashes, and failures, which means any writes that have gone through as part of the successful transaction should never abruptly vanish. It ensures zero data loss of any transactional data under any circumstance.
How ACID properties are implemented in Databases will discuss in detail in some other letters soon.