Batuhan's Blog | Return to home »

Transaction management and atoms?

September 3rd 2018

I spent my all weekend refactoring a huge function and make its database operations atomic for my job. So I thought maybe it would be nice to write what is transaction management and atomicity.

We can, informally, define Transaction Management as “(probably) the most overlooked part of an application that interacts with a database“.

We could discuss this transaction management thing for hours in depth (in fact, I wish we were talking face-to-face here..) but for now I will only focus on the first letter of “ACID properties” of database management systems. By the way, ACID is an acronym word and no matter if you don’t know this term or you are not a technical person since I will not be posting some alien-ish software developer language here. You will be OK even if you are not a developer.

“A” stands for “atomicity” in this context (Other letters stand for Consistency, Integrity and Durability). As the word itself gives a clue, atomicity means database operations we perform have to be indivisible since we cannot split atoms. Well, actually we can split atoms with today’s tech. Maybe we should rename this ACID thing, right? :)

If we are on the same page, we are good to go.
Question: Why this database operations should be atomic?
Answer: Because we do not want any anomalies in our database
Question: What you mean by anomaly?
Answer: Suppose you are about to buy an airplane ticket. You just picked the flight and made the payment. This magical code has just started to execute:

accept_payment_and_write_it_to_database()
do_something_about_booking_and_write_it_to_database()

First step is executed successfully but second step had some trouble and failed. You faced an error message that says “An error occurred, please try again” and when you check your balance you realised that your money is gone even you couldn’t get your ticket.

Atomicity dictates a database operation have to be all-or-nothing. Either all the operations will be successful or none. If an error occurred in the any part of the operation, rollback mechanism is initiated and all the changes made before the error are reverted.

In Django, a Python framework, we can utilise this atomicity property of database management systems easily. This gist explains how to do it from the official document (You can share your comments with me via twitter/@bakayim)

How it works?

August 31th 2018

Yet another technical micro blog. I will be posting about mostly software development things. Everything will be in the same page for now. Newest posts at the top, scroll down to read olders. No pagination no extra pages. Read N out.