What are the ACID properties in Database Systems?
Published: 2021-07-28 02:36:23

<!-- wp:heading -->
<h2>WHAT IS ACID</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>ACID  stands for Atomicity, Consistency, Isolation, and Durability. It is a set of database properties(rules) which transactions must obey to maintain the integrity and consistency of a database. In this article we'll take a look at each ACID property. <br><br>But first, what is a transaction?  A 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 - <a rel="noreferrer noopener" href="https://www.geeksforgeeks.org/acid-properties-in-dbms/" target="_blank">GeeksForGeeks</a></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Atomicity</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>With the atomicity rule all operations within a transaction must be successful in order for any changes to be commit to the database. If any failure occurs during a transaction then the entire transaction fails. Lets look at an example.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Imagine we have two bank accounts, account A and account B. Account A has $500, while account B has $100. We'll like to transfer $50 from account A to B. The transaction would like the following.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":1316,"sizeSlug":"large","linkDestination":"media"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><a href="https://taslangraham.files.wordpress.com/2021/04/a.png"><img src="https://taslangraham.files.wordpress.com/2021/04/a.png?w=344" alt="" class="wp-image-1316"/></a></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Lets imagine that a failure occurs right after we write the deductions to account A. This would leave account A with $450 but, account B was not updated. So we just magically lost $50. This would corrupt  your data. The property of atomicity prevents this - once there's a failure anywhere in the transaction then the entire transaction fails - and nothing gets committed to the database.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><span style="font-size:revert;color:var(--color-primary-base);font-family:var(--site-font-body);">CONSISTENCY</span> </h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Database consistency must be maintained after each transaction. This means that all changes should be accurate, and leaves the database in a valid state which also upholds data integrity. Lets look at the following example.<br>Imagine we have the following two tables(customers, and orders).</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1325,"sizeSlug":"large","linkDestination":"media"} -->
<figure class="wp-block-image size-large"><a href="https://taslangraham.files.wordpress.com/2021/05/image-3.png"><img src="https://taslangraham.files.wordpress.com/2021/05/image-3.png?w=981" alt="" class="wp-image-1325"/></a></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Both tables are linked via the foreign key constraint based on the <strong>customer_id</strong> on the. This will ensure that upon inserting a new record in the <strong>orders </strong>table, the <strong>customer_id </strong>used is valid (is an <strong>id</strong> of an existing customer record in the <strong>customers </strong>table).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If a transaction inserts a new record in the <strong>orders </strong>table using a <strong>customer_id</strong> (e.g 90) that does not exist then the database would be in an inconsistent state as two. Thankfully our foreign key constraint would prevent this, and the entire transaction would be aborted(including any other changes that may have occurred earlier in the transaction) and consistency would be maintained.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3> <span style="font-size:revert;color:var(--th-color-primary-base);font-family:var(--th-site-font-body);">ISOLATION</span> </h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A transaction being executed should not be affected by any other transactions executing simultaneously, therefore isolated. The result of concurrent transactions should be the same as it would be if the transactions were executed sequentially. Isolation also determines how/if changes in an uncommitted transaction are visible to other transactions or whether different transactions can access the same data at the same time. - this is done via <a href="https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels">isolation levels</a>. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Durability</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Durability ensures that changes committed during a transaction persists permanently(stored in non-volatile memory), even if there happens to be a system failure or a power outage. Lets take a look at an example.<br>Lets says you're transferring funds from account A to account B, and right after completing the transaction the bank has a sudden power outage, the durability property would ensure that the changes made to both accounts are still stored.  </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The ACID properties ensure that we have a database with correct data at any given time. In this article we looked at each of the ACID properties - there impact and importance. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is my first time writing about a database topic, hope you enjoyed it and found it useful. Thank you for reading. Until next time, <em>think, learn, create, repeat!</em></p>
<!-- /wp:paragraph -->