1 00:00:00,000 --> 00:00:01,680 ‫So, now, let's talk about Time to Live. 2 00:00:01,680 --> 00:00:04,710 ‫And Time to Live allows you to automatically delete items 3 00:00:04,710 --> 00:00:06,700 ‫after an expiry timestamp. 4 00:00:06,700 --> 00:00:08,490 ‫So the idea is that you have a column you'll define, 5 00:00:08,490 --> 00:00:11,170 ‫and then when you say the time of right now 6 00:00:11,170 --> 00:00:13,630 ‫goes over the value of this column 7 00:00:13,630 --> 00:00:15,290 ‫then please remove the item. 8 00:00:15,290 --> 00:00:18,270 ‫So having an item being deleted by your Time to Live 9 00:00:18,270 --> 00:00:20,650 ‫constraint does not consume any WCUs, 10 00:00:20,650 --> 00:00:22,110 ‫so there is no extra cost. 11 00:00:22,110 --> 00:00:24,900 ‫And this timestamp must be a number that represents 12 00:00:24,900 --> 00:00:28,150 ‫the Unix Epoch timestamp value as we'll see in the hands-on. 13 00:00:28,150 --> 00:00:30,370 ‫Now expired items will not be expired right away, 14 00:00:30,370 --> 00:00:31,970 ‫there's the guarantee that they will be expired 15 00:00:31,970 --> 00:00:35,520 ‫within 48 hours of the expiration, okay, 16 00:00:35,520 --> 00:00:38,670 ‫but in real life it's actually pretty good. 17 00:00:38,670 --> 00:00:40,047 ‫So if you look at a table, for example, 18 00:00:40,047 --> 00:00:41,830 ‫a SessionData table that has two columns, 19 00:00:41,830 --> 00:00:45,070 ‫User_ID and Session_ID, we want to add an expire time, 20 00:00:45,070 --> 00:00:47,160 ‫which is going to be a TTL of our table, 21 00:00:47,160 --> 00:00:50,810 ‫and we're going to define when each session will expire. 22 00:00:50,810 --> 00:00:52,250 ‫Now what happens is that when you go, 23 00:00:52,250 --> 00:00:55,060 ‫and there is an expiration process in DynamoDB, 24 00:00:55,060 --> 00:00:56,150 ‫it will look at the current time 25 00:00:56,150 --> 00:00:57,287 ‫and say the current time is this, 26 00:00:57,287 --> 00:01:00,670 ‫and it will mark, scan the table, and expire items 27 00:01:00,670 --> 00:01:04,560 ‫that are going to, obviously, have a TTL Epoch time, 28 00:01:04,560 --> 00:01:06,150 ‫less than the time right now. 29 00:01:06,150 --> 00:01:09,250 ‫And then the second process will scan and delete these items 30 00:01:09,250 --> 00:01:11,610 ‫from the table, and this is how TTL works. 31 00:01:11,610 --> 00:01:14,110 ‫Now these expired items, that haven't been deleted, 32 00:01:14,110 --> 00:01:16,520 ‫will still appear in the reads, query and scan, 33 00:01:16,520 --> 00:01:17,580 ‫so if you don't want them 34 00:01:17,580 --> 00:01:19,130 ‫you need to do some Client Side Filtering. 35 00:01:19,130 --> 00:01:20,610 ‫So there could be some expired items 36 00:01:20,610 --> 00:01:21,700 ‫already in your queries. 37 00:01:21,700 --> 00:01:25,630 ‫You need to wait up to 40 hours to see it being deleted. 38 00:01:25,630 --> 00:01:27,330 ‫And when the items are deleted 39 00:01:27,330 --> 00:01:28,760 ‫they're also deleted from your indexes, 40 00:01:28,760 --> 00:01:29,952 ‫so your Local Secondary Indexes 41 00:01:29,952 --> 00:01:32,290 ‫and your Global Secondary Indexes. 42 00:01:32,290 --> 00:01:34,200 ‫And a delete operation for each expired item 43 00:01:34,200 --> 00:01:36,050 ‫enters the DynamoDB stream. 44 00:01:36,050 --> 00:01:37,830 ‫That means that any item that gets deleted, 45 00:01:37,830 --> 00:01:39,890 ‫thanks to the TTL, will be in a stream 46 00:01:39,890 --> 00:01:41,960 ‫and you could recover it if you wanted to. 47 00:01:41,960 --> 00:01:44,900 ‫Now the use cases for TTL would be to reduce store data 48 00:01:44,900 --> 00:01:47,130 ‫by keeping only current items, to adhere to 49 00:01:47,130 --> 00:01:49,140 ‫regulatory obligations, or, for example, 50 00:01:49,140 --> 00:01:52,460 ‫for the SessionData it is a perfect use case of TTL. 51 00:01:52,460 --> 00:01:53,880 ‫Okay, so, let's have a look at how we can define 52 00:01:53,880 --> 00:01:55,730 ‫a TTL in DynamoDB. 53 00:01:55,730 --> 00:01:57,350 ‫So let's go ahead and create a table, 54 00:01:57,350 --> 00:01:59,740 ‫and I'll call this one DemoTTL. 55 00:01:59,740 --> 00:02:01,820 ‫Now the Partition key is going to be user_id, 56 00:02:01,820 --> 00:02:03,680 ‫and we don't need a Sort key right now. 57 00:02:03,680 --> 00:02:05,920 ‫Okay, we'll customize the settings, 58 00:02:05,920 --> 00:02:08,500 ‫we'll set provisions and Auto scaling off. 59 00:02:08,500 --> 00:02:12,460 ‫We'll have one RCU and one WCU, 60 00:02:12,460 --> 00:02:14,893 ‫and then we'll go ahead and create this table. 61 00:02:16,720 --> 00:02:19,010 ‫So my table is now created, and what I'm going to do 62 00:02:19,010 --> 00:02:21,540 ‫is just add a little bit of data. 63 00:02:21,540 --> 00:02:23,540 ‫So I'm going to insert some items. 64 00:02:23,540 --> 00:02:28,100 ‫So I'm going to create an item, user_id is john_123, 65 00:02:28,100 --> 00:02:31,130 ‫and then I'm going to add an attribute, for example, 66 00:02:31,130 --> 00:02:32,890 ‫name is going to be John. 67 00:02:32,890 --> 00:02:35,000 ‫And, then, I want to have an expire 68 00:02:35,000 --> 00:02:36,980 ‫on Attributes, so expire_on. 69 00:02:36,980 --> 00:02:38,020 ‫And, essentially, it's not a string 70 00:02:38,020 --> 00:02:39,830 ‫it's going to be a number. 71 00:02:39,830 --> 00:02:42,250 ‫So we do expire_on, and then we need to give 72 00:02:42,250 --> 00:02:44,550 ‫an expiration date to John. 73 00:02:44,550 --> 00:02:48,840 ‫So I call it epoch converter, online, 74 00:02:48,840 --> 00:02:50,850 ‫and let's just get the first one. 75 00:02:50,850 --> 00:02:53,770 ‫And, so, this is how we can translate a timestamp 76 00:02:53,770 --> 00:02:56,900 ‫of soon into an Epoch timestamp, 77 00:02:56,900 --> 00:02:59,000 ‫which I can enter into DynamoDB. 78 00:02:59,000 --> 00:03:02,200 ‫So, for example, if I take, say, five minutes from now, 79 00:03:02,200 --> 00:03:05,360 ‫so here we go, and I do Human to Timestamp 80 00:03:05,360 --> 00:03:08,550 ‫here is the Epoch value of five minutes from now. 81 00:03:08,550 --> 00:03:11,970 ‫So I'm going to paste that in and click on Create item. 82 00:03:11,970 --> 00:03:13,780 ‫So that's one item that has been created, 83 00:03:13,780 --> 00:03:15,450 ‫and I will create a second item. 84 00:03:15,450 --> 00:03:19,750 ‫And this one is alice_456, has a user_id, 85 00:03:19,750 --> 00:03:23,140 ‫while Alice's name is going to be Alice, 86 00:03:23,140 --> 00:03:26,103 ‫and we need to, again, have an expire_on, 87 00:03:27,480 --> 00:03:28,870 ‫and the expire_on, we can say, 88 00:03:28,870 --> 00:03:30,690 ‫for example, one hour from now. 89 00:03:30,690 --> 00:03:32,200 ‫So I will have a 10, here, 90 00:03:32,200 --> 00:03:34,480 ‫and click on Human date to Timestamp, 91 00:03:34,480 --> 00:03:37,950 ‫take this in and paste it and Create item. 92 00:03:37,950 --> 00:03:41,250 ‫So now we have two rows in our DynamoDB table, okay, 93 00:03:41,250 --> 00:03:44,340 ‫and both of them have a different expire_on attributes. 94 00:03:44,340 --> 00:03:48,500 ‫Now we need to go ahead and define the TTL on our table. 95 00:03:48,500 --> 00:03:49,630 ‫So, if we look at our table, 96 00:03:49,630 --> 00:03:52,230 ‫we can see the Time to Live is currently disabled. 97 00:03:52,230 --> 00:03:55,990 ‫So what I can do is go to Additional settings, scroll down, 98 00:03:55,990 --> 00:04:00,360 ‫and then look for the Time to Live and click on Enable. 99 00:04:00,360 --> 00:04:02,460 ‫Now we need to give the TTL attribute name, 100 00:04:02,460 --> 00:04:05,770 ‫so what is the data that I want to expire? 101 00:04:05,770 --> 00:04:06,770 ‫The column I want to expire on, 102 00:04:06,770 --> 00:04:09,030 ‫so expire_on is the name that I gave 103 00:04:09,030 --> 00:04:11,110 ‫to this TTL attribute name. 104 00:04:11,110 --> 00:04:13,690 ‫And, then, we could run a preview, okay. 105 00:04:13,690 --> 00:04:15,260 ‫So if you run a preview right now, 106 00:04:15,260 --> 00:04:17,740 ‫it says that these two items will be deleted in one hour. 107 00:04:17,740 --> 00:04:20,630 ‫So, if we take, for example, the time right now, 108 00:04:20,630 --> 00:04:23,010 ‫which is right here, and I do run preview, 109 00:04:23,010 --> 00:04:26,750 ‫it says, hey, there's no items that I want to delete, right. 110 00:04:26,750 --> 00:04:30,080 ‫But if we do one hour from now, so if we do, for example, 111 00:04:30,080 --> 00:04:34,630 ‫10, 10:10, and it gets this Epoch timestamp, right here, 112 00:04:34,630 --> 00:04:38,780 ‫and paste it, Run preview, only John will be expired, okay. 113 00:04:38,780 --> 00:04:42,810 ‫And then if I go a bit after, so if I go to 10:50 114 00:04:42,810 --> 00:04:45,260 ‫and take your Human timestamp, go to timestamp 115 00:04:45,260 --> 00:04:47,420 ‫and paste this in, run a preview. 116 00:04:47,420 --> 00:04:50,820 ‫Now two items will be deleted, and you could either specify 117 00:04:50,820 --> 00:04:54,630 ‫an Epoch value, or a Custom time, or in the next 60 minutes, 118 00:04:54,630 --> 00:04:56,530 ‫24 hours, or seven days, which is quite cool 119 00:04:56,530 --> 00:04:58,140 ‫for running some simulations. 120 00:04:58,140 --> 00:05:02,290 ‫So we can Enable TTL, and now TTL is enabled 121 00:05:02,290 --> 00:05:04,250 ‫and automatically, if I wait for one hour, 122 00:05:04,250 --> 00:05:07,500 ‫my items are going to be completely expired, okay. 123 00:05:07,500 --> 00:05:08,340 ‫So this is pretty cool. 124 00:05:08,340 --> 00:05:09,630 ‫And we can do a graph of all the items 125 00:05:09,630 --> 00:05:11,480 ‫deleted it in the last 24 hours, 126 00:05:11,480 --> 00:05:13,850 ‫thanks to this feature right here, okay. 127 00:05:13,850 --> 00:05:16,120 ‫So this is a CloudWatch metric. 128 00:05:16,120 --> 00:05:18,290 ‫So that's it for this lecture, I hope you liked it, 129 00:05:18,290 --> 00:05:20,240 ‫and I will see you in the next lecture.