1 00:00:00,300 --> 00:00:07,650 ‫My name is Hussein and and this video, we're going to quickly show you a practical example of how Dedlock 2 00:00:07,650 --> 00:00:12,090 ‫happens in databases and this specific so quickly. 3 00:00:12,090 --> 00:00:20,190 ‫Guys, I did lock happens when two processes are or two clients are fighting for one or more resources 4 00:00:20,190 --> 00:00:27,480 ‫and either one is waiting for the other process to release the lock on a given resource. 5 00:00:27,480 --> 00:00:34,910 ‫And they will be in this infinite state where nobody can release that log. 6 00:00:34,920 --> 00:00:39,300 ‫So it's a very famous problem in computer science, obviously happens in dead bases. 7 00:00:39,300 --> 00:00:41,640 ‫And I'm going to show you how this happens. 8 00:00:41,640 --> 00:00:47,370 ‫And most databases actually catch these deadlocks and fail the transactions when they happen. 9 00:00:47,500 --> 00:00:48,440 ‫How about we jump into it? 10 00:00:48,450 --> 00:00:54,540 ‫So before I jump into this video, guys, I want to add I want you to be familiar with locks, mainly 11 00:00:54,540 --> 00:00:56,550 ‫shared lock, exclusive locks. 12 00:00:56,550 --> 00:01:02,100 ‫And so if you want to learn more, check out the videos I've made about these topics here. 13 00:01:02,100 --> 00:01:04,800 ‫Go and take a look at them and what that say. 14 00:01:04,800 --> 00:01:06,900 ‫Let's just jump into this video, guys, and show you how. 15 00:01:07,110 --> 00:01:14,670 ‫So I have here two clients terminal connecting to one Posterous in instances running on Dalkia. 16 00:01:14,670 --> 00:01:16,500 ‫I showed many times how to do that. 17 00:01:16,510 --> 00:01:17,800 ‫And this channel. 18 00:01:17,910 --> 00:01:18,560 ‫Check it out. 19 00:01:18,810 --> 00:01:24,360 ‫So in the first client, I'm going to stop begin a transaction and in the second client, I'm going 20 00:01:24,360 --> 00:01:26,820 ‫to also begin a transaction. 21 00:01:27,300 --> 00:01:27,530 ‫Right. 22 00:01:27,690 --> 00:01:29,220 ‫So both of them begin the transaction. 23 00:01:29,250 --> 00:01:35,570 ‫There is a table here called Test and it has a primary key and has one field and a primary key field. 24 00:01:35,580 --> 00:01:40,200 ‫So you guys know if it's a primary, you can only have one value at a time. 25 00:01:40,200 --> 00:01:40,540 ‫Right. 26 00:01:40,560 --> 00:01:41,510 ‫A unique value. 27 00:01:41,790 --> 00:01:50,070 ‫So I'm going to insert into this table, insert into test values, let's call it, I don't know, twenty. 28 00:01:50,610 --> 00:01:50,940 ‫Right. 29 00:01:51,270 --> 00:01:53,310 ‫And that's successful because 20 doesn't exist. 30 00:01:53,310 --> 00:01:53,600 ‫Right. 31 00:01:53,610 --> 00:01:55,380 ‫And I didn't commit my transaction yet. 32 00:01:55,410 --> 00:02:00,930 ‫And then the other transaction, I am going to insert test values. 33 00:02:00,930 --> 00:02:01,860 ‫Twenty one. 34 00:02:01,980 --> 00:02:06,030 ‫OK, and that also succeeds because 21 doesn't exist. 35 00:02:06,060 --> 00:02:12,110 ‫However, now in this transaction, I am going to attempt to insert 20. 36 00:02:12,300 --> 00:02:20,520 ‫So 20 in this case, guys, despite it not being committed, there is an exclusive WLOX obtained on 37 00:02:20,520 --> 00:02:21,200 ‫that value. 38 00:02:21,270 --> 00:02:26,440 ‫Nobody can actually change that or insert that value again. 39 00:02:26,640 --> 00:02:30,690 ‫However, we are not sure yet because this transaction did not commit yet. 40 00:02:30,690 --> 00:02:30,890 ‫Right. 41 00:02:30,910 --> 00:02:32,940 ‫So this might roll back or commit. 42 00:02:32,940 --> 00:02:37,490 ‫And if that happened, this transaction will unblock and continue. 43 00:02:37,500 --> 00:02:41,160 ‫However, let's let's spice things up here in this transaction. 44 00:02:41,160 --> 00:02:45,810 ‫I am going to actually insert the value of, you guessed it, twenty one. 45 00:02:45,960 --> 00:02:49,130 ‫And now this guy is also going to be blocked. 46 00:02:49,140 --> 00:02:55,980 ‫However, both of them will be in a state where they are waiting for each other and a deadlock will 47 00:02:55,980 --> 00:02:56,730 ‫be detected. 48 00:02:56,970 --> 00:03:02,010 ‫The last one entering that deadlock will be the one to fail. 49 00:03:02,280 --> 00:03:08,020 ‫So in this case, what happened here, Posterous immediately deducted that took us like I don't talk. 50 00:03:08,040 --> 00:03:15,960 ‫I think I second for that deadlock to be detected and ones that did not get deducted, the transaction 51 00:03:15,960 --> 00:03:23,090 ‫water was rolled back and that transaction actually won because it entered the deadlock at first. 52 00:03:23,100 --> 00:03:27,990 ‫So essentially, that's how a deadlock is handled by the database. 53 00:03:27,990 --> 00:03:30,120 ‫It's very complex thing. 54 00:03:30,390 --> 00:03:33,770 ‫Most databases handle these that looks very smartly. 55 00:03:33,780 --> 00:03:38,940 ‫And they and they rolled back the transaction that essentially entered the deadlock last. 56 00:03:39,210 --> 00:03:44,610 ‫However, let's just explain a more optimistic scenario, guys, huh? 57 00:03:44,670 --> 00:03:46,110 ‫So I'm going to roll back this. 58 00:03:46,320 --> 00:03:54,330 ‫I'm going to also roll back this clear here, clear here and begin a transaction and also here begin 59 00:03:54,330 --> 00:03:57,480 ‫a transaction where things are there's no deadlock. 60 00:03:57,480 --> 00:03:58,110 ‫What do we do? 61 00:03:58,110 --> 00:04:00,480 ‫Insert into test values? 62 00:04:00,780 --> 00:04:01,230 ‫Twenty. 63 00:04:02,790 --> 00:04:06,660 ‫And here I'm going to insert into test values. 64 00:04:07,580 --> 00:04:12,470 ‫30 and here, I'm going to also insert into test values 20. 65 00:04:13,980 --> 00:04:20,850 ‫And if I do that, I'm going to enter into this lock mode, and that's absolutely fine, because until 66 00:04:20,850 --> 00:04:24,270 ‫this transaction finishes, we are locked, right. 67 00:04:24,390 --> 00:04:26,910 ‫So what happens if I do roll back here, guys? 68 00:04:27,060 --> 00:04:34,470 ‫And when I do roll back immediately, that transaction actually succeeds because that 20 is now available 69 00:04:34,620 --> 00:04:35,700 ‫to be inserted. 70 00:04:35,700 --> 00:04:36,090 ‫Right. 71 00:04:36,450 --> 00:04:39,960 ‫So if, however, we flipped this. 72 00:04:41,980 --> 00:04:44,710 ‫Let's flip it, and I do. 73 00:04:46,070 --> 00:04:55,850 ‫Begins transaction here, begin transaction here, insert into 20 and here insert into, let's say 30, 74 00:04:56,450 --> 00:05:00,470 ‫then I'm going to insert into 20 here, which lock sales. 75 00:05:00,470 --> 00:05:00,790 ‫Right. 76 00:05:00,950 --> 00:05:06,650 ‫But here in this case, I'm going to commit what what happened to this transaction exactly is going 77 00:05:06,650 --> 00:05:14,870 ‫to fail with the duplicate key, because now that this transaction actually gone, this as this transaction 78 00:05:14,890 --> 00:05:20,640 ‫in this case, the 20 is not available and we're going to get that error right and fail. 79 00:05:20,670 --> 00:05:20,950 ‫Right. 80 00:05:21,080 --> 00:05:21,510 ‫Our guys. 81 00:05:21,560 --> 00:05:25,100 ‫That was a quick video describing deadlocks and locks and. 82 00:05:25,430 --> 00:05:30,050 ‫Very quickly, Olby, enjoy this video and I'm going to see you in the next one. 83 00:05:30,170 --> 00:05:31,090 ‫You guys say on.