A developer is refactoring a web application that runs on an Auto Scaling group of Amazon EC2 instances. The application currently stores user session state in the memory of individual instances, which causes users to lose their sessions when the Auto Scaling group scales in. The session data contains nested JSON objects representing user preferences and search history. The developer wants to store these sessions in a shared, highly available cache that supports automatic expiration of idle sessions after 30 minutes. Which solution meets these requirements with the best performance and lowest operational overhead?
- AConfigure AWS Systems Manager Parameter Store to store each user's session state as a parameter, and retrieve the parameters using the AWS SDK on each request.
- Configure an Amazon ElastiCache for Redis cluster to store the session state, and set a Time to Live (TTL) of 1800 seconds on the session keys.Answer
- CStore the session state in an Amazon DynamoDB table, and configure a scheduled AWS Lambda function that performs a Scan operation on the table every 30 minutes to identify and delete expired sessions.
- DStore the session state in a single Amazon DynamoDB table, and continuously scale up the table's provisioned read and write capacity units to handle the throughput spikes instead of enabling table TTL.
Answer
Configure an Amazon ElastiCache for Redis cluster to store the session state, and set a Time to Live (TTL) of 1800 seconds on the session keys.
Storing session state in an Amazon ElastiCache for Redis cluster is an ideal solution. Redis is an in-memory data store that offers sub-millisecond latency, supports complex data structures (like nested JSON, lists, and sets), and provides native key expiration (TTL) which can be set to 1800 seconds (30 minutes) to automatically expire idle sessions.
Step-by-Step Solution
Key Concept
The core concept is offloading session state management from application servers (EC2 instances) to a shared, high-performance in-memory cache like Amazon ElastiCache for Redis, which natively supports complex data types and key expiration (TTL) to handle session lifetimes.