Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB condition ttl

Does DynamoDB support TTL based on specific conditions? For example, I want the TTL to be enabled on records with a specific attribute and no TTL on records which does not have a value for that attribute.

The docs I've currently read to look into this issue are

  1. http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/howitworks-ttl.html
  2. http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-how-to.html

Both the docs indicate no specific information on that.

Thanks,

like image 957
chrisrhyno2003 Avatar asked Sep 27 '17 21:09

chrisrhyno2003


People also ask

What is the default TTL for DynamoDB?

TTL typically deletes expired items within 48 hours of expiration.

Can you update TTL in DynamoDB?

The key statement, as you pointed out, is at How It Works: DynamoDB Time to Live (TTL): 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.

How do I add TTL to DynamoDB table?

Sign in to the AWS Management Console and open the DynamoDB console at https://console.aws.amazon.com/dynamodb/ . Choose Tables, and then choose the table that you want to modify. In the Additional settings tab, in the Time to Live (TTL) section, choose Enable.

How accurate is DynamoDB TTL?

Yes according to the link you sent. DynamoDB typically deletes expired items within 48 hours of expiration. The exact duration within which an item truly gets deleted after expiration is specific to the nature of the workload and the size of the table.


2 Answers

Remember that DynamoDB is schemaless. All records do not have to have the same fields. So, only create the TTL field on the records which you wish to have expired. When the record is updated such that your "condition" is met, add the TTL field.

like image 98
JimB Avatar answered Sep 28 '22 07:09

JimB


TTL compares a non-empty filed (which has been set as TTL field) in DynamoDB with current timestamp, delete the item if its TTL field is earlier than current time stamp.

A typical way to use DynamoDB TTL, is to create a field (maybe name it as ExpirationTime) in you data object, add set it as (currentEpochTime + TimeToLive). After TimeToLive seconds, DynamoDB would find the value less than current EpochTime, and do the delete action (expect some delay).

Having said that, if you want to enable TTL after this table has been used by adding a new field to original data object. Existing data will not be affected as they do not have that field. You may want to run a script to add that new field value to all existing items to make TTL work for existing data.

like image 29
Feng Han Avatar answered Sep 28 '22 07:09

Feng Han