1 00:00:00,180 --> 00:00:02,800 ‫So now let's talk about DynamoDB transactions. 2 00:00:02,800 --> 00:00:04,470 ‫And the idea is that with a transaction, 3 00:00:04,470 --> 00:00:06,930 ‫you're going to get an all-or-nothing operation 4 00:00:06,930 --> 00:00:08,930 ‫to multiple items across one or more tables. 5 00:00:08,930 --> 00:00:11,250 ‫So for example, we want to update, 6 00:00:11,250 --> 00:00:13,990 ‫delete or add items, not for just for one table, 7 00:00:13,990 --> 00:00:15,350 ‫but across multiple items. 8 00:00:15,350 --> 00:00:17,680 ‫And either all the transactions, all the writes work, 9 00:00:17,680 --> 00:00:18,600 ‫or none of them work. 10 00:00:18,600 --> 00:00:20,190 ‫This is why it's called a transaction. 11 00:00:20,190 --> 00:00:23,940 ‫Now this gives DynamoDB, the ACID feature. 12 00:00:23,940 --> 00:00:28,070 ‫So atomicity, consistency, isolation and durability. 13 00:00:28,070 --> 00:00:30,140 ‫Transactions can be applied into two things, 14 00:00:30,140 --> 00:00:31,670 ‫read modes and write modes. 15 00:00:31,670 --> 00:00:33,390 ‫For read modes, you're going to have three read modes. 16 00:00:33,390 --> 00:00:34,380 ‫Therefore in DynamoDB, 17 00:00:34,380 --> 00:00:36,660 ‫you're going to get eventual consistency, 18 00:00:36,660 --> 00:00:38,660 ‫strong consistency that we know before, 19 00:00:38,660 --> 00:00:40,410 ‫and now transactional consistency, 20 00:00:40,410 --> 00:00:42,570 ‫which means I want to read data from all these tables 21 00:00:42,570 --> 00:00:45,320 ‫at the same time and get a consistent view across it. 22 00:00:45,320 --> 00:00:48,650 ‫Transactionally, a consistent view across it. 23 00:00:48,650 --> 00:00:49,660 ‫And for write modes, 24 00:00:49,660 --> 00:00:52,150 ‫you're going to get standard and transactional. 25 00:00:52,150 --> 00:00:54,420 ‫So standard is you're going to do many writes 26 00:00:54,420 --> 00:00:55,253 ‫across many tables, 27 00:00:55,253 --> 00:00:56,930 ‫but some of them can fail. 28 00:00:56,930 --> 00:00:59,360 ‫But, transactional, either all the writes work 29 00:00:59,360 --> 00:01:00,510 ‫across all the tables, 30 00:01:00,510 --> 00:01:03,350 ‫or none of the writes work across all the tables. 31 00:01:03,350 --> 00:01:04,400 ‫Now to do a transaction, 32 00:01:04,400 --> 00:01:06,750 ‫it will consume twice the write capacity you need 33 00:01:06,750 --> 00:01:08,040 ‫and the read capacity units. 34 00:01:08,040 --> 00:01:10,340 ‫That's because anybody who will perform two operations 35 00:01:10,340 --> 00:01:11,790 ‫in the background for every item, 36 00:01:11,790 --> 00:01:14,500 ‫it will prepare the transaction and then commit it. 37 00:01:14,500 --> 00:01:16,340 ‫Now, these two operations you need to know 38 00:01:16,340 --> 00:01:18,730 ‫are going to be TransactGetItems, 39 00:01:18,730 --> 00:01:21,880 ‫is an API code to do one or more GetItem operations 40 00:01:21,880 --> 00:01:23,240 ‫as part of the transaction, 41 00:01:23,240 --> 00:01:26,890 ‫or TransactWriteItems to do one of more of PutItem, 42 00:01:26,890 --> 00:01:29,370 ‫UpdateItem, and DeleteItem operations 43 00:01:29,370 --> 00:01:31,640 ‫as part of one transaction. 44 00:01:31,640 --> 00:01:34,440 ‫The use cases for DynamoDB transactions is any time you 45 00:01:34,440 --> 00:01:35,770 ‫would have ACID needs. 46 00:01:35,770 --> 00:01:36,603 ‫So for example, 47 00:01:36,603 --> 00:01:38,990 ‫for financial transactions, for managing orders, 48 00:01:38,990 --> 00:01:40,940 ‫for multiplayer games, et cetera, et cetera, 49 00:01:40,940 --> 00:01:43,960 ‫everywhere you need some sort of consistency. 50 00:01:43,960 --> 00:01:45,230 ‫So let's take an example. 51 00:01:45,230 --> 00:01:46,220 ‫So we have AccountBalance, 52 00:01:46,220 --> 00:01:48,530 ‫which presents the account ID, the balance, 53 00:01:48,530 --> 00:01:50,820 ‫so how many dollars they are on each accounts, 54 00:01:50,820 --> 00:01:54,300 ‫and the last time a transaction was made onto this account. 55 00:01:54,300 --> 00:01:56,950 ‫And then we have another table called BankTransactions, 56 00:01:56,950 --> 00:01:58,710 ‫which represents all the transactions that happened 57 00:01:58,710 --> 00:02:00,263 ‫to the bank, okay? 58 00:02:00,263 --> 00:02:02,120 ‫And there's a transaction ID, 59 00:02:02,120 --> 00:02:04,410 ‫a transaction timestamp, From Accounts, 60 00:02:04,410 --> 00:02:06,400 ‫To Accounts, and the amount. 61 00:02:06,400 --> 00:02:07,400 ‫And so the idea is 62 00:02:07,400 --> 00:02:09,820 ‫that we want to, obviously, add a transactions 63 00:02:09,820 --> 00:02:11,930 ‫and modify the AccountBalance at the same time. 64 00:02:11,930 --> 00:02:14,720 ‫So our application is going to do one transaction. 65 00:02:14,720 --> 00:02:15,553 ‫And as part of this one transaction, 66 00:02:15,553 --> 00:02:18,390 ‫is going to do an UpdateItem on the AccountBalance, 67 00:02:18,390 --> 00:02:20,980 ‫and a PutItem on the BankTransactions. 68 00:02:20,980 --> 00:02:22,418 ‫Now with the DynamoDB transaction, 69 00:02:22,418 --> 00:02:25,220 ‫either the transaction is returned to both tables 70 00:02:25,220 --> 00:02:27,000 ‫at the same time or to none. 71 00:02:27,000 --> 00:02:29,060 ‫And this is the power of transactions. 72 00:02:29,060 --> 00:02:30,140 ‫And we can see, 73 00:02:30,140 --> 00:02:32,090 ‫especially in a financial setting like this, 74 00:02:32,090 --> 00:02:33,380 ‫that it is really, really important 75 00:02:33,380 --> 00:02:36,290 ‫to have these level of transaction guarantees. 76 00:02:36,290 --> 00:02:38,620 ‫So now let's talk about capacity computations 77 00:02:38,620 --> 00:02:41,300 ‫for DynamoDB, which is super important for the exam. 78 00:02:41,300 --> 00:02:44,450 ‫So if you want to do three transactional writes per second, 79 00:02:44,450 --> 00:02:46,353 ‫and the item size is five kilobytes, 80 00:02:46,353 --> 00:02:49,560 ‫what is it going to be the WCU needed? 81 00:02:49,560 --> 00:02:52,930 ‫Well, you're going to need three times five divided by one, 82 00:02:52,930 --> 00:02:55,500 ‫because one WCU is one kilobyte, 83 00:02:55,500 --> 00:02:59,240 ‫times two because transactional operations 84 00:02:59,240 --> 00:03:00,780 ‫are twice as expensive, 85 00:03:00,780 --> 00:03:03,910 ‫which makes you a need for 30 WCUs. 86 00:03:03,910 --> 00:03:06,730 ‫Now, if you have five transactional reads per second, 87 00:03:06,730 --> 00:03:08,826 ‫with items of size five kilobytes, 88 00:03:08,826 --> 00:03:12,820 ‫then you need five times eight divided by four times two, 89 00:03:12,820 --> 00:03:15,720 ‫because again, a transactional operation 90 00:03:15,720 --> 00:03:19,340 ‫is twice as expensive as a strongly consistent read, 91 00:03:19,340 --> 00:03:22,090 ‫which gives you 20 RCUs. 92 00:03:22,090 --> 00:03:23,350 ‫And why did we get eight? 93 00:03:23,350 --> 00:03:26,240 ‫Well, five did get rounded to the upper four kilobyte 94 00:03:26,240 --> 00:03:27,890 ‫that we've seen from before. 95 00:03:27,890 --> 00:03:29,670 ‫So be familiar with these computations, 96 00:03:29,670 --> 00:03:32,177 ‫be understanding of what a transaction is in DynamoDB. 97 00:03:32,177 --> 00:03:33,930 ‫I can't really demo in the console, 98 00:03:33,930 --> 00:03:35,290 ‫but I hope you liked this lecture 99 00:03:35,290 --> 00:03:37,240 ‫and I will see you in the next lecture.