Automatic expiration helps keep your database clean by removing old data you no longer need. It saves space and keeps your data fresh without extra work.
When you want to delete user session data after it expires.
When you store temporary logs that are only useful for a short time.
When you keep cache data that should be removed after a set period.
When you want to automatically remove outdated records like promotions or offers.
When you want to manage data lifecycle without manual cleanup.
Syntax
DynamoDB
1. Add a timestamp attribute to your items (e.g., 'ttl').
2. Enable Time To Live (TTL) on the table using that attribute.
3. DynamoDB automatically deletes items after the timestamp passes.
The timestamp must be in Unix epoch time (seconds since 1970-01-01).
TTL deletion happens automatically but can take up to 48 hours to remove expired items.
Examples
This item will expire and be deleted after the Unix time 1686000000.
This tells DynamoDB to use the 'ttl' attribute to know when to delete items.
DynamoDB
Enable TTL on attribute 'ttl'in DynamoDB table settings.
Sample Program
This example adds a user session with a TTL timestamp. DynamoDB will remove it after the time passes.
DynamoDB
{
"UserId": "user1",
"SessionData": "data",
"ttl": 1893456000
}
// Put this item into DynamoDB table 'Users' using PutItem
// TTL set to a future timestamp
// After that time, DynamoDB will delete this item automatically
OutputSuccess
Important Notes
TTL helps reduce manual cleanup and storage costs.
Expired items are deleted asynchronously, so they might still appear briefly after expiration.
Make sure your TTL attribute is a number representing seconds since 1970-01-01 UTC.
Summary
Automatic expiration removes old data without manual work.
Use TTL attribute with Unix epoch time to set expiration.
DynamoDB deletes expired items automatically, helping manage data lifecycle.
Practice
(1/5)
1. What is the main benefit of using automatic expiration (TTL) in DynamoDB to manage data lifecycle?
easy
A. It requires manual deletion of expired items.
B. It increases the storage size of the table.
C. It automatically deletes expired data without manual intervention.
D. It slows down query performance.
Solution
Step 1: Understand TTL purpose in DynamoDB
TTL (Time To Live) is designed to automatically remove items after a set expiration time.
Step 2: Identify the benefit of automatic expiration
This automatic removal means no manual cleanup is needed, reducing maintenance effort.
Final Answer:
It automatically deletes expired data without manual intervention. -> Option C
Quick Check:
Automatic deletion = It automatically deletes expired data without manual intervention. [OK]
Hint: TTL auto-removes expired data, no manual cleanup needed [OK]
Common Mistakes:
Thinking TTL increases storage size
Assuming manual deletion is still required
Believing TTL slows down queries
2. Which of the following is the correct way to define a TTL attribute in a DynamoDB item?
easy
A. { "ttl": 1717262400 }
B. { "ttl": "2024-06-01T12:00:00Z" }
C. { "ttl": true }
D. { "ttl": "expired" }
Solution
Step 1: Recall TTL attribute format
TTL requires a Unix epoch time (number of seconds since Jan 1, 1970) as a number.
Step 2: Identify correct TTL value type
{ "ttl": 1717262400 } uses a numeric Unix epoch timestamp, which is correct for TTL.
Final Answer:
{ "ttl": 1717262400 } -> Option A
Quick Check:
TTL = Unix epoch number [OK]
Hint: TTL must be a Unix epoch number, not string or boolean [OK]
Common Mistakes:
Using ISO date string instead of Unix epoch
Setting TTL as boolean true
Using non-numeric strings for TTL
3. Given the following DynamoDB item with TTL set to 1717262400 (Unix epoch time), what happens after that time passes?
When the current time passes the TTL timestamp, DynamoDB marks the item for deletion.
Step 2: Identify DynamoDB behavior on expired items
DynamoDB automatically deletes expired items without user action.
Final Answer:
The item is automatically deleted by DynamoDB. -> Option A
Quick Check:
TTL expiry triggers auto-deletion [OK]
Hint: Expired TTL items get auto-deleted by DynamoDB [OK]
Common Mistakes:
Thinking items stay forever after TTL
Assuming items are archived instead of deleted
Believing TTL expiration causes errors
4. You set a TTL attribute on your DynamoDB table items, but expired items are not being deleted. What is the most likely cause?
medium
A. The TTL attribute is set as a string instead of a number.
B. The TTL feature is not enabled on the table.
C. The table has no primary key defined.
D. The items have no attribute named 'ttl'.
Solution
Step 1: Check TTL feature activation
TTL must be enabled on the DynamoDB table to work; just setting attribute is not enough.
Step 2: Identify common misconfiguration
If TTL is not enabled, expired items will not be deleted automatically.
Final Answer:
The TTL feature is not enabled on the table. -> Option B
Quick Check:
TTL must be enabled on table [OK]
Hint: Enable TTL on table, not just set attribute [OK]
Common Mistakes:
Assuming TTL works without enabling it
Confusing attribute type with feature activation
Ignoring missing TTL attribute on items
5. You want to keep user session data in DynamoDB but automatically remove sessions older than 24 hours. Which approach best uses automatic expiration to manage this lifecycle?
hard
A. Manually scan and delete sessions older than 24 hours every hour.
B. Disable TTL and rely on application logic to remove old sessions.
C. Store session expiration as a string date and delete manually.
D. Add a TTL attribute with the session creation time plus 86400 seconds (24 hours).
Solution
Step 1: Calculate TTL for session expiration
TTL should be set as Unix epoch time when the session expires, i.e., creation time + 86400 seconds.
Step 2: Use DynamoDB TTL to automate deletion
Setting TTL this way lets DynamoDB auto-delete sessions after 24 hours without manual work.
Final Answer:
Add a TTL attribute with the session creation time plus 86400 seconds (24 hours). -> Option D