Transcript for:
Database Transactions & ACID

Hello friends and welcome back to study tonight in this video I'll be talking about some of the useful database transaction examples that will help you understand what database transactions are although that is something that I've already covered in a previous video that I created explaining database transactions so if you haven't seen that video go check that video out first and then come back to this video because in this video I'll cover few examples to help you understand the Practical usage of database transactions I've seen a lot of videos and I've mentioned in the last video as well a lot of creators on YouTube have mentioned that database transaction is rather a theoretical concept not a practical concept and I strongly differ maybe you know they have not worked on some practical application and they don't think then database transactions are that useful but in every programming language you'll see when the programming language interacts with the database there are features that you can use to enable database transactions and use them whenever you are interacting with a database so in this video I'll give you a couple of examples where you should consider using database transactions where you should ignore it when you are you know developing any application so let's get started so as you guys might already know if you have seen the last video that I created if you have not seen you know please go ahead and see that video first so a database transaction follows you know basic properties which are termed as acid not the asset you know the chemical one but you know the property is a combination of four properties which is atomicity constraint isolation and durability so I will not get into the details of all these properties but let me explain you uh you know what these properties mean so atomicity means that okay you know every uh you know database transaction when executed you know either everything passes or everything fails there is no in between state for a database transaction constraint means you know whenever you execute something in a database transaction all the SQL queries must respect the constraints that are provided in the table right unique constraints primary key constraint foreign queries inside a database transaction though those must be executed in complete isolation for example if there are two users interacting with the same application at the same time add you know performing the same operation so obviously you know the same uh backend calls would be fired from the application right if two users are using the same application at the same time and there is a common database right so the application would be interacting with the database to perform the same particular thing right maybe you know it can be uh two users are trying to purchase the same product and if for the product just a single unit is left then only one of them can buy right if the execution of the database queries happen at the same time maybe the application will give back a successful order completion for to both the users which is something wrong because we just had one unit of the product so that's why isolation comes into picture with database transactions so for such scenarios in real world applications we should definitely use database transaction because isolation is a key feature that database transaction provides which is super useful when it comes to so you know real world applications that we create e-commerce website social networking websites Etc durability every change that has been made uh through SQL queries or you know through interaction with the database under a transaction must be permanent so these are you know basic properties that uh a transaction implements or the transaction brings in so if you are able to implement all these four properties you know you are able to implement a transaction so let's see uh the various scenarios where we need database transactions before moving on to the scenarios where we need database transactions let's cover the scenarios where we don't need a database transaction so it's not necessary that for every a particular you know interaction with the database your application must follow the standards of transaction it's not necessary right and in different programming language it can be different the implementation can be different so I'll give you a basic example of how you can do it and you know whatever programming language you use you can see in that particular programming language how you can Implement transactions right so if your application is performing basically read operations on the database tables for example basic you know select queries are being filed even if you are firing a single a select query or you are firing you know 10 or let's say 100 select queries you don't have to worry about database transactions because these select queries would not change the data inside the database tables it will just read right so n number of queries can be filed together you know a thousand uh users can parallely read the same data if they are not affecting the state of the data if they're not updating the data reading the data is totally fine you don't need database transactions for that right so if your application is performing basic read operations ignore transactions also if you know for some case so apart from the isolation case if you know you have an application which is just performing so if you have a table and in that table one insert is being done right and it's in harmless insert by harmless insert I mean you know if for example a user is signing up for your application education and you are saving the user data into the user table right now for that particular user you'll be saving data into one single table and if you're performing a single insert then again you don't have to worry about database transactions because multiple uh changes in the database data is not happening only a single change is happening so you don't have to worry even if that particular change fails even if that particular insert fails then also it would be okay you know this particular single insert fails you can simply tell the user okay you know oops you know some error happened please try again Etc you should not worry about uh you know the intermediate or inconsistent state of the data because it was a single insert that was being done and that failed that's totally fine similarity for update and delete as well until and unless you know these are not affecting this Isolation part so for example like I said for a e-commerce website now you have to be a little careful when deciding whether you want to use database transactions not so your decision of deciding whether you know you want you need data based transactions or you don't cannot just rely on these particular points right you have to be a little extra cautious for example I'm saying you know in single data operation query you don't need database transaction but that can not be always true right for example if if we have an Ecom website where we have an admin panel who is uh you know who can monitor and update different stuff on the Ecom website and there is a general user who can visit dcom website and maybe you know purchase some product Etc now if there is some product available right and the user can purchase it and the admin can update you know where maybe the stock for this maybe the pricing for this Etc and the admin decides okay you know post 12 p.m uh we will update the price of this particular product and we'll offer 50 discount right now if the user is also interacting with the same model into which you know the admin might be performing some update whether it may be you know a single update but it is affecting the state of a entity which is also being accessed by someone else so for thus such cases as well you know maybe the user is trying to buy a product maybe the admin is also trying to make updates to it so such updates even if single should be done in transaction because when a transaction starts to execute it you know takes a lock on the data that it is you know affecting so if some other SQL query is trying to affect that particular data you know the database will return an error it will say okay you know some existing operation is ongoing on this particular data and you know you know the other query maybe come back later and work on the same data so isolation is a very important aspect of database transactions that you should always keep in your mind whenever you are you know creating some small or large application if you are you know working at a product based company or a Services based company where you interact with database where you write queries or features which are interacting with the database so these are things that you know you should always keep in your mind so now this was that now let's come to the cases where we need uh which is obvious you know we need database transactions right so these are the cases where you need database transaction and these are the most obvious cases right like I said some cases in which I am saying that you don't need even then you can have a situation where you might need database transactions based on your applications requirement and I've explained when that can happen so you have to think that way you know when you read a database transaction and when you don't right so when you have a mixed bag of query so these are the most obvious cases where you should definitely go for database transaction for example if your forming some insert some update or you know some deletion on some data so in our case you know in case of study tonight.com so we have a small flow where a user purchases a course so we have some interactive courses available on the website which a user can purchase when a user purchases a course what happens is there are certain coins associated with a user account that are stored in let's say a user Point table right inside this we have certain number of points that a user might have then there is a course enrollment table in which you will keep whether you know which user is enrolled and which user is not enrolled and then there are other helper tables like you know Point Ledger in which we keep uh you know how many points are credited or debited from a user account Etc so when a user enrolls for a course what happens is from the user Point table we deduct the 5000 points so an update happens over here right and then if the number of coins are available and they're deducted successfully the course enrollment so an insert happens over here right so the course enrollment is done and then in the point Ledger also we insert a new entry of you know debiting of these 5000 coins right so now a group of application queries are happening update is happening then an insert is happening even though they're happening on different tables and another insert is happening so for such cases uh you know we should definitely use database transactions and all these things should be performing database transactions because even if a single one fails we should fail the entire process that's the requirement we cannot do like you know 5000 points are deducted over here the user is enrolled in the course right but Point Ledger entry fails so then our Point Ledger will have inconsistent data or maybe you know this fails the point deduction fails but the user is still enrolled in the course and this also passes so then you know without even deducting the required coins we are enrolling a user into the database so this is an example right this is how you figure out where you need database transaction now we have a couple of uh you know functions in our website as well on straightway.com where we do not use a database transactions at all but there are some important uh you know steps like this one where course enrollment is happening or where you know some very important data is being updated or modified in multiple tables in one single go in one single function call so that's where you know we should be very careful and we should definitely use transactions right so we use PHP in the back end in PHP you have you know now functions that you can use with mySQL and you can begin the database transaction and once you are done you can execute it right you can commit it sorry and in case you get some error you can you know call the rollback function in every change will be roll back so that's the basic you know uh feature in almost all the programming language yes different Frameworks provide different ways of executing transactions but it should be like this you know you begin a transaction then you perform all your SQL queries right if everything goes well you know if everything passes then you commit unless until anything fails you roll back all the changes right so that's how a basic database digestion flow happens and I've already covered the example for isolation so isolation is very important uh this is again use case based but I think I have explained what use cases require isolation and different programming languages have different implementation but this should be the basic flow right so that's how you should be focusing on implementing transactions in your application when you are interacting with database I hope this video makes sense I hope you are able to understand where you can use transactions and where you should seriously consider using transactions if you like this video give it a thumbs up uh like I said in the beginning also I have 80 000 subscribers but very less people watch the videos I'm not able to understand maybe you know I'm not able to figure out what my subscribers want from me so you know you can also tell me what sort of content you would want me to make and I'll be very happy to create that sort of content I'm trying a lot of things out uh you know maybe you know I'll figure it out my self but yes if you like this video just give it a thumbs up share it with your friends if you like such videos then subscribe to our Channel if you haven't subscribed already press the Bell icon please please press the Bell icon so that you get notified whenever I post a video I am trying to be regular on updating you know YouTube videos posting more videos and shots as well yeah that's about it for this video uh I hope you know database transactions concept becomes a bit clearer to you after watching this video thank you for your time and see you in the next video [Music] [Music] thank you [Music] [Applause] [Music]