9

Hi I am wonder about how to set the time to live in dynamo db.

I understand that I have to create a field, could be called ttl and set the value to be deleted, but in which format innodejs do I have to save to use for the ttl field for 20 or 24 hours?

Thanks for help.

Pablo Alejandro
  • 591
  • 2
  • 10
  • 19

1 Answers1

22

From the official DynamoDB documentation:

TTL compares the current time in epoch time format to the time stored in the Time To Live attribute of an item. [...]

Note

The epoch time format is the number of seconds elapsed since 12:00:00 AM January 1st, 1970 UTC.

In Javascript, you can get the number of milliseconds since the epoch by doing Date.now(). Once you have that, you can divide everything by 1000 to get the seconds (also rounding to the nearest integer value) and finally add the number of seconds in the TTL that you want.

This means that if you want to set the expiration time 24 from now, you can easily set the TTL field with the value expirationTime calculated this way:

const SECONDS_IN_AN_HOUR = 60 * 60;
const secondsSinceEpoch = Math.round(Date.now() / 1000);
const expirationTime = secondsSinceEpoch + 24 * SECONDS_IN_AN_HOUR;
Community
  • 1
  • 1
Stefano Dalpiaz
  • 1,673
  • 10
  • 11
  • Ok, thanks a lot, It is the same of use: `const current = new Date();` then `const expirationTime = new Date(current.getTime() + 86400000);` and `Math.floor(ttl.getTime() / 1000)`? – Pablo Alejandro Jul 31 '18 at 13:36
  • Hi, again, could you explain me why dynamodb only delete the fields of an id with ttl and not delete the entire record? – Pablo Alejandro Aug 06 '18 at 15:31
  • What do you mean only delete the fields? When DynamoDB detects that a record has passed its TTL time, it will mark the record as expired. The actual deletion, according to the docs (see link in the answer), will happen within 48 hours. Until that happens, the field will still be available for query/update operations. Does this answer your question? – Stefano Dalpiaz Aug 07 '18 at 07:26
  • yes, thank you but in dynamo the ttl field remove just the fields of the id, not delete the entire record, just keep the id with empty fields – Pablo Alejandro Aug 07 '18 at 23:09
  • Are you saying that once an item with TTL has expired, you can still find the item with the same ID, but with no other attributes? Is this from your experience or did you read it somewhere? – Stefano Dalpiaz Aug 07 '18 at 23:15
  • myes by my experience, if I have id: my-id, value: [object], ttl: 1533805368. Once the ttl expire the record stay like this: id: my-id, value: [], ttl: empty – Pablo Alejandro Aug 08 '18 at 15:31
  • That sounds strange to me. Anyway, since it's difficult to provide details in comments, and also this is getting a bit off-topic from the original question, I suggest that you open a new question where you provide all the details of this problem, including the code that you use to retrieve the data. – Stefano Dalpiaz Aug 09 '18 at 02:12
  • Hi, sorry the problem was with the capacity units not the ttl, when the data exceded the capacity units, dynamodb fields are empty. Thanks for help – Pablo Alejandro Aug 12 '18 at 21:22