1
00:00:00,180 --> 00:00:02,290
<v ->So a few additional lectures on DynamoDB</v>

2
00:00:02,290 --> 00:00:04,180
because there are some more advanced questions now

3
00:00:04,180 --> 00:00:06,500
that are being asked of you at the exam.

4
00:00:06,500 --> 00:00:09,190
So the first feature I want to talk to you about is the TTL

5
00:00:09,190 --> 00:00:10,710
or Time to Live.

6
00:00:10,710 --> 00:00:13,290
The TTL means that you define a column

7
00:00:13,290 --> 00:00:16,160
and based on that column, the items that have

8
00:00:16,160 --> 00:00:19,270
an expiry date based on that column will expire

9
00:00:19,270 --> 00:00:20,810
and get deleted from the table.

10
00:00:20,810 --> 00:00:22,960
Hence the name, Time to Live.

11
00:00:22,960 --> 00:00:25,760
So the Time to Live feature in DynamoDB is provided to you

12
00:00:25,760 --> 00:00:28,340
at no extra cost and the deletions will not use

13
00:00:28,340 --> 00:00:31,270
any of the WCU or RCU.

14
00:00:31,270 --> 00:00:34,940
It's a background task and the DynamoDB service itself

15
00:00:34,940 --> 00:00:36,550
does it periodically

16
00:00:36,550 --> 00:00:39,040
and why would you use TTL, well maybe to reduce

17
00:00:39,040 --> 00:00:41,490
the storage and manage the table size over time

18
00:00:41,490 --> 00:00:43,850
because at some point, maybe you don't need a row

19
00:00:43,850 --> 00:00:46,580
to be in DynamoDB because it would have expired.

20
00:00:46,580 --> 00:00:49,370
It could also help you to adhere to regulatory norms.

21
00:00:49,370 --> 00:00:51,470
For example, if you need to keep (mumbles)

22
00:00:51,470 --> 00:00:53,620
only for seven days in some table.

23
00:00:53,620 --> 00:00:55,893
And TTL must be enabled per row so you define

24
00:00:55,893 --> 00:00:58,490
your TTL column as we'll see in the hands-on

25
00:00:58,490 --> 00:01:00,410
and we'll add a date there and we'll see

26
00:01:00,410 --> 00:01:01,670
in which format in a second.

27
00:01:01,670 --> 00:01:03,646
And then, on the pro basis,

28
00:01:03,646 --> 00:01:06,570
DynamoDB will delete these rows based on the expiration.

29
00:01:06,570 --> 00:01:09,030
So typically, it can happen pretty quickly but the guarantee

30
00:01:09,030 --> 00:01:12,490
is that DynamoDB will delete or will try to its best

31
00:01:12,490 --> 00:01:15,910
to delete a row within 48 hours of expiration,

32
00:01:15,910 --> 00:01:18,920
so it won't happen the second after the item expires

33
00:01:18,920 --> 00:01:21,420
but within 48 hours it should happen.

34
00:01:21,420 --> 00:01:24,410
The deleted items due to TTL will also be deleted

35
00:01:24,410 --> 00:01:28,020
from any indexes, so your GSI or LSI.

36
00:01:28,020 --> 00:01:31,160
And if you wanted to somehow recover these deleted items,

37
00:01:31,160 --> 00:01:34,000
we just saw what streams is, so DynamoDB streams

38
00:01:34,000 --> 00:01:36,000
upon a delete due to TTL.

39
00:01:36,000 --> 00:01:38,810
We'll have an event and maybe you can recover

40
00:01:38,810 --> 00:01:40,800
these expired items if you wanted to.

41
00:01:40,800 --> 00:01:41,880
So that's it for all the theory.

42
00:01:41,880 --> 00:01:44,580
Let's just go into the practice to see how TTL works.

43
00:01:44,580 --> 00:01:46,490
So let's go to creating a new table

44
00:01:46,490 --> 00:01:50,300
and I'll call it DemoTTL and I'll have user ID

45
00:01:50,300 --> 00:01:51,980
as my partition key.

46
00:01:51,980 --> 00:01:53,360
I will not use the default settings.

47
00:01:53,360 --> 00:01:54,470
I will choose provisioned.

48
00:01:54,470 --> 00:01:56,850
I will not have auto scaling and I will just have

49
00:01:56,850 --> 00:01:59,910
one read capacity unit and one write capacity unit.

50
00:01:59,910 --> 00:02:02,250
I'll choose the default for the encryption at rest

51
00:02:02,250 --> 00:02:03,420
and click on Create.

52
00:02:03,420 --> 00:02:04,770
Okay, so here is my table.

53
00:02:04,770 --> 00:02:05,923
It is being created.

54
00:02:06,820 --> 00:02:09,650
And now lets go to items and I'm going to create an item.

55
00:02:09,650 --> 00:02:12,720
So user ID, I'll say user_123.

56
00:02:12,720 --> 00:02:16,640
And maybe you'll have a name, it's Stephane.

57
00:02:16,640 --> 00:02:19,730
And here, I'm going to add an expire_on attribute.

58
00:02:19,730 --> 00:02:22,300
Now you can name this column whatever you want

59
00:02:22,300 --> 00:02:26,540
but it has to be a number and you'll set expire_on.

60
00:02:26,540 --> 00:02:28,255
And I will choose expire_on name

61
00:02:28,255 --> 00:02:31,320
because it's pretty obvious about what it does.

62
00:02:31,320 --> 00:02:34,920
So, now you do time to epoch on Google,

63
00:02:34,920 --> 00:02:36,960
and you have this website called epochconverter.com

64
00:02:36,960 --> 00:02:38,110
which is a great website.

65
00:02:38,110 --> 00:02:40,440
It allows you to take a time stamp, for example this one,

66
00:02:40,440 --> 00:02:43,620
and it gives you a number which represents an epoch time

67
00:02:43,620 --> 00:02:47,210
which is a number of milliseconds since the year '70.

68
00:02:47,210 --> 00:02:49,410
So, what we'll do is that we'll set okay,

69
00:02:49,410 --> 00:02:53,580
this one will expire at time 58, human to date timestamp

70
00:02:53,580 --> 00:02:55,730
and here's my epoch timestamp.

71
00:02:55,730 --> 00:02:58,640
And so what I'm going to do is copy this here.

72
00:02:58,640 --> 00:03:00,860
So this item should expire in five minutes.

73
00:03:00,860 --> 00:03:04,540
I'll create another item, maybe for user_234.

74
00:03:04,540 --> 00:03:09,540
And for its name, I will say Laura and the name is Laura.

75
00:03:10,430 --> 00:03:13,400
And then finally, again I will add an expire_on number

76
00:03:13,400 --> 00:03:16,380
so expire_on, and this time I'm going to select

77
00:03:16,380 --> 00:03:19,210
in one hour from now, so here we go.

78
00:03:19,210 --> 00:03:20,590
I paste this one here.

79
00:03:20,590 --> 00:03:21,660
Click on Save.

80
00:03:21,660 --> 00:03:25,180
And now I have two rows with two expire_on attributes.

81
00:03:25,180 --> 00:03:28,700
So I go to Overview, and I click on Time to Live attribute

82
00:03:28,700 --> 00:03:30,480
and I click on Manage TTL.

83
00:03:30,480 --> 00:03:33,010
Here, I will say that my column to look out

84
00:03:33,010 --> 00:03:35,440
for the TTL is expire_on

85
00:03:35,440 --> 00:03:38,300
and I could enable DynamoDB streams if I wanted

86
00:03:38,300 --> 00:03:41,300
to receive that as an update whenever it gets deleted.

87
00:03:41,300 --> 00:03:43,130
But here we can run a preview and say,

88
00:03:43,130 --> 00:03:46,100
show me the items that will expire by right now.

89
00:03:46,100 --> 00:03:49,250
If I run the preview, it says no items are matching.

90
00:03:49,250 --> 00:03:53,360
But if I go to a few minutes from now and do a run review

91
00:03:53,360 --> 00:03:55,110
it says I found one item

92
00:03:55,110 --> 00:03:57,960
this one that should expire by these dates.

93
00:03:57,960 --> 00:04:00,720
And if I go even further and run again the preview,

94
00:04:00,720 --> 00:04:03,020
now Laura is also going to be expired.

95
00:04:03,020 --> 00:04:05,260
So that TTL attribute just works fine

96
00:04:05,260 --> 00:04:06,480
and you can simulate here

97
00:04:06,480 --> 00:04:08,440
and when you're ready, click on Continue.

98
00:04:08,440 --> 00:04:11,230
And here we go, now it's being enabled.

99
00:04:11,230 --> 00:04:12,540
And so what will happen is that

100
00:04:12,540 --> 00:04:15,010
as we add items to this table,

101
00:04:15,010 --> 00:04:17,740
they will get deleted by DynamoDB in the background

102
00:04:17,740 --> 00:04:20,710
due to the TTL value on this column expire_on.

103
00:04:20,710 --> 00:04:21,990
So that's it, all you need to know.

104
00:04:21,990 --> 00:04:24,840
I hope that helps and I will see you in the next lecture.

