ElastiCache (Valkey) as an In-Memory Cache
Topics: Databases, Caching, ElastiCache, Valkey, AWS
Overview
This lab demonstrates Amazon ElastiCache using Valkey as an in-memory caching layer to improve application performance. You'll deploy a Valkey cluster, connect from an EC2 instance, and perform basic cache operations like storing, retrieving, and expiring data.
Key Concepts
| Concept | Description |
|---|---|
| ElastiCache | Managed in-memory caching service compatible with Valkey/Redis/Memcached |
| Valkey | High-performance, Redis-compatible data structure store used as cache |
| Redis OSS | Open-source alternative, but Valkey is recommended for new deployments |
| TTL (Time To Live) | Automatic expiration of cached data |
| Cache Hit/Miss | Data found in cache (hit) or fetched from DB (miss) |
| Subnet Groups | Define subnets for ElastiCache clusters in VPC (not needed for serverless) |
| Serverless | Auto-scaling, pay-per-use deployment option |
| Encryption | Data protection at rest and in transit |
| Authentication | Secure access using passwords/tokens |
Prerequisites
- Active AWS account with billing enabled
- IAM permissions for ElastiCache and EC2
- Basic knowledge of Redis and caching concepts
Architecture Overview
Click to expand ElastiCache Data Flow
Phase 1: Launch EC2 Client using Amazon Linux 2023
Purpose: To create an EC2 instance that will act as a client machine to connect to ElastiCache using valkey-cli.
Select Region. Ensure EC2 and ElastiCache stay in the same region.
Launch Instance
- Name:
RedisClient-AL2023 - AMI: Amazon Linux 2023 AMI
- Instance Type:
t3.micro(Free Tier) - Key Pair: Select an existing
.pemfile
- Name:
Network & Security Group
- VPC: Default VPC
- Security Group Name:
SG-RedisClient - Inbound Rules: | Type: SSH | Port: 22 | Source : 0.0.0.0/0 |
- Outbound rules: Allow all (default)
IMPORTANT
Add Redis 6379 and 6380 also for durabilty. The client does not listen on 6379.
- Connect & Install Client Connect via EC2 Instance Connect and run:
sudo dnf update -y
sudo dnf install -y valkey
# Verify installation
valkey-cli --versionNOTE
Amazon Linux 2023 uses Valkey, which is fully Redis-protocol compatible.
Verification: Confirm EC2 status is "Running" and valkey-cli version displays.
Phase 2: Create Amazon ElastiCache (Valkey) Cache
Purpose: To create an Amazon ElastiCache Valkey serverless cache that will act as an in-memory cache, accessible from the EC2 client created in Phase-1.
Confirm AWS Region
- Ensure you are in the same region used in Phase-1
- EC2 and ElastiCache must be in the SAME region and VPC
Open ElastiCache Console
- AWS Console → Services
- Select ElastiCache
- Click Create cache
Select Cache Engine
- Engine: Valkey
Choose Deployment Settings
- Deployment option: Serverless
TIP
Serverless deployment provides automatic scaling and simplifies management for development and testing.
Provide Cluster Information
- Cache name:
lab-valkey
- Cache name:
Configure Network
- VPC: Default VPC
- Security Group: Security group with port 6379, 6380 access from EC2
Note the Valkey Endpoint
- Click on the cache name
lab-valkey - Copy the Serverless endpoint (for serverless deployment)
- This endpoint will be used in Phase-3 to connect from EC2
- Click on the cache name
In the created cache instance go to "Connected compute resources" and add the EC2 Instance. This automatically connects the EC2 security groups and allows connection.
NOTE
Can Use cloudshell to run valkey commands if EC2 is not preferred. Connect to Compute allows connecting to EC2 created in Phase-1 automatically.
Verification: Confirm cache status is "Available" and endpoint is noted.
Phase 3: Connect EC2 to ElastiCache Valkey and Execute Cache Commands
Purpose: To connect the EC2 client (Amazon Linux 2023) to the ElastiCache Valkey serverless cache using valkey-cli and demonstrate basic in-memory cache operations.
Pre-checks
Before connecting, confirm:
- EC2 and ElastiCache are in the same AWS region
- EC2 security group =
SG-RedisClient - Valkey security group =
SG-ValkeyCache - Valkey security group allows port 6379 from
SG-RedisClient - Valkey Status = Available
- You have copied the Serverless endpoint
- You have the authentication password set in Phase 2
Connect to EC2
- AWS Console → EC2
- Select instance
RedisClient-AL2023 - Click Connect
- Choose EC2 Instance Connect and click Connect
- You are now inside the EC2 terminal
Connect to ElastiCache Valkey
Use the Serverless Endpoint from Phase-2 and the password:
If the endpoint has .....cache.amazonaws.com:6379 port at the end, change it to -p 6379.
valkey-cli --tls -h <Serverless_ENDPOINT> -p 6379You should see a lab-valkey-xxxxxx.serverless.use1.cache.amazonaws.com:6379>. This means the connection is successful.
- Execute Valkey Commands
These commands simulate what an application does internally.
4.1 Test Connectivity
PINGExpected Output: PONG
4.2 Store and Retrieve Data (SET / GET)
SET course "Cloud Computing"
GET courseExpected Output: "Cloud Computing"
NOTE
Demonstrates key–value storage in memory.
4.3 Counter Example (INCR)
INCR visits
INCR visits
GET visitsExpected Output: "2"
TIP
Common real-world use - page views, hit counters.
4.4 Set Data with Expiry (TTL)
SET notice "Results Published" EX 60
TTL notice
GET noticeExpected Output:
- TTL shows a value ≤ 60
- GET returns "Results Published"
4.5 Verify Automatic Expiry
Wait ~60 seconds, then run:
GET noticeExpected Output: (nil)
4.6 Delete Data Manually
DEL course
GET courseExpected Output: (nil)
- Exit Valkey Client
EXITTIP
The application first checks Valkey. If data is present, it is returned immediately from memory. If not present, the application fetches data from the database and stores it in Valkey with a TTL. Valkey automatically removes the data after expiry.
Verification: Confirm all commands executed as expected (e.g., GET returns values, TTL expires).
- EC2 successfully connected to ElastiCache Valkey
- In-memory key–value operations verified
- Temporary storage and TTL behavior observed
- Valkey used as a cache, not as a primary database
Validation
Validation
- EC2 Setup: Confirm instance is running, valkey-cli installed, and connected to Valkey.
- ElastiCache Cache: Verify status is "Available" and endpoint is accessible.
- Cache Operations: Test SET/GET, INCR, TTL, and DEL commands.
- Expiry: Confirm data is removed after TTL expires.
- Security: Ensure only authorized EC2 can connect via security groups, with authentication and encryption enabled.
Cleanup
Cleanup
Step 1: Delete Valkey Cache
ElastiCache → Select lab-valkey → Delete (Disable snapshots)
Step 2: Terminate EC2 Instance
EC2 → Instances → Terminate RedisClient-AL2023
Step 3: Optional
Delete unused security groups
Valkey Commands Explanation
SET course "Cloud Computing": Stores frequently accessed data in cacheGET course: Retrieves data from memoryEXPIRE course 30: Sets a 30-second timer for the dataTTL course: Checks remaining time before the data is removed
NOTE
The above commands simulate application caching behavior. Cached data is temporary and stored in memory. After expiry, the application would fetch fresh data from the database again.
Result
Successfully deployed an ElastiCache Valkey serverless cache with encryption and authentication enabled, and performed in-memory caching operations from an EC2 instance. Demonstrated key concepts like TTL, cache hits/misses, and integration with databases for improved application performance, following AWS security best practices.
Viva Questions
- What is the primary purpose of ElastiCache in AWS?
- Explain the difference between a cache hit and a cache miss.
- How does TTL work in Valkey, and why is it important?
- Why would you use ElastiCache instead of storing data directly in RDS?
- What security measures are implemented when connecting EC2 to ElastiCache?
- What are the benefits of using ElastiCache Serverless deployment?
- Why is encryption important for cached data?
Quick Start Guide
Quick Start Guide
- Launch EC2 instance with Amazon Linux 2023 and install valkey-cli.
- Allow Port 6379 and 6380 in security group.
sudo dnf install -y valkey
valkey-cli --version- Create ElastiCache Valkey serverless cache.
- Connect to EC2 and choose the Created instance.
- Connect EC2 to ElastiCache using valkey-cli with endpoint url.
- Execute Valkey commands: PING, SET/GET, INCR, TTL, DEL.
PING
SET course "Cloud Computing"
GET course
INCR visits
TTL notice
SET notice "Results Published" EX 60
GET notice
DEL course
GET course- Verify data storage, retrieval, expiry, and deletion.
