A team is building a serverless document collaboration portal where users edit documents concurrently. The portal needs to manage transient user presence indicators (e.g., 'User X is online') that expire automatically after 5 minutes of user inactivity. The developer wants to minimize storage costs and avoid running unnecessary compute resources to clean up expired session records. Which approach should the developer take to meet these requirements?
- AStore the presence states in AWS Systems Manager Parameter Store and query the parameters using the AWS SDK.
- BStore the presence states in Amazon DynamoDB and configure a cron-triggered AWS Lambda function to run a Scan operation to find and delete expired records.
- Store the presence states in an Amazon DynamoDB table and enable Time to Live (TTL) on a timestamp attribute representing the expiration time.Answer
- DStore the presence states in the local memory of the backend AWS Lambda functions and increase the Lambda timeout duration to preserve the execution context.
Answer
Store the presence states in an Amazon DynamoDB table and enable Time to Live (TTL) on a timestamp attribute representing the expiration time.
The correct approach is to store the presence states in an Amazon DynamoDB table and enable Time to Live (TTL). DynamoDB TTL allows developers to define a timestamp attribute indicating when an item should expire. DynamoDB automatically deletes these items in the background, typically within 48 hours of expiration, without consuming provisioned read or write throughput. This completely eliminates the need for custom clean-up code, minimizes storage costs, and scale-out overhead.
Step-by-Step Solution
Key Concept
Session state expiration using Amazon DynamoDB Time to Live (TTL)