I want to enable TTL on the DyanmoDB records. However, the TTL should kick in only when the record is updated to a particular state {status: completed}
.
Is it possible to first create the record with no TTL in sight (or to set the TTL to a long time in the future) and then to later update it to N days from now when the record is updated to {status: completed}
?
2
Answers
Yes, it is doable. I believe
status
would be one of the columns of the record. The only limitation of this approach would be, you will have to keep the logic of setting up TTL value basisstatus
value inside your service/application. DynamoDB can’t do it on its own.You can use the following workaround:
expireAfter
column as the TTL attribute.un-complete
status. Then you have two choicesexpireAfter
attribute and save it into DDB.expireAfter
attribute and save it into DDB. During internal records cleaning activity, DDB will ignore all of those records where TTL attribute is missing.complete
, then make sure you have setexpireAfter
value properly. And, save it. DDB will take care of it automatically.Hope this clarifies your doubts
Few AWS official links, that have explained few more things in details:
Yes, the TTL field can be updated to shorten or lengthen a record’s life.
As the docs explain, a "background process automatically and continuously evaluates the expiry status of items in the table". One process marks expired records as deletable and a second actually deletes the records. The deletion step "typically" occurs within 48 hours of expiry, although usually much faster. "Items that are past their expiration, but have not yet been deleted can still be updated, and successful updates to change or remove the expiration attribute will be honored".
DynamoDB ignores TTL values more than 5 years older than the current time. So setting a TTL to the far past or far future will work for your uncompleted records.