‫So, now, let's talk about Time to Live.
‫And Time to Live allows you to automatically delete items
‫after an expiry timestamp.
‫So the idea is that you have a column you'll define,
‫and then when you say the time of right now
‫goes over the value of this column
‫then please remove the item.
‫So having an item being deleted by your Time to Live
‫constraint does not consume any WCUs,
‫so there is no extra cost.
‫And this timestamp must be a number that represents
‫the Unix Epoch timestamp value as we'll see in the hands-on.
‫Now expired items will not be expired right away,
‫there's the guarantee that they will be expired
‫within 48 hours of the expiration, okay,
‫but in real life it's actually pretty good.
‫So if you look at a table, for example,
‫a SessionData table that has two columns,
‫User_ID and Session_ID, we want to add an expire time,
‫which is going to be a TTL of our table,
‫and we're going to define when each session will expire.
‫Now what happens is that when you go,
‫and there is an expiration process in DynamoDB,
‫it will look at the current time
‫and say the current time is this,
‫and it will mark, scan the table, and expire items
‫that are going to, obviously, have a TTL Epoch time,
‫less than the time right now.
‫And then the second process will scan and delete these items
‫from the table, and this is how TTL works.
‫Now these expired items, that haven't been deleted,
‫will still appear in the reads, query and scan,
‫so if you don't want them
‫you need to do some Client Side Filtering.
‫So there could be some expired items
‫already in your queries.
‫You need to wait up to 40 hours to see it being deleted.
‫And when the items are deleted
‫they're also deleted from your indexes,
‫so your Local Secondary Indexes
‫and your Global Secondary Indexes.
‫And a delete operation for each expired item
‫enters the DynamoDB stream.
‫That means that any item that gets deleted,
‫thanks to the TTL, will be in a stream
‫and you could recover it if you wanted to.
‫Now the use cases for TTL would be to reduce store data
‫by keeping only current items, to adhere to
‫regulatory obligations, or, for example,
‫for the SessionData it is a perfect use case of TTL.
‫Okay, so, let's have a look at how we can define
‫a TTL in DynamoDB.
‫So let's go ahead and create a table,
‫and I'll call this one DemoTTL.
‫Now the Partition key is going to be user_id,
‫and we don't need a Sort key right now.
‫Okay, we'll customize the settings,
‫we'll set provisions and Auto scaling off.
‫We'll have one RCU and one WCU,
‫and then we'll go ahead and create this table.
‫So my table is now created, and what I'm going to do
‫is just add a little bit of data.
‫So I'm going to insert some items.
‫So I'm going to create an item, user_id is john_123,
‫and then I'm going to add an attribute, for example,
‫name is going to be John.
‫And, then, I want to have an expire
‫on Attributes, so expire_on.
‫And, essentially, it's not a string
‫it's going to be a number.
‫So we do expire_on, and then we need to give
‫an expiration date to John.
‫So I call it epoch converter, online,
‫and let's just get the first one.
‫And, so, this is how we can translate a timestamp
‫of soon into an Epoch timestamp,
‫which I can enter into DynamoDB.
‫So, for example, if I take, say, five minutes from now,
‫so here we go, and I do Human to Timestamp
‫here is the Epoch value of five minutes from now.
‫So I'm going to paste that in and click on Create item.
‫So that's one item that has been created,
‫and I will create a second item.
‫And this one is alice_456, has a user_id,
‫while Alice's name is going to be Alice,
‫and we need to, again, have an expire_on,
‫and the expire_on, we can say,
‫for example, one hour from now.
‫So I will have a 10, here,
‫and click on Human date to Timestamp,
‫take this in and paste it and Create item.
‫So now we have two rows in our DynamoDB table, okay,
‫and both of them have a different expire_on attributes.
‫Now we need to go ahead and define the TTL on our table.
‫So, if we look at our table,
‫we can see the Time to Live is currently disabled.
‫So what I can do is go to Additional settings, scroll down,
‫and then look for the Time to Live and click on Enable.
‫Now we need to give the TTL attribute name,
‫so what is the data that I want to expire?
‫The column I want to expire on,
‫so expire_on is the name that I gave
‫to this TTL attribute name.
‫And, then, we could run a preview, okay.
‫So if you run a preview right now,
‫it says that these two items will be deleted in one hour.
‫So, if we take, for example, the time right now,
‫which is right here, and I do run preview,
‫it says, hey, there's no items that I want to delete, right.
‫But if we do one hour from now, so if we do, for example,
‫10, 10:10, and it gets this Epoch timestamp, right here,
‫and paste it, Run preview, only John will be expired, okay.
‫And then if I go a bit after, so if I go to 10:50
‫and take your Human timestamp, go to timestamp
‫and paste this in, run a preview.
‫Now two items will be deleted, and you could either specify
‫an Epoch value, or a Custom time, or in the next 60 minutes,
‫24 hours, or seven days, which is quite cool
‫for running some simulations.
‫So we can Enable TTL, and now TTL is enabled
‫and automatically, if I wait for one hour,
‫my items are going to be completely expired, okay.
‫So this is pretty cool.
‫And we can do a graph of all the items
‫deleted it in the last 24 hours,
‫thanks to this feature right here, okay.
‫So this is a CloudWatch metric.
‫So that's it for this lecture, I hope you liked it,
‫and I will see you in the next lecture.