How it works: Amazon Keyspaces Time to Live (TTL) - Amazon Keyspaces (for Apache Cassandra)

How it works: Amazon Keyspaces Time to Live (TTL)

Amazon Keyspaces Time to Live (TTL) is fully managed. You don’t have to manage low-level system settings such as compaction strategies. Data expires at the time you specify, and Amazon Keyspaces removes expired data automatically (typically within 10 days) without impacting your application performance or availability.

Expired data is marked for deletion and isn't available for data manipulation language (DML) statements. As you continue to perform reads and writes on rows that contain expired data, the expired data continues to count towards read capacity units (RCUs) and write capacity units (WCUs) until it's deleted from storage.

Setting the default TTL value for a table

In Amazon Keyspaces, you can set a default TTL value for all rows in a table when the table is created. You can also edit an existing table to set or change the default TTL value for new rows inserted into the table. Changing the default TTL value of a table doesn't modify the TTL value of any existing data in the table. The default TTL value for a table is zero, which means that data doesn't expire automatically. If the default TTL value for a table is greater than zero, an expiration timestamp is added to each row.

Amazon Keyspaces calculates a new TTL timestamp each time the data is updated. TTL values are set in seconds, and the maximum configurable value is 630,720,000 seconds, which is the equivalent of 20 years. For more information about how to set, modify, and disable the default TTL value for tables using the AWS Management Console or CQL, see How to use Time to Live (TTL).

Setting custom TTL values for rows and columns

Note

Before setting custom TTL values for rows and columns, TTL must be enabled on the table first. For more information, see How to enable Time to Live (TTL) on existing tables using custom properties.

To overwrite a table's default TTL value or to set expiration dates for individual rows, you can use the following CQL data manipulation language (DML) statements:

  • INSERT – Use to insert a new row of data with a TTL value set.

  • UPDATE – Use to modify an existing row of data with a new TTL value.

Setting TTL values for rows takes precedence over the default TTL setting for the table.

For CQL syntax and examples, see To use INSERT to edit custom Time to Live (TTL) settings using CQL.

To overwrite or set TTL values for individual columns, you can update the TTL setting for a subset of columns within existing rows using the following CQL DML statement:

  • UPDATE – Use to update a column of data.

Setting TTL values for columns takes precedence over the default TTL setting for the table and any custom TTL setting for the row. For CQL syntax and examples, see To use UPDATE to edit custom Time to Live (TTL) settings using CQL.

Enabling TTL on tables

TTL is automatically enabled for tables when you specify a default_time_to_live value greater than 0 in either CREATE TABLE or ALTER TABLE statements. If you don’t specify a default_time_to_live for the table, but you want to specify custom TTL values for rows or columns by using INSERT or UPDATE operations, you must first enable TTL for the table. You can enable TTL for a table using the ttl custom property.

When you enable TTL on a table, Amazon Keyspaces begins to store additional TTL-related metadata for each row. In addition, TTL uses expiration timestamps to track when rows or columns expire. The timestamps are stored as row metadata and contribute to the storage cost for the row.

Enabling TTL also requires Amazon Keyspaces to read a row before performing write operations on that row to ensure data accuracy. For example, to write a 3-KB row requires 1 RCU/RRU in addition to 3 WCUs/WRUs. After the TTL feature is enabled, you can't disable it for a table. Setting the table’s default_time_to_live to 0 disables default expiration times for new data, but it doesn't deactivate the TTL feature or revert the table back to the original Amazon Keyspaces storage metadata or write behavior.

Amazon Keyspaces Time to Live and integration with AWS services

The following TTL metric is available in Amazon CloudWatch to enable continuous monitoring.

  • TTLDeletes – The units consumed to delete or update data in a row by using Time to Live (TTL).

For more information about how to monitor CloudWatch metrics, see Monitoring Amazon Keyspaces with Amazon CloudWatch.