1 00:00:00,060 --> 00:00:09,180 ‫Day's topic is exclusive, Locke versus shared Locke, a very, very common topic out there and the 2 00:00:09,180 --> 00:00:11,240 ‫database and the DBMS systems. 3 00:00:11,850 --> 00:00:14,370 ‫So let's start with this episode. 4 00:00:14,370 --> 00:00:18,810 ‫We're going to discuss the differences between them and we're going to talk about an example of some 5 00:00:18,810 --> 00:00:21,720 ‫advantages, disadvantages of both. 6 00:00:22,080 --> 00:00:24,530 ‫And we're going to talk about an example. 7 00:00:24,720 --> 00:00:29,580 ‫A story about defining each of them. 8 00:00:29,970 --> 00:00:32,490 ‫So exclusive, Locke and a shared Locke. 9 00:00:32,490 --> 00:00:36,160 ‫So think of an exclusive law as following. 10 00:00:36,180 --> 00:00:39,570 ‫I wanted to read a piece of data in my database. 11 00:00:39,690 --> 00:00:42,360 ‫Think of it like a column or a row. 12 00:00:42,900 --> 00:00:52,740 ‫But I want to make sure that when I read this value or when I want to update this value, nobody is 13 00:00:52,740 --> 00:00:53,790 ‫connected. 14 00:00:53,790 --> 00:01:02,370 ‫And nobody is attempting to read this value for security reason, for consistency reasons. 15 00:01:03,030 --> 00:01:12,030 ‫You know, I want to acquire an exclusive privileges on this value, on this column, on this role, 16 00:01:12,240 --> 00:01:18,040 ‫so that if anybody tried to read that value, they'll get an error. 17 00:01:18,360 --> 00:01:19,120 ‫So you know what? 18 00:01:19,170 --> 00:01:22,200 ‫So that's that's the idea of an exclusive look. 19 00:01:22,210 --> 00:01:26,070 ‫So I will be the only one essentially as a connection. 20 00:01:26,220 --> 00:01:33,570 ‫I'll be the only one who is allowed to update that so nobody can sneak in and start reading values out 21 00:01:33,570 --> 00:01:33,960 ‫of that. 22 00:01:34,330 --> 00:01:34,750 ‫Right. 23 00:01:35,640 --> 00:01:45,480 ‫The shared look, on the other hand, is actually the opposite of that, is when I want to read a value 24 00:01:45,930 --> 00:01:47,130 ‫from the database. 25 00:01:47,130 --> 00:01:53,490 ‫When I read a certain value, I want to make sure that nobody changed that. 26 00:01:54,360 --> 00:02:00,420 ‫So if I am reading my balance in a bank right, I want to make sure I'm doing the reporting. 27 00:02:00,420 --> 00:02:02,240 ‫I'm doing a huge query. 28 00:02:02,580 --> 00:02:06,900 ‫I want to acquire a shared look and says, Hey, database, please. 29 00:02:07,260 --> 00:02:09,880 ‫I'm acquiring a shared look on this row. 30 00:02:10,260 --> 00:02:16,380 ‫So if anybody's trying to edit that value, just make them fail. 31 00:02:16,920 --> 00:02:26,250 ‫So I am I want this value to be always the same in my vision, in my transaction, in this current transaction. 32 00:02:26,260 --> 00:02:27,700 ‫I want it to be unchanged. 33 00:02:27,750 --> 00:02:33,570 ‫That's why I acquire a shared look and many users can acquire many connections, can acquire a different 34 00:02:33,570 --> 00:02:33,810 ‫share. 35 00:02:33,850 --> 00:02:37,520 ‫Look, I know this might sound a little vague, but I will explain. 36 00:02:37,570 --> 00:02:39,680 ‫And in an example, hopefully. 37 00:02:40,320 --> 00:02:41,830 ‫So let's give you an example. 38 00:02:41,860 --> 00:02:50,520 ‫So, look, General, I want to obtain a lock on a value, so I am the only one who is allowed to edit 39 00:02:50,520 --> 00:02:51,520 ‫it shared. 40 00:02:51,540 --> 00:03:00,670 ‫Look, I want to acquire a shared log on or a read log on a value so it doesn't change. 41 00:03:01,020 --> 00:03:07,710 ‫So another thing I won't explain before we got to do the example is when I try to obtain an exclusive 42 00:03:07,710 --> 00:03:14,070 ‫lock, there must not be any shared lock acquired on that value. 43 00:03:14,250 --> 00:03:17,310 ‫Does that make sense that they are linked actually together? 44 00:03:17,910 --> 00:03:19,880 ‫And the opposite? 45 00:03:19,890 --> 00:03:26,790 ‫If I want to obtain a shared log, there must not be any exclusive log obtained. 46 00:03:27,090 --> 00:03:30,530 ‫So these two are exactly the opposite of each other. 47 00:03:30,750 --> 00:03:36,930 ‫So you can have as let's say, for example, I have like seven shared log on on the balance on a certain 48 00:03:36,930 --> 00:03:37,520 ‫value. 49 00:03:37,890 --> 00:03:42,210 ‫That means nobody can acquire an exclusive lock on that value. 50 00:03:42,390 --> 00:03:45,450 ‫So nobody can edit it as a sense, you know. 51 00:03:45,480 --> 00:03:46,410 ‫So that's the idea. 52 00:03:46,420 --> 00:03:47,910 ‫Read or write log. 53 00:03:48,300 --> 00:03:48,970 ‫That makes sense. 54 00:03:49,230 --> 00:03:51,260 ‫Let's jump to an example, guys. 55 00:03:51,270 --> 00:03:51,980 ‫All right. 56 00:03:51,990 --> 00:03:55,440 ‫So here I have three people, Alice, Bob and Charlie. 57 00:03:55,650 --> 00:03:59,820 ‫Alice's Red Bob is blue, Charlie's green. 58 00:04:00,240 --> 00:04:04,140 ‫And we're going to start this is the timeline from left to right. 59 00:04:04,350 --> 00:04:10,830 ‫And then we're going to start with Alice started the transaction trying to deposit a two hundred dollars 60 00:04:10,980 --> 00:04:12,030 ‫on her account. 61 00:04:12,330 --> 00:04:12,790 ‫Right. 62 00:04:13,020 --> 00:04:17,790 ‫So in order to do that, she needs obviously to do an album date statement. 63 00:04:17,790 --> 00:04:18,120 ‫Right. 64 00:04:18,120 --> 00:04:21,330 ‫To do that, you need to acquire an exclusive log. 65 00:04:21,330 --> 00:04:26,450 ‫And that's what she's the only one issue that she's the only one that is connected right now. 66 00:04:26,460 --> 00:04:33,000 ‫So she she'll basically acquire it, tries to acquire an exclusive log and guess what? 67 00:04:33,000 --> 00:04:37,170 ‫To succeed because nobody else acquired any shared log. 68 00:04:37,320 --> 00:04:38,370 ‫So they're good. 69 00:04:38,520 --> 00:04:42,720 ‫And now you just like, move, move on and and acquire the exclusive look. 70 00:04:42,880 --> 00:04:46,240 ‫She is guaranteeing that, hey, I'm on about Ed. 71 00:04:46,310 --> 00:04:46,800 ‫That's good. 72 00:04:46,890 --> 00:04:47,490 ‫That's good. 73 00:04:47,490 --> 00:04:50,970 ‫Because I don't want people reading bad value. 74 00:04:51,150 --> 00:04:51,540 ‫Right. 75 00:04:51,690 --> 00:04:54,120 ‫Because I'm about to update this balance. 76 00:04:54,120 --> 00:04:59,780 ‫So don't ever try to read my balance right now because it's going to change if you read. 77 00:04:59,860 --> 00:05:03,300 ‫Did I make decisions based on that is going to be bad, right? 78 00:05:03,350 --> 00:05:07,980 ‫So you're going to acquire that and then say, OK, I'm going to deposit. 79 00:05:08,140 --> 00:05:13,300 ‫So the next transaction is essentially transaction going to come and then we're going to increase our 80 00:05:13,300 --> 00:05:19,000 ‫balance by two hundred very, very quick and then commit the transaction and then have a transaction 81 00:05:19,000 --> 00:05:21,750 ‫finished just a short period of time. 82 00:05:22,090 --> 00:05:27,640 ‫And then you turn around Alice and then it starts another long running transaction. 83 00:05:27,650 --> 00:05:28,160 ‫Look at that. 84 00:05:28,270 --> 00:05:30,230 ‫Said it's a long transaction. 85 00:05:30,250 --> 00:05:30,760 ‫Looks like it. 86 00:05:30,760 --> 00:05:31,010 ‫Right. 87 00:05:31,180 --> 00:05:33,520 ‫It's a job reporting job on her. 88 00:05:33,520 --> 00:05:35,280 ‫But she is doing reading a lot. 89 00:05:35,320 --> 00:05:36,980 ‫She's just doing reading operation. 90 00:05:37,690 --> 00:05:40,480 ‫So a Reid operation is acquiring Shedlock. 91 00:05:40,660 --> 00:05:47,110 ‫So what she says here is, hey, hey, guys, I am going to acquire a shared look on my balance account. 92 00:05:47,290 --> 00:05:50,900 ‫So don't try to change it. 93 00:05:50,920 --> 00:05:53,830 ‫Please don't try to mess with it. 94 00:05:53,840 --> 00:06:00,010 ‫Don't try to update it because you can't, because I just acquired the shares so nobody can acquire 95 00:06:00,010 --> 00:06:06,640 ‫an exclusive log on her account now because she just had a shared log there. 96 00:06:06,760 --> 00:06:13,620 ‫OK, so, OK, she is doing her thing or reading all this tables and doing the reporting so that all 97 00:06:13,630 --> 00:06:19,610 ‫transaction when Bob started the reporting job on his account. 98 00:06:19,620 --> 00:06:21,490 ‫So that's a completely different thing. 99 00:06:21,660 --> 00:06:21,870 ‫Right. 100 00:06:21,910 --> 00:06:28,210 ‫So they acquired a shared log barbacoa to shared log on his account. 101 00:06:28,210 --> 00:06:34,870 ‫So on this value, while while Alice acquired a show like a shared log on, this value makes sense. 102 00:06:35,920 --> 00:06:36,340 ‫All right. 103 00:06:36,350 --> 00:06:37,710 ‫So that succeeded. 104 00:06:37,720 --> 00:06:39,930 ‫We have a lock for Bob. 105 00:06:39,940 --> 00:06:43,830 ‫We have a shared log for Alice, and then we're continuing. 106 00:06:43,840 --> 00:06:45,340 ‫So something is going to happen here. 107 00:06:45,370 --> 00:06:46,390 ‫What is that green thing? 108 00:06:46,910 --> 00:06:48,820 ‫What is going on, Charlie? 109 00:06:48,850 --> 00:06:50,690 ‫Charlie, what are you going to do? 110 00:06:51,070 --> 00:06:53,980 ‫Charlie wants to do a transfer to Bob account. 111 00:06:54,010 --> 00:06:55,600 ‫Bob, it's your lucky day, dude. 112 00:06:56,170 --> 00:07:00,910 ‫Bob's account attempt to obtain an exclusive slot but fails. 113 00:07:01,120 --> 00:07:01,420 ‫Why? 114 00:07:01,570 --> 00:07:07,360 ‫Because you want to do a transfer right from his account of a 300 dollars. 115 00:07:07,360 --> 00:07:12,640 ‫I didn't write in there, but trying to do a three hundred dollars transfer from Charlie's account, 116 00:07:12,640 --> 00:07:14,510 ‫from his account to Bob's account. 117 00:07:14,710 --> 00:07:17,710 ‫So in order to do that, he needs to acquire an exclusive log. 118 00:07:17,710 --> 00:07:24,160 ‫So anyone I want to update that, guys, I don't want it to change as the board would try to acquire 119 00:07:24,160 --> 00:07:26,080 ‫an exclusive look on Bob's account. 120 00:07:26,320 --> 00:07:29,960 ‫And he says, oh, fails. 121 00:07:30,010 --> 00:07:30,750 ‫Why? 122 00:07:31,510 --> 00:07:34,840 ‫Because Bob has already acquired a shared log. 123 00:07:34,840 --> 00:07:35,810 ‫He's doing something. 124 00:07:35,840 --> 00:07:36,850 ‫He's doing his thing. 125 00:07:37,090 --> 00:07:38,380 ‫Bob is doing his job. 126 00:07:38,380 --> 00:07:40,390 ‫He's reading it the all these tables. 127 00:07:40,390 --> 00:07:41,440 ‫He's doing the thing. 128 00:07:41,740 --> 00:07:47,680 ‫So he asked us to to to create this shared log so nobody changes his account. 129 00:07:47,920 --> 00:07:49,150 ‫So guess what? 130 00:07:49,150 --> 00:07:50,770 ‫Charlie just failed his transaction. 131 00:07:50,770 --> 00:07:51,700 ‫He cannot do that. 132 00:07:51,820 --> 00:07:53,470 ‫And that's actually good. 133 00:07:53,530 --> 00:08:00,150 ‫We don't want people editing while other people are reading as just that. 134 00:08:00,700 --> 00:08:03,160 ‫So that's a danger, especially with banking systems. 135 00:08:03,550 --> 00:08:06,010 ‫So guess what? 136 00:08:06,010 --> 00:08:11,560 ‫After a while, Bob completes his transaction, his job, his reading job, and he says, you know what, 137 00:08:11,560 --> 00:08:12,060 ‫I'm done. 138 00:08:12,340 --> 00:08:16,300 ‫So he releases that -- log so he's no longer a quiet Shatalov. 139 00:08:16,870 --> 00:08:22,870 ‫Afterwards, Charlie attempts again to acquire exclusive love. 140 00:08:22,870 --> 00:08:32,360 ‫And Stormy Zaid's says, OK, I'm going to since I just succeeded, let me quickly go ahead and acquired 141 00:08:32,440 --> 00:08:37,870 ‫the log and then commit to three hundred bucks from Charlie's account to Bob. 142 00:08:37,900 --> 00:08:38,470 ‫Right. 143 00:08:38,950 --> 00:08:44,410 ‫So that's a general the difference between exclusive log and shared log. 144 00:08:44,560 --> 00:08:51,280 ‫And there is another example that I mentioned is that while while there is exclusive look, nobody can 145 00:08:51,280 --> 00:08:53,140 ‫read, so. 146 00:08:54,190 --> 00:09:01,210 ‫You can you can think of this, that let's jump to the disadvantage advantages of all this thing as 147 00:09:02,260 --> 00:09:12,730 ‫shared lock and exclusive lock and was introduced to to ensure consistency in the system, as you can 148 00:09:12,730 --> 00:09:12,970 ‫see. 149 00:09:13,000 --> 00:09:14,800 ‫I want to be consistent. 150 00:09:15,010 --> 00:09:18,580 ‫When I read this value, I wanted this value to always be the same. 151 00:09:18,700 --> 00:09:20,140 ‫That's a shared exclusive. 152 00:09:20,260 --> 00:09:26,530 ‫When I want to attempt when I wanted to write something, I wanted nobody to be connected. 153 00:09:26,860 --> 00:09:27,200 ‫Right. 154 00:09:27,580 --> 00:09:34,000 ‫So next time they connect, they read the latest volume and exclusive look and shared log. 155 00:09:34,000 --> 00:09:40,330 ‫In general, they are very useful to obtain consistency, especially with a banking first. 156 00:09:40,330 --> 00:09:43,180 ‫And the second thing is with configurations. 157 00:09:43,300 --> 00:09:48,700 ‫If like you have a central configuration system and it's a critical configuration system, obviously, 158 00:09:48,970 --> 00:09:54,220 ‫and you're making changes to it, you don't want others reading stale configuration. 159 00:09:54,220 --> 00:10:02,590 ‫You want people to you want to kick people out from the connection from the database and only let them 160 00:10:02,590 --> 00:10:05,380 ‫read when you're done editing, essentially. 161 00:10:05,590 --> 00:10:05,890 ‫Right. 162 00:10:05,890 --> 00:10:07,720 ‫So you saw this this way. 163 00:10:07,870 --> 00:10:12,940 ‫All the clients will always read the latest and greatest configuration values. 164 00:10:12,940 --> 00:10:14,110 ‫So that's one of the benefits. 165 00:10:14,110 --> 00:10:17,140 ‫The disadvantages of that obviously can cause. 166 00:10:17,140 --> 00:10:18,460 ‫He suffers as a result. 167 00:10:18,700 --> 00:10:20,680 ‫You can see like people. 168 00:10:21,860 --> 00:10:28,070 ‫There are chances where your failures obviously all the time, especially in banking system, you see, 169 00:10:28,070 --> 00:10:31,820 ‫if you go to Bank of America, sometimes you can see those great out. 170 00:10:31,850 --> 00:10:37,310 ‫You can do any transactions at night, sometimes like a midnight, because they are doing their job 171 00:10:37,310 --> 00:10:40,280 ‫reporting and they are doing all this shared log. 172 00:10:40,290 --> 00:10:43,070 ‫So you can't really obtained exclusive like at that time. 173 00:10:43,400 --> 00:10:46,790 ‫But there are times that you cannot do your transactions. 174 00:10:47,270 --> 00:10:50,530 ‫Concurrency cannot be achieved because of these luck. 175 00:10:50,750 --> 00:10:55,940 ‫But at the same time, they are very critical to ensure consistency. 176 00:10:56,180 --> 00:11:00,880 ‫All right, guys, this is in a nutshell, exclusive lock versus share. 177 00:11:00,890 --> 00:11:03,240 ‫Like I'm going to see you in the next one. 178 00:11:03,660 --> 00:11:04,370 ‫Have a good day.