<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mwenda Denis on Medium]]></title>
        <description><![CDATA[Stories by Mwenda Denis on Medium]]></description>
        <link>https://medium.com/@mwendadenis?source=rss-bddd8a729cd9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2kK4O8NFojKfpEs_RNJuIg.jpeg</url>
            <title>Stories by Mwenda Denis on Medium</title>
            <link>https://medium.com/@mwendadenis?source=rss-bddd8a729cd9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 01:37:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mwendadenis/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ACID Transactions]]></title>
            <link>https://medium.com/@mwendadenis/acid-transactions-eda7add0963f?source=rss-bddd8a729cd9------2</link>
            <guid isPermaLink="false">https://medium.com/p/eda7add0963f</guid>
            <category><![CDATA[acid]]></category>
            <category><![CDATA[transactions]]></category>
            <dc:creator><![CDATA[Mwenda Denis]]></dc:creator>
            <pubDate>Sat, 16 Jul 2022 08:39:26 GMT</pubDate>
            <atom:updated>2022-07-16T08:39:26.318Z</atom:updated>
            <content:encoded><![CDATA[<p>In every Software Engineer’s development journey, we all get to a point and start wondering about the state of the data in our database or data storage.</p><p>We begin to ask important questions like how consistent is our data (how does a write in one table affect another table and when it does so), or what happens when we are updating data into a database and suddenly there is a failure or power goes off 😬.</p><p>All these are valid questions that any Engineer needs to be able to answer and address, especially while writing code that directly does changes to data in a database.</p><p>This is why we need to understand what ACID Transactions are, and how we can use them in our applications and databases effectively.</p><h4>What is a Transaction? What is ACID?</h4><p>Basically, a transaction in this context is an operation that is treated as a single logical unit of work to access (read or write) data. A transaction can either execute fully and complete, or fail entirely.</p><p>ACID on the other hand is an acronym that can be broken down as in the example image below. <em>(Image courtesy of https://geeksforgeeks.org)</em></p><figure><img alt="ACID Properties" src="https://cdn-images-1.medium.com/max/1024/1*lhPFomlCSNgzVLrJHxjz-g.jpeg" /></figure><p>To further explain this, we will use an example.</p><p>Suppose you are a Software Engineer in a bank. In such a setup, users generally do a lot of transfers from one bank account to another every single minute. We will use this example to further explain ACID transactions.</p><p><strong>A = Atomicity </strong>→ When transferring money from one account to another, we need to deduct money from the source, and add it to the recipient. This means that we have 2 update statements, which we need to treat as a single unit. We are updating the balance of source (by deducting), and also updating the balance of recipient (by adding). Either the entire update statement is executed, or none of them is executed. If money is deducted from one account and it fails to be added to the other account, data will be left in an inconsistent state.</p><p><strong>C = Consistency</strong> → We should ensure that the integrity of the database constraints is maintained, both before and after the Transaction has taken place. We need to ensure that a transaction makes changes to a database table in a predictable and predefined way. This however is mostly enforced by a developer especially while creating a database schema. For instance, we can define in a database schema that a user’s balance should never be below zero meaning we should never have a negative value as a user’s balance. Using our example above, if for instance a user wants to transfer $500 to another user yet their balance is $200, we abort that transaction since that would leave a negative value of -$300 as the sender’s balance, which would make our data inconsistent.</p><p><strong>I = Isolation</strong> → Using our example above, suppose four friends all decide to send money to one user at the same exact time. That means the application will be reading and writing data on the database table all at the same time. This means we need to isolate these transactions so that their concurrency does not affect or interfere with one another. Even though these transactions are occurring simultaneously, the final result should be as if the transactions happened one by one. For example if the four friends decided to each send a user $100, and the user’s balance is $200, then after the transaction, the user’s balance should be $600, even though they all sent the money in at the same exact time.</p><p><strong>D = Durability</strong> → When you hear of Durability, think lock and key 🔐. Using the example above, once, say friends have sent a user money and the transactions have happened successfully, then the changes to that data in the database should be saved, even incase a system failure. The changes should be written and saved(persisted) to disk.</p><h4>Why are ACID Transactions important?</h4><p>ACID transactions are very important for data integrity and consistency in our databases. They ensure that our databases do not fall to inconsistent states because of transactions that execute partially or fail at specific points.</p><p>Think of ACID transactions as a version control system, but for data 😊</p><h4>When do we NOT need ACID Transactions?</h4><p>As interesting and as important as ACID transactions are, they may not be required in every application use case. However, it is important to ask yourself a few of these questions before deciding that they are not useful in your application.</p><ol><li>If x amount of users read and write data at the same time to the same table, do you want to end up with the same result?</li><li>Does your application use case require that data changes be done in a specific order?</li><li>Are you okay with losing any set of data within the application use case?</li></ol><p>If you answered Yes to the first two questions and No the last one, then you really need ACID transactions.</p><p>For number three, some application use cases, example Machine Learning applications, you may be okay with eventual consistency, meaning you may be okay with loosing at least a portion of relevant data while training a model, hence ACID transactions may not really be useful in such a use case.</p><h4>Conclusion</h4><p>In this article, we looked at ACID transactions, and their importances, as well as situations when you may not need them in your application.</p><p>In the next tutorial, we will dive in and see how we can achieve ACID transactions in a NoSQL database (MongoDB).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eda7add0963f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>