What is DynamoDB Time to Live (TTL) and when do you use it?
Learn how DynamoDB Time to Live (TTL) auto-deletes expired items for free, when to use it, and the epoch-seconds gotcha that trips up engineers.
Expected Interview Answer
DynamoDB Time to Live (TTL) is a free feature that automatically deletes items after a timestamp you store in a designated attribute, letting the table expire stale data without you running delete operations.
You enable TTL on one numeric attribute holding a Unix epoch timestamp (in seconds). A background process periodically scans partitions and removes items whose TTL time has passed, typically within 48 hours of expiry. Deletions consume no write capacity and are not billed, and expired items can be captured in DynamoDB Streams so downstream systems can react.
- Automatically purges stale data with no code
- Consumes no write capacity and incurs no cost
- Reduces storage bills by shrinking table size
- Keeps hot data small for better performance
- Expired items can flow through DynamoDB Streams
AI Mentor Explanation
Think of a cricket ground scoreboard that shows the current over and recent balls, but a groundsman quietly wipes deliveries older than a session so the board never gets cluttered. Nobody manually erases each ball; a routine sweep clears whatever has aged out. TTL works the same way: every item carries an expiry stamp, and a background groundsman removes it once its time has passed, keeping the live board lean without any scorer lifting a finger.
Step-by-Step Explanation
Step 1
Choose a TTL attribute
Pick or add a top-level numeric attribute that will hold a Unix epoch time in seconds, for example expiresAt.
Step 2
Store the expiry per item
When writing an item, set the attribute to the future epoch second at which it should expire (current time plus your retention window).
Step 3
Enable TTL on the table
Turn on TTL for the table and point it at your attribute via the console, CLI update-time-to-live, or IaC.
Step 4
Let the background process delete
DynamoDB scans and deletes expired items automatically, usually within 48 hours of expiry — it is not instantaneous.
Step 5
Filter and react
Filter out items whose TTL has passed but are not yet deleted in your queries, and optionally consume expirations from DynamoDB Streams.
What Interviewer Expects
- TTL attribute must be a Number holding Unix epoch seconds
- Deletion is asynchronous, not immediate (up to ~48h)
- Deletes are free and consume no write capacity
- Expired-but-not-deleted items may still appear in reads
- TTL deletes can appear in DynamoDB Streams
Common Mistakes
- Storing the TTL value in milliseconds or as an ISO string instead of epoch seconds
- Assuming items disappear exactly at the TTL time
- Relying on TTL for security or strict compliance deletion timing
- Forgetting to filter still-present expired items in queries
- Putting the TTL attribute inside a nested map instead of top level
Best Answer (HR Friendly)
“DynamoDB TTL is a setting that lets the database automatically delete records once they reach an expiry time you save on them. It is great for cleaning up temporary data like sessions or logs without extra code, and it costs nothing to run.”
Code Example
import time, boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Sessions')
# expiry = now + 30 days, in Unix epoch SECONDS
expires_at = int(time.time()) + 30 * 24 * 60 * 60
table.put_item(Item={
'sessionId': 'abc-123',
'userId': 'u-42',
'expiresAt': expires_at, # Number, epoch seconds
})
# Enable TTL once on the table (usually done via IaC)
client = boto3.client('dynamodb')
client.update_time_to_live(
TableName='Sessions',
TimeToLiveSpecification={'Enabled': True, 'AttributeName': 'expiresAt'},
)Follow-up Questions
- Why must the TTL attribute be in epoch seconds rather than milliseconds?
- How can you detect and process items that TTL deletes?
- Does TTL deletion consume write capacity units or cost money?
- How would you hide expired items that have not yet been physically deleted?
- What are good use cases for TTL versus a scheduled cleanup job?
MCQ Practice
1. What format must the DynamoDB TTL attribute value use?
DynamoDB TTL expects a Number attribute holding a Unix epoch timestamp expressed in seconds; milliseconds or strings will not expire.
2. How quickly are items removed after their TTL time passes?
TTL deletion is a background process and typically occurs within 48 hours of expiry, so items are not removed exactly on time.
3. What is the capacity cost of TTL deletions?
TTL deletes do not consume provisioned write capacity and are not billed, which is a key advantage over manual deletes.
Flash Cards
What attribute type does TTL require? — A top-level Number attribute holding a Unix epoch timestamp in seconds.
How timely is TTL deletion? — Asynchronous — usually within 48 hours of the expiry time, not exactly at it.
Does TTL cost write capacity? — No. TTL deletions are free and consume no provisioned write capacity.
Can you react to TTL deletes? — Yes, expired items can be captured in DynamoDB Streams for downstream processing.