0
0
DynamodbConceptBeginner · 3 min read

What is TTL in DynamoDB: Explanation and Usage

TTL in DynamoDB stands for Time to Live, a feature that automatically deletes expired items from a table based on a timestamp attribute. It helps manage storage by removing data that is no longer needed without manual intervention.
⚙️

How It Works

TTL in DynamoDB works like a self-cleaning timer for your data. You add a special attribute to each item that holds a timestamp indicating when the item should expire. Once the current time passes this timestamp, DynamoDB automatically deletes the item.

Think of it like setting an expiration date on food in your fridge. When the date passes, the food is thrown away to keep your fridge clean and fresh. Similarly, TTL keeps your database tidy by removing old data you no longer need.

This process runs in the background and does not affect your application's performance. It helps save storage costs and keeps your tables efficient by removing stale data automatically.

💻

Example

This example shows how to enable TTL on a DynamoDB table and set an expiration timestamp for an item.

python
import boto3
from time import time

# Create DynamoDB client
client = boto3.client('dynamodb')

table_name = 'MyTable'
ttl_attribute = 'ExpireAt'

# Enable TTL on the table
client.update_time_to_live(
    TableName=table_name,
    TimeToLiveSpecification={
        'Enabled': True,
        'AttributeName': ttl_attribute
    }
)

# Add an item with TTL set to 1 hour from now
expiration_time = int(time()) + 3600  # current time + 3600 seconds

client.put_item(
    TableName=table_name,
    Item={
        'Id': {'S': '123'},
        'Data': {'S': 'Sample data'},
        ttl_attribute: {'N': str(expiration_time)}
    }
)

print(f"Item added with TTL set to expire at {expiration_time} (unix timestamp)")
Output
Item added with TTL set to expire at 1700000000 (unix timestamp)
🎯

When to Use

Use TTL in DynamoDB when you want to automatically remove data that is only relevant for a limited time. This is useful for:

  • Session data that expires after user logout or timeout.
  • Temporary logs or analytics data that become irrelevant after a period.
  • Cache entries that should be refreshed regularly.
  • Any data with a natural expiration like coupons, tokens, or event registrations.

TTL helps reduce manual cleanup tasks and controls storage costs by deleting expired items automatically.

Key Points

  • TTL uses a timestamp attribute to mark when items expire.
  • Expired items are deleted automatically without affecting app performance.
  • TTL helps manage storage and reduce costs by removing stale data.
  • It is ideal for temporary or time-sensitive data.

Key Takeaways

TTL in DynamoDB automatically deletes items after a specified expiration time.
You enable TTL by specifying a timestamp attribute on your table.
TTL is perfect for managing temporary or expiring data like sessions or logs.
Using TTL reduces manual cleanup and helps control storage costs.
Expired items are removed in the background without impacting your app.