Orientation with Kojo Idrissa
Published October 26, 2026
This video features Eva Nanyonga at DjangoCon US 2024 in Durham, North Carolina, USA.
Database transactions are incredibly important for the reliability of financial operations. The reliability and accuracy of these tarnsactions hinge upon robust database integrity measures. This presentation will explore key concepts essential to maintaining database integrity within financial ledger environments using the Django Framework.
Attendees will gain insights into the following aspects:
Transaction Atomicity: Understanding how atomic database transactions are performed in Django to ensure data consistency.
Concurrency Control: Using the Django ORM to manage concurrent transactions to safeguard data against corruption.
Durability through Logging: Performing transaction logging to ensuring help teams recover from failures.
This talk will address real-world challenges and considerations in implementing and maintaining database integrity using the Django framework. Practical examples and case studies will be shared to illustrate the application of database integrity with the help of the Django ORM.
Whether the attendee is a database administrator or a developer, this session will provide valuable insights into the foundational principles and strategies for upholding database integrity in critical financial environments.
This talk was presented at: https://2024.djangocon.us/
LINKS:
Follow Eva Nanyonga 👇
On X: https://x.com/
Website: https://www.linkedin.com/
Reliable data depends on database atomicity: each related operation must either complete as one unit or leave no partial results. Using a Django REST Framework account-creation example, Eva Nanyonga shows how sending an email after saving an account can fail, leaving a database record even though the user was not notified and may retry, creating duplicates. Wrapping the operation in `transaction.atomic()`—or using Django’s `@transaction.atomic` decorator—causes the database write to roll back when the email step fails, while clearer exception reporting helps identify configuration problems such as a refused SMTP connection. She explains that the same approach is essential for money transfers, payments, orders, telecommunications, ticketing, supply chains, and medical records, where partial updates can produce misleading or unsafe data. In response to a question about retries, she suggests that a script could rerun failed transactions, while noting that retry behaviour needs to account for recurring failures such as a server outage.
Summarised automatically from the transcript.
Atomicity means a transaction either completes all of its operations as one unit or none of them take effect. It prevents partial results and helps keep data consistent and uncorrupted when an operation fails.
Discussed at 1:07Improve error reporting by catching and identifying the error type; in the example, this exposed a connection-refused error caused by incorrect email/SMTP configuration. Once the configuration was fixed, the transaction completed successfully.
Discussed at 8:08Wrap the operation in Django’s `transaction.atomic()` context manager so that a failure rolls back the database changes instead of leaving a partial record. Django also supports applying `@transaction.atomic` as a decorator to a function.
Discussed at 9:41They are useful anywhere several related changes must succeed together, including e-commerce orders, telecommunications, ticketing, supply chains, and electronic medical records. For example, if creating a patient succeeds but attaching a prescription fails, the whole operation should be rolled back.
Discussed at 12:03Automatically transcribed, so expect mistakes in names and technical terms.
Speaker 1
All right, thank you. My name is Eva, like I've been introduced. And today I'll be talking about how to keep our data reliable using Django. We'll talk about atomicity, which is basically at the base of having reliable data and how we can perform atomic transactions in Django And then we will also see how to report failures in these transactions to help us both debug and also make sure that our transactions
Are reliable. Postgres, one of the D B engines, defines Atomicity as the property of a transaction that either all its operations complete in a single unit or nando. In addition, if a system failure occurs during the execution of a transaction, no partial results are visible after recovery. And this is one of the acid properties of a database. Basically, the database to keep the database reliable, we need to make sure that we apply atomicity to it.
And some would even argue that atomicity even helps all the other properties in the acid. acronym because I mean to keep it consistent you really need it to perform all the transactions either all at once or you know fail if there's an error, an execution error that has occurred. So I think atomicity is really important, but some of sometimes we as developers overlook it Maybe database engineers, you find that database engineers
put it at the heart of their operations because they want to have data that is not corrupted, data that can be reported on so we also need to make sure that we make it easy for the database engineers So how do we perform atomic transactions in Django? I used an example of a some APIs. And what you see there is the user, creation of an account, a top-up and a debit.
I'll probably use the account creation to demonstrate atomicity, but That doesn't mean that you can't apply automicity to all the other APIs on the list there. So it can cover pretty much all things that are listed there. So when I run my API for creating a transaction, as you saw In the first screenshot, I ran an API using Postman and I have a model that describes
the accountants, the account name, the number, the balance, the status of that account, and who created that account. And so After I run the creation of that API, you see the database shows that we have a record in there. I first ran I first checked the database before I ran the creation and then I checked the database after so you can see the difference. So right now I'm gonna override
I used rest framework Django Rest framework to create this API. And right now that is me trying to over overwrite the perform create A function within Django Rest framework so that I can do something else with my API. For example, I want to send out an email after I've created an account to show someone that they have an account with our company. So I go ahead and create the account and then try to send an email.
What happens when I try to do that? So what happens is I get an error, but I'm not sure what the error is really about. I mean I read down below it reads There's an attribute error, but it's not very clear. But what do you see here? There was a record that was made to the database. Another record is there as stella. This is this is a second creation that I'm doing So a record has been made, but I have an error.
So it means the email was not sent, but We have another user. But so it so happens that maybe an another user won't know that, you know, an account was actually created And they'll try again. So what happens is I introduce some error reporting And because I already know, I create a new account called with another name, as you can see. But you can see that
before that I tried again and it created another record with the same name. And the same account. This is this is not good data. It means there are two records that are the same. And I mean what happens if For example, you're going to put money on that account. Which one are you going to choose? It becomes a big problem for anyone that's using the system. Especially the data guys, when they're trying to find how much money you have, this is going to be a big problem.
So the new error that I have is that the code is invalid and this is because I introduced some error reporting. Well, it is a good error, but I'm not too sure yet. It doesn't have everything. So I introduce even better error reporting. I Try to find out that try to find out which type of error it is right down there by wrapping the error in a type. So what happens then? What happens is I'll get a much better error, a connection refused error.
And this means that my email was not configured well. So what do I do? I try to configure my email and everything is, you know Going on well. So what happens then? So as you can see right here I there was an error. I tried to I tried to create an account and well it wasn't created but we still had
If you if you look at If you look at the errors that are introduced and the atomic the atomic transaction that are introduced with atomic here. Under the account creation, you can see after the perform creation, I use the with transaction atomic as Django does it to Introduced atomicity to this function. This means the function, when the function fails, a record will not go to the database.
So then when that error is reported, I get a new database That shows me, if you see the database right now, we jump from five to eleven. It shows all the number of times that I tried. There was no record then But after successfully configuring my email client, my SMTP email client, I managed to get a new record in. And I can comfortably say that we have demonstrated how atomic transactions work If you
want to maybe perform more atomic transactions You can use decorators. Django gives you another way to perform atomic transactions using decorators. It would have an art and just transaction the atomic just as just as it is with transaction. atomic, you just decorate your function right there with the at decorator at transaction. atomic So we go then to some of the most common use cases of
Atomic transactions. I would argue that almost all systems require atomic transactions, but they are most crucial in financial transactions. For example banking you want to make payments or in accounting systems These transactions, it's very important that you record for example, you're transferring money from one account to another. What if something fails within the middle? You need to find a way to roll back everything as to
have the integrity of the database intact. Otherwise you will have issues with one bank showing a balance that doesn't exist in another And we have e-commerce systems. It it's used in Order management, order when you're making orders on your e-commerce systems, this is one other use case that's really important. We can see Transactions being used in telecommunication systems, online ticketing, supply chain management systems, and
so on and so forth. So I would say that atomic transactions or atomicity of databases is really, really important. And because Django automatically commits a transaction when it started we do not we do not know if when one of them fails if we are going to get reliable data. So we need to make sure that the atom the transaction is actually atomic What I mean here is that when you run an SQL command, say of create of
Update or add To the database, it automatically saves that transaction. Django automatically saves that transaction. So if there were say two transactions of maybe using someone's you know financial wallet system and then updating a balance after that It means if the there is a failure on updating the balance, there is going to be a problem where you have more money in the account than the person is supposed to have. So atomicity is really important.
Yeah, I think that's pretty much it about my talk today. I don't know if there are any questions or
Speaker 2
Now we have time for questions. Just raise your hand. I'm going to get your the microphone to you
Speaker 3
Thank you, Eva. If you go back to the previous slide, you've got a list of common use cases. And They all seem to be about transactions that kind of involve money at one level or goods at one level or another.
Speaker 1
Right.
Speaker 3
This also applied to cases where things are moving in a system, for example, I don't know, medications in a drugs delivery system, in a in a medical equipment or in where it's robotics for example
Speaker 1
Yeah, absolutely. Yeah, that's true. It applies to pretty much everything. For example, an electronic medical record You want someone to get a prescription, but you haven't one of the transactions hasn't recorded So you need to roll back. If there was if the prescription did not go through, you need to roll back the entire transaction. The patient, you try to get a patient to get a particular subsc prescription rather So you managed to create a patient, for example, but the prescription was not attached.
You need to roll back the entire thing. Because then you will have you will think that you sold something that you didn't. And you might think that this patient got this medication, but well they did, they could physically get it. But you don't know when you check their records for example to see maybe if their if an allergy was registered on this patient, you may not find the record of that particular prescription medication. So I think it's pretty important.
Speaker 4
Hi, do you have any practices or recommendations around retrying transactions? Like in your in your example, there was just like a misconfiguration in the SMTP server, but what if there's sort of a Some kind of a state causing a failure or just in a you know, there's no not necessarily like a really obvious bug in the code or configuration like that, but it's just in running in production something happens and then you've got you've got a I don't know, maybe you've got 150 transactions that fail, then you don't have a that you don't have like a mismatching data in your database, but maybe you've got you have to do something about that situation.
Speaker 1
You mean like run the transaction again, for example?
Speaker 4
Yeah, I mean can you just kind of speak to different options ways that you might think about those kind of kinds of problems
Speaker 1
I haven't thought about that but a solution I could think of right now is Maybe having a script in case something like that happens where there's a failure to run the transaction again. I mean if it's just Obviously, you'll notice if it happens again, maybe the server went down. You notice that the script maybe is overrunning. I don't know if that makes sense. Thank you.
Speaker 2
Any other questions?
September 22 - 27, 2024
Durham, North Carolina, USA
Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026