Database transactions using ACID properties

We process huge database transactions simultaneously. Using ACID properties, we ensure that the correctness and consistency of a database in a way such that each transaction is a group of operations that acts a single unit, produces consistent results, acts in isolation from other operations and updates that it makes are durably stored.

A database transaction is a single logical unit of work which accesses and possibly modifies the contents of a database. Transactions access data using read and write operations. In order to maintain consistency in a database, before and after transaction, ACID properties are followed.



Atomicity: In a transaction involving two or more discrete pieces of information, either all of the pieces are committed or none are committed. Let us take an example. We are trying to save/commit 7 customer address fields as part of a database transaction. Going with atomicity rule, if one of the addresses fields are not committed/saved the way, it was supposed to, all the 7 customer field commits will not be saved. That means, database commit transactions happen either in "all" or "nothing" fashion.

Consistency: A transaction either creates a new and valid state of data, or, if any failure occurs, returns all data to its state before the transaction was started. Let us take a parent state table "Sate_Master" that consists of value ("AP", "TN", "TS", "KA", "UP","MH"). We have another customer table whose column "Customer_State" has a referential integrity with "State_Master" table. That means, this column can only allow values that are available in its parent table "State_Master". As part of a database transaction, if we intend to save "CZ" in the "Customer_State" column for a specific customer, going with consistency rule, the database commit/save will be failed as "CZ" is not a valid state (because "CZ" value is not there in "State_Master" table).

ISOLATED: For a database, isolation refers to the ability to concurrently process multiple transactions in a way that one does not affect another. Incredibly huge amount of database transactions are occurring simultaneously. Let us suppose user_A and user_B want to write to same customer record of the same table almost at the same time ( It's just that user_A tried to access this customer record little ahead than user_B) Going with ISOLATED principle, database will give user_A the write access first making user_B to wait during this time. This way, ISOLATED principle ensures database integrity.

Let me explain further on this. Imagine you and your neighbor are both trying to buy something from the same e-commerce platform at the same time. There are 10 items for sale: your neighbor wants five and you want six. Isolation means that one of those transactions would be completed ahead of the other one. In other words, if your neighbor clicked first, they will get five items, and only five items will be remaining in stock. So you will only get to buy five items. If you clicked first, you will get the six items you want, and they will only get four. Thus, isolation ensures that eleven items aren’t sold when only ten exist.

DURABLE: Committed data is saved by the system such that, even in the event of a failure and system restart, the data is available in its correct state.

The ACID properties, in totality, provide a mechanism to ensure correctness and consistency of a database in a way such that each transaction is a group of operations that acts a single unit, produces consistent results, acts in isolation from other operations and updates that it makes are durably stored.