TL;DR
Amazon ElastiCache for Redis is a fully managed, in-memory data store that delivers sub-millisecond latency, high availability, and automatic scaling. It is designed for real-time applications such as caching, session storage, leaderboards, and event-driven architectures.
š The Ultimate Guide to AWS ElastiCache for Redis
Amazon ElastiCache for Redis* is a **fully managed, in-memory data store** that delivers **sub-millisecond latency** and *high availability for real-time applications such as caching, session storage, leaderboards, and event-driven architectures. For multi-region deployments, check out our guide on [AWS ElastiCache for Redis Global Datastore](/posts/the-ultimate-guide-to-aws-elasticache-for-redis-global-datastore).
---
$1
$1
#### š Real-World Example: Caching for an E-Commerce Website
A high-traffic e-commerce platform can use Redis as a caching layer to store frequently accessed product data. Instead of querying a database for every request, Redis serves cached responses, reducing latency and database load.
`` import redis redis_client = redis.Redis(host='my-redis-endpoint', port=6379, db=0, decode_responses=True) redis_client.setex("product:1234", 3600, "{'name': 'Laptop', 'price': 1200}") print(redis_client.get("product:1234"))
python
`Store product data in cache
Retrieve product data from cache
$1
#### š Real-World Example: Session Management for a Web App
A user authentication system can store session tokens in Redis for quick retrieval, ensuring a seamless login experience.
` session_id = "user_5678_session"
redis_client.setex(session_id, 1800, "{'user_id': 5678, 'login_time': '2025-02-03T10:00:00'}")
python
`
$1
#### š Real-World Example: Leaderboard in a Gaming App
A multiplayer game can use Redis Sorted Sets to maintain real-time leaderboards efficiently.
` redis_client.zadd("game_leaderboard", {"player_1": 1500, "player_2": 2000})
print(redis_client.zrevrange("game_leaderboard", 0, 4, withscores=True))
python
`
---
$1
` resource "aws_elasticache_cluster" "redis" {
cluster_id = "my-redis-cluster"
engine = "redis"
node_type = "cache.t3.micro"
num_cache_nodes = 1
parameter_group_name = "default.redis6.x"
subnet_group_name = aws_elasticache_subnet_group.default.name
}
hcl
`
$1
` import redis redis_client = redis.Redis(
host='my-redis-endpoint',
port=6379,
db=0,
decode_responses=True
) redis_client.set("key", "value")
print(redis_client.get("key"))
python
`
---
$1
` flowchart TD
A[Client Application] -->|API Requests| B[ElastiCache for Redis]
B -->|Primary Node| C[Read Replicas]
B -->|Auto Scaling| D[Additional Shards]
C -->|Multi-AZ| E[Failover Replica]
B -->|Metrics| F[CloudWatch]
style A fill:#4C4C4C,stroke:#333,stroke-width:2px
style B fill:#FF9900,stroke:#232F3E,stroke-width:2px
style C fill:#FF9900,stroke:#232F3E,stroke-width:2px
style D fill:#FF9900,stroke:#232F3E,stroke-width:2px
style E fill:#FF9900,stroke:#232F3E,stroke-width:2px
style F fill:#FF9900,stroke:#232F3E,stroke-width:2px
mermaid
`
$1
$1
| Instance Type | vCPU | Memory | Network | Price/Hour | Price/Month* |
|---|---|---|---|---|---|
| cache.t4g.micro | 2 | 0.5 GiB | Up to 5 Gigabit | $0.016 | ~$11.65 |
| cache.t4g.small | 2 | 1.37 GiB | Up to 5 Gigabit | $0.032 | ~$23.30 |
| cache.t4g.medium | 2 | 3.09 GiB | Up to 5 Gigabit | $0.064 | ~$46.60 |
| cache.r6g.large | 2 | 13.07 GiB | Up to 10 Gigabit | $0.156 | ~$113.65 |
| cache.r6g.xlarge | 4 | 26.14 GiB | Up to 10 Gigabit | $0.312 | ~$227.30 |
*Monthly prices are approximate, based on 730 hours per month
$1
| Component | Description | Cost |
|---|---|---|
| Data Transfer OUT | First 1 GB | FREE |
| Data Transfer OUT | Up to 10 TB / Month | $0.09 per GB |
| Backup Storage | Beyond Free Tier | $0.085 per GB-month |
| Snapshot Transfer | To another region | $0.02 per GB |
$1
Note: All prices are for US East (N. Virginia) region as of February 2024. Actual prices may vary by region and are subject to change. Please check the [AWS Pricing Calculator](https://calculator.aws/) for the most current pricing.
$1
#### š Real-World Example: API Rate Limiting
A backend API can use Redis to enforce rate limiting per user to prevent abuse.
` import time def rate_limit(user_id):
key = f"rate_limit:{user_id}"
count = redis_client.incr(key)
if count == 1:
redis_client.expire(key, 60) # Set expiry of 60 seconds
if count > 10:
return False # Block request if limit exceeded
return True user_id = "user_1234"
if rate_limit(user_id):
print("Request allowed")
else:
print("Rate limit exceeded")
python
``Example usage
---
$1
ā No infrastructure management ā Fully managed by AWS.
ā Seamless scaling ā Automatically adjust capacity.
ā High performance ā Ideal for real-time applications.
---
$1
Why This Matters
Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.
Trade-offs to Consider
Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.
When NOT to Use This
Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.
Decision Framework
Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.