📖 Scenario: You are building a web application that needs to manage user sessions, store logs, and cache data efficiently. To keep your database clean and performant, you want to automatically remove old or expired data using DynamoDB's TTL (Time To Live) feature.
🎯 Goal: Learn how to set up DynamoDB tables with TTL attributes for sessions, logs, and cache data to automatically expire and delete items after a certain time.
📋 What You'll Learn
Create a DynamoDB table named Sessions with a TTL attribute called expires_at.
Create a DynamoDB table named Logs with a TTL attribute called log_expiry.
Create a DynamoDB table named Cache with a TTL attribute called cache_expiry.
Insert sample items into each table with appropriate TTL timestamps.
Enable TTL on each table using the correct TTL attribute.
💡 Why This Matters
🌍 Real World
TTL helps automatically remove expired sessions, logs, and cache data, keeping your database clean and reducing storage costs.
💼 Career
Understanding TTL in DynamoDB is essential for backend developers and cloud engineers managing scalable, cost-effective applications.
Progress0 / 4 steps
1
Create DynamoDB tables with TTL attributes
Create three DynamoDB tables named Sessions, Logs, and Cache. Each table should have a primary key called id (string). Add a TTL attribute to each table: expires_at for Sessions, log_expiry for Logs, and cache_expiry for Cache. Use the AWS CLI commands exactly as shown below.
DynamoDB
Hint
Use the AWS CLI create-table command with --attribute-definitions and --key-schema to create tables.
2
Insert sample items with TTL timestamps
Insert one sample item into each table with the exact id values: session1 for Sessions, log1 for Logs, and cache1 for Cache. Add the TTL attribute with a Unix epoch timestamp in the future: expires_at = 1893456000 for Sessions, log_expiry = 1893456000 for Logs, and cache_expiry = 1893456000 for Cache. Use the AWS CLI put-item command exactly as shown.
DynamoDB
Hint
Use the AWS CLI put-item command with JSON syntax for the item attributes.
3
Enable TTL on each DynamoDB table
Enable TTL on the Sessions table using the attribute expires_at. Enable TTL on the Logs table using the attribute log_expiry. Enable TTL on the Cache table using the attribute cache_expiry. Use the AWS CLI update-time-to-live command exactly as shown below.
DynamoDB
Hint
Use the AWS CLI update-time-to-live command with --time-to-live-specification to enable TTL.
4
Verify TTL settings and data expiry
Write AWS CLI commands to describe the TTL status for each table: Sessions, Logs, and Cache. Use the describe-time-to-live command exactly as shown below to confirm TTL is enabled with the correct attribute names.
DynamoDB
Hint
Use the AWS CLI describe-time-to-live command to check TTL status.
Practice
(1/5)
1. What is the main purpose of TTL (Time To Live) in DynamoDB?
easy
A. Automatically delete items after a specified time to save storage
B. Encrypt data for security
C. Backup data to another region
D. Increase read capacity automatically
Solution
Step 1: Understand TTL functionality
TTL marks items with an expiration timestamp to delete them automatically after that time.
Step 2: Identify the main benefit
This automatic deletion helps save storage and keeps data fresh without manual cleanup.
Final Answer:
Automatically delete items after a specified time to save storage -> Option A
Quick Check:
TTL = automatic deletion [OK]
Hint: TTL deletes expired data automatically to save space [OK]
Common Mistakes:
Confusing TTL with encryption
Thinking TTL backs up data
Assuming TTL changes capacity settings
2. Which attribute type must the TTL attribute have in a DynamoDB table?
easy
A. String representing a date
B. Boolean indicating expiration
C. Binary data
D. Number representing epoch time in seconds
Solution
Step 1: Check TTL attribute requirements
DynamoDB TTL requires a Number attribute that stores the expiration time as epoch time in seconds.
Step 2: Eliminate other types
String, Boolean, or Binary types are not valid for TTL expiration timestamps.
Final Answer:
Number representing epoch time in seconds -> Option D
Quick Check:
TTL attribute = epoch seconds number [OK]
Hint: TTL attribute must be a number with epoch seconds [OK]
Common Mistakes:
Using string dates instead of numbers
Setting TTL attribute as Boolean
Using binary data for TTL
3. Given a DynamoDB table with TTL enabled on attribute expireAt, what happens to an item with expireAt set to a timestamp 10 minutes in the past?
medium
A. The item is automatically deleted by DynamoDB
B. The item remains until manually deleted
C. The item is archived automatically
D. The item is updated with a new timestamp
Solution
Step 1: Understand TTL expiration behavior
Items with TTL timestamps in the past are marked for deletion by DynamoDB automatically.
Step 2: Confirm deletion timing
DynamoDB deletes expired items typically within 48 hours after expiration, often sooner.
Final Answer:
The item is automatically deleted by DynamoDB -> Option A
Quick Check:
Expired TTL item = auto delete [OK]
Hint: Expired TTL items get deleted automatically [OK]
Common Mistakes:
Thinking expired items stay forever
Assuming items get archived
Believing TTL updates timestamps automatically
4. You enabled TTL on attribute sessionExpiry but items are not being deleted after expiration. What is the most likely cause?
medium
A. TTL only works on string attributes
B. The table has no primary key defined
C. The sessionExpiry attribute is missing or not a number
D. DynamoDB does not support TTL
Solution
Step 1: Check TTL attribute correctness
TTL requires the attribute to exist and be a number representing epoch time; missing or wrong type prevents deletion.
Step 2: Verify other options
Tables must have primary keys, but that doesn't affect TTL. TTL does not work on strings. DynamoDB supports TTL.
Final Answer:
The sessionExpiry attribute is missing or not a number -> Option C
Quick Check:
TTL attribute missing or wrong type = no deletion [OK]
Hint: TTL attribute must exist and be a number [OK]
Common Mistakes:
Assuming TTL works on strings
Ignoring missing TTL attribute
Thinking DynamoDB lacks TTL support
5. You want to use TTL to automatically expire cached items after 1 hour. Which approach correctly sets the TTL attribute cacheExpire when inserting an item?
hard
A. Set cacheExpire to current date string plus 1 hour
B. Set cacheExpire to current epoch time plus 3600 seconds
C. Set cacheExpire to Boolean true
D. Do not set cacheExpire attribute
Solution
Step 1: Calculate TTL expiration timestamp
TTL requires a number representing epoch time in seconds, so add 3600 seconds (1 hour) to current epoch time.
Step 2: Confirm correct attribute setting
Setting a string or Boolean won't work; omitting the attribute disables TTL for that item.
Final Answer:
Set cacheExpire to current epoch time plus 3600 seconds -> Option B
Quick Check:
TTL = current time + seconds [OK]
Hint: TTL value = current epoch seconds + expiration seconds [OK]
Common Mistakes:
Using date strings instead of epoch seconds
Setting TTL as Boolean
Not setting TTL attribute at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.