what is OPTIMISTIC LOCK?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so there are two kinds of blocks one is pessimistic and the other one is optimistic so both are pretty famous and both have been used in a lot of different applications so first you need to understand the meaning of pessimistic and optimistic to better understand when we are going to use pessimistic law and when optimistic lock is used so pessimistic the meaning of pessimistic means it is the feeling of something worst is going to happen and the meaning of optimistic is that you always like hopeful that something that is not going to happen or some good thing might happen so some sticky lock is the log which most of the people knows and have used so this is the most common commonly used locking method to ensure the data integrity when there are concurrent operations are happening on that data so I just give an example on the database we basically put a lock so that only the transaction can only update that particular row not anyone else that's the kind of lock which is actually the best mr. clock so it's the usual way it works is you basically acquire the lock and then you do some work and then you basically release still long okay so who has acquired lock he has got a hold of that particular data to do any modifications read and write and update so apart from this particular guy who has got the lock and no other threads or processes or any conscience cannot do any modification to that particular data until this this guy releases the lock and someone else acquires it so this is the usual lock which everyone knows that's what we actually call it has dismissed it lock so let's learn about optimistic in lock as the name itself says it always think all the good thing is going to happen so what is the definition of miss o'clock it is the strategy in which you basically read a record and also record the timestamp protection or version or hash and you check the version or all of this information or any one of the information is same before you write it so if the information is then you can update the record if it is different then you should update you just need to discard that red card so that's how the optimistic lock works and there are so many advantages of optimistic lock and they are supposed to be used in only some places not everywhere I'm going to explain all of that later so before that I am going to take an example to explain how the optimist did you lock work okay so basically I have a table okay in this I have three columns wise ie one is data and one is timestamp chips and ocean on hash now I'm just going to pick devotion just for the simplicity of it okay two people what is this guy they are the one is so this guy they both want to modify the data in this table fine okay now let's take a positive case or a happy scenario in this case so this guy I want to modify the data which is in the table with ID 3 so what he wants to do is you want to modify this data 13 to something so okay so let's also have a different question okay let's have one one okay because it's let's update the question to one one one okay so now this guy want to modify this recovered 32 something else now he maybe he wants to add +1 to this type this data so what it does is he just comes here and look up to the row which has ID 3 so he found out this one he should now read the data and the worship okay now what he does is he reads the data and also he meets the portion 13 is the data and the version is one although he does some modification what it does it wanted to increment the leader so the new value is 14 and the worship he saw when he was reading the record with IE 3 was 1 so he has this data and he should go back and then update the data with a you know roll with I 83 so what he had to do is first you to check the version the version which he has and the version which is data in the database if it is same that means that he is good he can update the data what he lose he will check one one they both the same so now he can modify the data from 30 to 40 and now he need to update this version from 1 to 2 because he did modify ok other and decided nothing so he's fine so we just successfully updated record in the table now let's take one more example and see what happens so so this guy I gave want to update this record from 40 to 50 what he has to do he comes to this row with ID 3 and then he leaves the data inversion so what he reads 40 with the version 2 now why he is incrementing he is this guy is little slow why he is incrementing this guy came in and somehow he also he is interested in ID number 3 row and then he also want to increment 42 maybe 17 Politis also does this he comes here and reads the data and the version here is the data the data is 14 and the version is stood so and he wanted to increment it by 3 so what he does is it is plus 14 plus or sorry 3 he basically gets 17 and the version is stated now obviously he will go back to this master and this little scholar so he is still doing let's whatever okay so he will go back and then he update 14 to 17 first you need to check the Russian so he checks the question - and - it's good so you can update now what he does is he takes from 40 to 70 and then he will increment the portion took 3 because he just did updates he need to increment the version 3 so he's happy and he came back now this guy who actually read up first now he's finished with the operation she has to increment I think he will increment from 14 to 15 and their version when he that was to know he will go back to check the version 2 3 it's a mismatch that means that this I will know that before I do some modification someone has came in and then has modified the data if I update this data 58 to this record the data is going to get corrupted so he's not supposed to do that because the versions are the front-end that means that he just need to discard this information what he has so he will have to recompute the information so he will again read it what he has today is 17 and the version is 3 he will happen he commits whatever and that he will have to go back in that so now this is a easy way to figure out someone has modified or not now in this case as optimistically he all these things nothing bad is going to happen so he always allow others to update when he is doing some work ok even though we have transactions we basically have isolated this operation we basically read it and do some updates meanwhile we are no others to update this particular row as well but if it was pessimistic lock we completely lock the slope when this guy is doing something this there is not even allowed to you know update this particular row sometimes it is ok to order to read but it is never allowed to update this information if it was a semester clock when this guy read it because this guy is Chloe is doing some okay this day is not even a lot to update this information he has to wait until this day mr. Locke and then only counted because it was optimistic loss it was okay to know multiple people to update when someone is actually you know doing some work on the data which was existed earlier so there are specific use cases where you have to use optimistic long and then specific case places where you have the specimen system you know it's not always true that you have to always use optimistic or you have to obvious it's pessimistic it all depends from the use cases so if it was a database you can yeah it's always good to use pessimistic lock if it was some places like Wikipedia you know article editing say because it they take time right because you have for example in the database you have a article which is a b c d the article ID is said one so editing article takes so much of time so what usually people do is they read this take this information and put it into editor and then they started to type and then when they come back and update by the time there could be so many people they also want to update this article if it was a pessimist o'clock it's like until i finish the editing of this article no one is supposed to edit this article but wikipedia and any other you know content updation portals will basically use optimistic clock what it means is the that will basically work in this facility this way basically i read this information i edited I can take a day or two to edit that information and if someone really wants to do some updates they are not solar top page so if they are faster they can read it and update it by the time I finish it if I go back and try to update it it basically does a conflict of version and and I get to no case that's already upgraded I just need to get the new information and my updates and then push it back so this is the place where we actually can use Optimus o'clock and the signal are basically used in as I mentioned in our DBMS and you know locking the particular row up and not allowing others to update let's just a little bit about the performance think so how does the how does mr. Clark performs in the distributed systems and how since the distributed systems has a lot of concurrency so since IQ law tends to like like holding up a resource so no other concurrent I know actors can actually get access to that particular document or document for a patient but since as a mystical Lama is kind of all those other concurrent actors to modify it it is kind of like holds good for any distributed systems not pessimistic Locke is not the kind of law which basically distributed systems will really like so in case of performance I think I would give up plus points to Optimus it law but not to the pessimistic lock there is a general rule of thumb of picking up what kind of lock many to use this is strategies if there are very few conflicts in the record updation then it's always good to go for using Optimus adopts a case of Wikipedia itself right so how many people do you think will be trying to update the same article so if it is like thousands of people are trying to update a same article that means that optimistic law might not give a lot of performance the recent being say you have so this is the article and so so basically they're like thousands of people or concurrent actors who is trying to update it now because of this many people trying to update optimistic lock will still allow everyone to you know basically read and try updating right because no one gets the version number right or no one gets it timestamp right so everyone basically go there and then see a different version drop it go there and drop it go they don't drop it so no one actually succeeds in operating the actual data it's like like too much of Kairos there so in this case it's so the performance will be not good because the most of the time is wasted by you know by trying to update and failing to do so so there were so many actors everyone is trying to update the conference they do the versions are mismatched change so nobody's basically maybe succeeding so on the other hand if there are very few conflicts that means that there are not so many you know concurring actors or concurrent connections of threads who is trying to update this article so if there are only few people that means that it's good it's ok because not a lot of clash is going to happen between the versions though there are high likely chances that the record will become updated successfully in order of you know mismatch in two versions but on the other hand if there are no more conflicts the best Millikan pessimistic lock basically works really well how so you basically doesn't even allow anyone to acquire that lock itself so it's basically like who acquires lock they're the one who can basically do the job if it is like Wikipedia example as a mystery truffles did not perform really well because you can't just lock the article by one person so none of them can pick it up so so there is a fine distinction or you just need to understand which lock is better based on the scenario and the kind of feature you want to provide while deciding which kind of block you need to interview speak to your system so a lot of drawbacks are you know some things which you need to really care be careful when you're using pistols to clock something like we always need to make sure that there are mounted locks in your Messam stick clock because there could be possibility that there are two locks in your system and one for your room for B and both locks are basically waiting for each of them so this guy is waiting for the stock and this dais with this lock and no one really senses that's kind of deadlock so you always need to make sure that there are no deep locks in your system and also you need to make sure that there is always a timeout in case of these kind of problems or in case of even even a different problem if lock is head for more time that you need to make sure that you always have a time off for rocks otherwise the logs are completely locking the table that no one really can do anything about it so if you have a timeout of some time the locks will be automatically released and so the system is still back into action that's one thing and also in kind of pessimistic lock you always money eat a diet connection you know allow connections between the you know person who is actually trying to lock it and then to the system so in case of database you always need a persistent connection to have this block working because say for example transactions which is right you need to have a live connection to the database from from your application server just to keep on assuring the series of you know instruction which actually will be considered as a unit of work if not a continuous connection you still need to have a transaction ID or something which basically have a reference of reference to basically collect all of these individual queries into one unit of work but those problems will not be really effective if you are using optimistic clock because optimistic lock is like it basically was Sanda versioning or based on the time stance so you basically can do a lot of operations a synchronously or without having a persistent connection basically you go and read it you have the version you can go back do some modification and then come back without having a persistent connection so you basically have always a version number to compare before you update but that's not always easier in case of I know business if you are I think I have covered a lot of information on pessimistic and optimistic Rock I think I definitely hope that you guys learned something about how transactions work and how actually loss will be used in transactions and what are the different kind of box you can use it in your distributed systems and when to use it I think if you like this video please give a thumbs up and do comment if you really want to learn something if you know some new topics please leave a comment I'm going to definitely make a video on that and also definitely please share this video with your friends and to subscribe and also thanks a lot
Info
Channel: Tech Dummies Narendra L
Views: 58,027
Rating: undefined out of 5
Keywords: Amazon interview question, interview questions, interview preparations, algo and ds interview question, software interview preparation, developer interview questions, Facebook interview question, google interview question, Technical interview question, software architecture, system design, learn System design, optimistic locks, pessimistic locks, wikipideal ocks, locks and transactions, system design basics
Id: R-iX1r_7UY0
Channel Id: undefined
Length: 18min 41sec (1121 seconds)
Published: Thu Jun 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.