dynobase-icon
Dynobase

DynamoDB TTL (Time to Live) - How to Use It Effectively

Rafal Wilinski

Written by Rafal Wilinski

Published on April 16th, 2021

    Still using AWS console to work with DynamoDB? 🙈

    Time to 10x your DynamoDB productivity with Dynobase [learn more]

    What is DynamoDB TTL?

    DynamoDB allows you to specify a time-to-live (TTL) timestamp attribute in an epoch timestamp format to define when the item is no longer needed. This attribute will tell DynamoDB to remove items whose TTL attribute is earlier than the current time.

    The time-to-live mechanism is extremely useful for removing temporary items such as session data. We don't want to store invalidated sessions in the store because they not only don't matter to us, but they are also consuming storage which incurs costs (DynamoDB is also priced by the amount of data we're storing).

    What's also advantageous about TTL is that it is completely free. Moreover, the action of removing items is also free - we're not paying for consumed WCUs that we would be paying for if we were to delete these items on our own. These two features make TTL a great money saver.

    Setting up TTL

    Using CLI

    In CLI, there's a special command called update-time-to-live. Use it in conjunction with the --time-to-live-specification flag.

    Using CDK (Cloud Development Kit)

    In CDK, you can specify TTL on creation. All you need to do is set timeToLiveAttribute to the attribute of your choice.

    Using AWS Console

    In AWS Console, click "Manage TTL" in the Table Details section.

    DynamoDB TTL setting in the console

    Caveats

    • The TTL attribute must use the Number (N) data type.
    • The TTL attribute must use epoch time format in seconds, not milliseconds. For example: 1563369999.
    • The TTL attribute value must be a timestamp with an expiration of no more than five years in the past.
    • The TTL mechanism will work on items that have been inserted after the TTL has been enabled on the table.
    • TTL setting cannot be reconfigured. You need to first disable it and then re-enable it on another attribute.
    • DynamoDB removes items with expired TTL within up to 48 hours from the original expiration time. This means you shouldn't rely on it 100%. To ensure that your scans or queries are not returning expired data, make sure to include the following FilterExpression in your queries:

    Use Cases for DynamoDB TTL

    DynamoDB TTL is particularly useful in scenarios where data is only relevant for a certain period. For example, in e-commerce applications, shopping cart items can be set to expire after a few hours of inactivity. Similarly, in social media platforms, temporary posts or stories can be automatically deleted after a set period. Another use case is in IoT applications where sensor data might only be relevant for a short duration and can be purged to save storage costs. By leveraging TTL, you can ensure that your database remains lean and cost-effective.

    Dynobase is a Professional GUI Client for DynamoDB

    Start your 7-day free trial today

    Product Features

    Download
    /
    Changelog
    /
    Pricing
    /
    Member Portal
    /
    Privacy
    /
    EULA
    /
    Twitter
    © 2024 Dynobase