‫Day's topic is exclusive, Locke versus shared Locke, a very, very common topic out there and the
‫database and the DBMS systems.
‫So let's start with this episode.
‫We're going to discuss the differences between them and we're going to talk about an example of some
‫advantages, disadvantages of both.
‫And we're going to talk about an example.
‫A story about defining each of them.
‫So exclusive, Locke and a shared Locke.
‫So think of an exclusive law as following.
‫I wanted to read a piece of data in my database.
‫Think of it like a column or a row.
‫But I want to make sure that when I read this value or when I want to update this value, nobody is
‫connected.
‫And nobody is attempting to read this value for security reason, for consistency reasons.
‫You know, I want to acquire an exclusive privileges on this value, on this column, on this role,
‫so that if anybody tried to read that value, they'll get an error.
‫So you know what?
‫So that's that's the idea of an exclusive look.
‫So I will be the only one essentially as a connection.
‫I'll be the only one who is allowed to update that so nobody can sneak in and start reading values out
‫of that.
‫Right.
‫The shared look, on the other hand, is actually the opposite of that, is when I want to read a value
‫from the database.
‫When I read a certain value, I want to make sure that nobody changed that.
‫So if I am reading my balance in a bank right, I want to make sure I'm doing the reporting.
‫I'm doing a huge query.
‫I want to acquire a shared look and says, Hey, database, please.
‫I'm acquiring a shared look on this row.
‫So if anybody's trying to edit that value, just make them fail.
‫So I am I want this value to be always the same in my vision, in my transaction, in this current transaction.
‫I want it to be unchanged.
‫That's why I acquire a shared look and many users can acquire many connections, can acquire a different
‫share.
‫Look, I know this might sound a little vague, but I will explain.
‫And in an example, hopefully.
‫So let's give you an example.
‫So, look, General, I want to obtain a lock on a value, so I am the only one who is allowed to edit
‫it shared.
‫Look, I want to acquire a shared log on or a read log on a value so it doesn't change.
‫So another thing I won't explain before we got to do the example is when I try to obtain an exclusive
‫lock, there must not be any shared lock acquired on that value.
‫Does that make sense that they are linked actually together?
‫And the opposite?
‫If I want to obtain a shared log, there must not be any exclusive log obtained.
‫So these two are exactly the opposite of each other.
‫So you can have as let's say, for example, I have like seven shared log on on the balance on a certain
‫value.
‫That means nobody can acquire an exclusive lock on that value.
‫So nobody can edit it as a sense, you know.
‫So that's the idea.
‫Read or write log.
‫That makes sense.
‫Let's jump to an example, guys.
‫All right.
‫So here I have three people, Alice, Bob and Charlie.
‫Alice's Red Bob is blue, Charlie's green.
‫And we're going to start this is the timeline from left to right.
‫And then we're going to start with Alice started the transaction trying to deposit a two hundred dollars
‫on her account.
‫Right.
‫So in order to do that, she needs obviously to do an album date statement.
‫Right.
‫To do that, you need to acquire an exclusive log.
‫And that's what she's the only one issue that she's the only one that is connected right now.
‫So she she'll basically acquire it, tries to acquire an exclusive log and guess what?
‫To succeed because nobody else acquired any shared log.
‫So they're good.
‫And now you just like, move, move on and and acquire the exclusive look.
‫She is guaranteeing that, hey, I'm on about Ed.
‫That's good.
‫Because I don't want people reading bad value.
‫Right.
‫Because I'm about to update this balance.
‫So don't ever try to read my balance right now because it's going to change if you read.
‫Did I make decisions based on that is going to be bad, right?
‫So you're going to acquire that and then say, OK, I'm going to deposit.
‫So the next transaction is essentially transaction going to come and then we're going to increase our
‫balance by two hundred very, very quick and then commit the transaction and then have a transaction
‫finished just a short period of time.
‫And then you turn around Alice and then it starts another long running transaction.
‫Look at that.
‫Said it's a long transaction.
‫Looks like it.
‫Right.
‫It's a job reporting job on her.
‫But she is doing reading a lot.
‫She's just doing reading operation.
‫So a Reid operation is acquiring Shedlock.
‫So what she says here is, hey, hey, guys, I am going to acquire a shared look on my balance account.
‫So don't try to change it.
‫Please don't try to mess with it.
‫Don't try to update it because you can't, because I just acquired the shares so nobody can acquire
‫an exclusive log on her account now because she just had a shared log there.
‫OK, so, OK, she is doing her thing or reading all this tables and doing the reporting so that all
‫transaction when Bob started the reporting job on his account.
‫So that's a completely different thing.
‫Right.
‫So they acquired a shared log barbacoa to shared log on his account.
‫So on this value, while while Alice acquired a show like a shared log on, this value makes sense.
‫All right.
‫So that succeeded.
‫We have a lock for Bob.
‫We have a shared log for Alice, and then we're continuing.
‫So something is going to happen here.
‫What is that green thing?
‫What is going on, Charlie?
‫Charlie, what are you going to do?
‫Charlie wants to do a transfer to Bob account.
‫Bob, it's your lucky day, dude.
‫Bob's account attempt to obtain an exclusive slot but fails.
‫Why?
‫Because you want to do a transfer right from his account of a 300 dollars.
‫I didn't write in there, but trying to do a three hundred dollars transfer from Charlie's account,
‫from his account to Bob's account.
‫So in order to do that, he needs to acquire an exclusive log.
‫So anyone I want to update that, guys, I don't want it to change as the board would try to acquire
‫an exclusive look on Bob's account.
‫And he says, oh, fails.
‫Why?
‫Because Bob has already acquired a shared log.
‫He's doing something.
‫He's doing his thing.
‫Bob is doing his job.
‫He's reading it the all these tables.
‫He's doing the thing.
‫So he asked us to to to create this shared log so nobody changes his account.
‫So guess what?
‫Charlie just failed his transaction.
‫He cannot do that.
‫And that's actually good.
‫We don't want people editing while other people are reading as just that.
‫So that's a danger, especially with banking systems.
‫So guess what?
‫After a while, Bob completes his transaction, his job, his reading job, and he says, you know what,
‫I'm done.
‫So he releases that -- log so he's no longer a quiet Shatalov.
‫Afterwards, Charlie attempts again to acquire exclusive love.
‫And Stormy Zaid's says, OK, I'm going to since I just succeeded, let me quickly go ahead and acquired
‫the log and then commit to three hundred bucks from Charlie's account to Bob.
‫Right.
‫So that's a general the difference between exclusive log and shared log.
‫And there is another example that I mentioned is that while while there is exclusive look, nobody can
‫read, so.
‫You can you can think of this, that let's jump to the disadvantage advantages of all this thing as
‫shared lock and exclusive lock and was introduced to to ensure consistency in the system, as you can
‫see.
‫I want to be consistent.
‫When I read this value, I wanted this value to always be the same.
‫That's a shared exclusive.
‫When I want to attempt when I wanted to write something, I wanted nobody to be connected.
‫Right.
‫So next time they connect, they read the latest volume and exclusive look and shared log.
‫In general, they are very useful to obtain consistency, especially with a banking first.
‫And the second thing is with configurations.
‫If like you have a central configuration system and it's a critical configuration system, obviously,
‫and you're making changes to it, you don't want others reading stale configuration.
‫You want people to you want to kick people out from the connection from the database and only let them
‫read when you're done editing, essentially.
‫Right.
‫So you saw this this way.
‫All the clients will always read the latest and greatest configuration values.
‫So that's one of the benefits.
‫The disadvantages of that obviously can cause.
‫He suffers as a result.
‫You can see like people.
‫There are chances where your failures obviously all the time, especially in banking system, you see,
‫if you go to Bank of America, sometimes you can see those great out.
‫You can do any transactions at night, sometimes like a midnight, because they are doing their job
‫reporting and they are doing all this shared log.
‫So you can't really obtained exclusive like at that time.
‫But there are times that you cannot do your transactions.
‫Concurrency cannot be achieved because of these luck.
‫But at the same time, they are very critical to ensure consistency.
‫All right, guys, this is in a nutshell, exclusive lock versus share.
‫Like I'm going to see you in the next one.
‫Have a good day.