Question #729
A social media app stores user session data in an Amazon DynamoDB table. Millions of sessions are created daily, and each session is a single item. The app requires only active sessions, and any session older than 24 hours must be removed. What is the MOST cost-effective way to delete expired sessions?
For each item, add a String attribute with the session creation timestamp. Create a script to scan the table for expired sessions and delete them using BatchWriteItem. Schedule the script hourly via a cron job on an Amazon EC2 instance.
For each item, add a String attribute with the session creation timestamp. Create a script to scan the table and delete expired sessions using BatchWriteItem. Deploy the script in a container and schedule it every 10 minutes using an Amazon ECS task on AWS Fargate.
For each item, add a Date attribute set to 24 hours after the session creation time. Create a GSI using this attribute as the sort key. Use a Lambda function referencing the GSI to delete expired sessions via BatchWriteItem. Trigger the function every minute with a CloudWatch event.
For each item, add a Number attribute set to 24 hours after the session creation time. Configure the DynamoDB table's TTL to reference this attribute.
Explanation
Option D is correct because DynamoDB's TTL feature automatically deletes items when their specified timestamp attribute expires. This requires no additional compute resources (e.g., EC2, ECS, Lambda) or recurring costs for script execution. TTL is serverless, scales seamlessly, and minimizes operational overhead.
Other options are incorrect:
- A & B: Scanning the entire table hourly or every 10 minutes is inefficient and costly due to high read capacity consumption. Running scripts on EC2/ECS adds compute costs.
- C: While using a GSI and Lambda reduces scan overhead, frequent Lambda invocations (every minute) and BatchWriteItem operations are less cost-effective than TTL.
Key Points:
1. DynamoDB TTL is designed for automatic expiration of items.
2. TTL requires a Number-type attribute storing the expiration timestamp (epoch time in seconds).
3. Avoid scans and external compute resources for cost optimization.
Answer
The correct answer is: D