How to Enable Time-to-Live (TTL) for Automatic Data Deletion in Amazon DynamoDB Table

Imran Shaikh
0

By leveraging DynamoDB's Time-to-Live (TTL) feature, you can implement a cost-effective strategy for automated data retention. By setting an expiration timestamp for each item, DynamoDB will automatically delete it once the specified time has elapsed.


This process is transparent to the application and does not require additional write capacity units.


Enabling Time to Live (TTL) on a DynamoDB table is easy. You can do this using the AWS Management Console, AWS CLI, or the AWS API. The AWS Console offers the most user-friendly experience.

Thumbnail Image
toc

Why Time to Live (TTL) is Essential for DynamoDB Performance


While you can manually delete items from your DynamoDB table using various methods, configuring TTL offers significant advantages in terms of performance and cost-effectiveness.


Manual deletion operations, regardless of the method used, consume write throughput capacity. This can be particularly expensive when dealing with large numbers of items.


TTL, however, enables automatic item deletion without consuming any write throughput, leading to substantial savings.

Please note that DynamoDB can take up to 48 hours or more to fully process and delete items marked for expiration.(alert-warning)

Enable Time to Live (TTL) using the AWS CLI


The most straightforward approach to enable Time to Live (TTL) on a DynamoDB table is through the AWS CLI. I'll apply this to my session_data table, utilizing the expires_at attribute to automatically expire outdated records.


 aws dynamodb update-time-to-live \ 
 --table-name session_data \ 
 --time-to-live-specification "Enabled=true, AttributeName=expires_at"

The following output will be displayed after executing the AWS CLI command above.


 {
    "TimeToLiveSpecification": {
        "Enabled": true,
        "AttributeName": "expires_at"
    }
  }

If you missed the previous output, you can alternatively describe the TTL configuration for a DynamoDB table to retrieve the information.


 aws dynamodb describe-time-to-live --table-name session_data

Enable Time to Live (TTL) using the AWS console


To leverage DynamoDB's automatic item deletion feature, you need to add a timestamp attribute of type Number to your table.


The timestamp value should be in Unix epoch time format, which represents the number of seconds elapsed since January 1, 1970 UTC. DynamoDB exclusively supports this format for automatic TTL.


I'll leverage an existing DynamoDB table named session_data for this exercise. The expire_at attribute within this table will be used to set up Time to Live (TTL) and automate item deletion.

session data table in dynamodb table

STEP 1: From the list of available DynamoDB tables, select the table you want to configure for Time to Live (TTL). In my example, I've chosen the session_data table.

session data table selected

STEP 2: Select "Update Setting" from the "Actions" dropdown menu, as shown in the screenshot.

Select Update Setting from Actions drop down button

STEP 3: Once the DynamoDB table configuration page opens, you'll see the "Overview" section selected by default. Click the "Additional settings" button located in the top-right corner (highlighted in red).

Session data table' overview sections

STEP 4: Locate the "Time to Live (TTL)" section by scrolling down the page. By default, TTL is disabled. To activate it, click the "Turn On" button.

Tile to Live(TLE) section in additional setting section of session data dynamodb table

STEP 5: In the TTL settings, specify the Number type attribute (e.g., expire_at) that will be used to determine item expiration based on Unix epoch timestamps.

Time to Live(TTL) Settings

STEP 6: Optionally, To test the TTL configuration before activating it, click the "Run Preview" button within the "Preview" section. This will simulate item deletion based on the specified TTL settings. Once you're satisfied with the preview, click "Turn On TTL" to enable the configuration.

Preview TTL configurations

Once you've activated TTL, the table configuration page will reflect the updated status: "TTL: On".

Tile to Live(TTL) turned on

Conclusion


Time to Live (TTL) is a powerful feature in DynamoDB that can optimize performance and minimize costs. By automatically deleting expired items, TTL reduces storage overhead and improves query performance.


This tutorial has guided you through the process of enabling TTL on your DynamoDB table using the AWS Management Console.


Keep learning and keep growing.

Tags

Post a Comment

0 Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top