Amazon DynamoDB | Time to Live (TTL) Flashcards
What happens if I add a tag with a key that is same as one for an existing tag?
Time to Live (TTL)
Amazon DynamoDB | Database
Each DynamoDB table can only have up to one tag with the same key. If you add a tag with the same key as an existing tag, the existing tag is updated with the new value.
What is DynamoDB Time-to-Live (TTL)?
Time to Live (TTL)
Amazon DynamoDB | Database
DynamoDB Time-to-Live (TTL) is a mechanism that lets you set a specific timestamp to delete expired items from your tables. Once the timestamp expires, the corresponding item is marked as expired and is subsequently deleted from the table. By using this functionality, you do not have to track expired data and delete it manually. TTL can help you reduce storage usage and reduce the cost of storing data that is no longer relevant.
Why do I need to use TTL?
Time to Live (TTL)
Amazon DynamoDB | Database
There are two main scenarios where TTL can come in handy:
Deleting old data that is no longer relevant – data like event logs, usage history, session data, etc. when collected can get bloated over time and the old data though stored in the system may not be relevant any more. In such situations, you are better off clearing these stale records from the system and saving the money used for storing it.
Sometimes you may want data to be kept in DynamoDB for a specified time period in order to comply with your data retention and management policies. You might want to eventually delete this data once the obligated duration expires. Please do know however that TTL works on a best effort basis to ensure there is throughput available for other critical operations. DynamoDB will aim to delete expired items within a two-day period. The actual time taken may be longer based on the size of the data.
How does DynamoDB TTL work?
Time to Live (TTL)
Amazon DynamoDB | Database
To enable TTL for a table, first ensure that there is an attribute that can store the expiration timestamp for each item in the table. This timestamp needs to be in the epoch time format. This helps avoid time zone discrepancies between clients and servers.
DynamoDB runs a background scanner that monitors all the items. If the timestamp has expired, the process will mark the item as expired and queue it for subsequent deletion.
Note: TTL requires a numeric DynamoDB table attribute populated with an epoch timestamp to specify the expiration criterion for the data. You should be careful when setting a value for the TTL attribute since a wrong value could cause premature item deletion.
How do I specify TTL?
Time to Live (TTL)
Amazon DynamoDB | Database
To specify TTL, first enable the TTL setting on the table and specify the attribute to be used as the TTL value. As you add items to the table, you can specify a TTL attribute if you would like DynamoDB to automatically delete it after its expiration. This value is the expiry time, specified in epoch time format. DynamoDB takes care of the rest. TTL can be specified from the console from the overview tab for the table. Alternatively, developers can invoke the TTL API to configure TTL on the table. See our documentation and our API guide.
Can I set TTL on existing tables?
Time to Live (TTL)
Amazon DynamoDB | Database
Yes. If a table is already created and has an attribute that can be used as TTL for its items, then you only need to enable TTL for the table and designate the appropriate attribute for TTL. If the table does not have an attribute that can be used for TTL, you will have to create such an attribute and update the items with values for TTL.
Can I delete an entire table by setting TTL on the whole table?
Time to Live (TTL)
Amazon DynamoDB | Database
No. While you need to define an attribute to be used for TTL at the table level, the granularity for deleting data is at the item level. That is, each item in a table that needs to be deleted after expiry will need to have a value defined for the TTL attribute. There is no option to automatically delete the entire table.
Can I set TTL only for a subset of items in the table?
Time to Live (TTL)
Amazon DynamoDB | Database
Yes. TTL takes affect only for those items that have a defined value in the TTL attribute. Other items in the table remain unaffected.
What is the format for specifying TTL?
Time to Live (TTL)
Amazon DynamoDB | Database
The TTL value should use the epoch time format, which is number of seconds since January 1, 1970 UTC. If the value specified in the TTL attribute for an item is not in the right format, the value is ignored and the item won’t be deleted.
How can I read the TTL value for items in my table?
Time to Live (TTL)
Amazon DynamoDB | Database
The TTL value is just like any attribute on an item. It can be read the same way as any other attribute. In order to make it easier to visually confirm TTL values, the DynamoDB Console allows you to hover over a TTL attribute to see its value in human-readable local and UTC time.
Can I create an index based on the TTL values assigned to items in a table?
Time to Live (TTL)
Amazon DynamoDB | Database
Yes. TTL behaves like any other item attribute. You can create indexes the same as with other item attributes.
Can the TTL attribute be projected to an index?
Time to Live (TTL)
Amazon DynamoDB | Database
Yes. TTL attribute can be projected onto an index just like any other attribute.
Can I edit the TTL attribute value once it has been set for an item?
Time to Live (TTL)
Amazon DynamoDB | Database
Yes. You can modify the TTL attribute value just as you modify any other attribute on an item.
Can I change the TTL attribute for a table?
Time to Live (TTL)
Amazon DynamoDB | Database
Yes. If a table already has TTL enabled and you want to specify a different TTL attribute, then you need to disable TTL for the table first, then you can re-enable TTL on the table with a new TTL attribute. Note that Disabling TTL can take up to one hour to apply across all partitions, and you will not be able to re-enable TTL until this action is complete.
Can I use AWS Management Console to view and edit the TTL values?
Time to Live (TTL)
Amazon DynamoDB | Database
Yes. The AWS Management Console allows you to easily view, set or update the TTL value.