Only reliable Data: Protecting Database Integrity with Eva Nanyonga
Transcript
By Speaker 1, Speaker 2, Speaker 3, Speaker 4. From DjangoCon US 2024. Published December 6, 2024.
Watch the video Markdown version 2,163 words · auto-generated · en
Summary
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.
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
0:00 Introduction to Reliable Data
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
1:07 Atomicity and ACID
Speaker 1
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
2:46 Atomic Transactions in Django
Speaker 1
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
5:04 Partial Failures and Duplicate Records
Speaker 1
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.
8:41 Error Reporting and Transaction Rollbacks
Speaker 1
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
11:16 The Atomic Decorator
Speaker 1
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
12:03 Atomicity Use Cases
Speaker 1
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
13:33 Django Autocommit Risks
Speaker 1
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
15:29 Questions
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?