Fatskills
Practice. Master. Repeat.
Study Guide: TECH **AWS ElastiCache (Redis vs Memcached) & Session State: Zero-Fluff Study Guide**
Source: https://www.fatskills.com/aws-certified-solutions-architect-associate/chapter/tech-aws-elasticache-redis-vs-memcached-session-state-zero-fluff-study-guide

TECH **AWS ElastiCache (Redis vs Memcached) & Session State: Zero-Fluff Study Guide**

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~6 min read

AWS ElastiCache (Redis vs Memcached) & Session State: Zero-Fluff Study Guide

For AWS Solutions Architect – Associate (SAA-C03) and Real-World Production


1. What This Is & Why It Matters

You’re a cloud engineer at a fast-growing e-commerce startup. Your monolithic app is slowing to a crawl during Black Friday sales because every user request hits the database for session data (e.g., shopping carts, login status). Your CTO demands a fix today—without rewriting the app.

Enter ElastiCache: AWS’s managed in-memory caching service. It sits between your app and database, slashing latency from 100ms (DB query) to <1ms (cache hit). But here’s the catch: - Redis and Memcached are not interchangeable. Pick wrong, and you’ll either: - Lose data (Memcached’s lack of persistence).
- Pay 2x more (Redis’s replication overhead).
- Fail compliance (Redis’s encryption-at-rest vs Memcached’s none).

This guide gives you: ✅ Decision matrix (Redis vs Memcached) for session state.
Step-by-step deployment of a Redis cluster for session caching.
Production-ready configs (security, scaling, monitoring).
Exam traps (e.g., "Why can’t you use Memcached for multi-AZ failover?").


2. Core Concepts & Components


? ElastiCache

  • Definition: AWS-managed in-memory cache (Redis or Memcached) that reduces database load.
  • Production Insight: Without caching, your RDS bill will skyrocket during traffic spikes. A single Redis node can handle 100K+ requests/sec.

? Redis (Cluster Mode Disabled vs Enabled)

  • Definition:
  • Cluster Mode Disabled: Single primary node + read replicas (simpler, but manual failover).
  • Cluster Mode Enabled: Sharded data across multiple nodes (scales writes, but complex).
  • Production Insight:
  • Use Cluster Mode Disabled for session state (low latency, simple failover).
  • Use Cluster Mode Enabled for high-throughput apps (e.g., leaderboards, real-time analytics).

? Memcached

  • Definition: Multi-threaded, simple key-value cache (no persistence, no replication).
  • Production Insight:
  • Memcached is cheaper and faster for ephemeral data (e.g., HTML fragments, API responses).
  • Never use it for session state if you need durability (data disappears on node restart).

? Session State

  • Definition: User-specific data (e.g., shopping cart, login token) stored between requests.
  • Production Insight:
  • Sticky sessions (ALB) + ElastiCache is the gold standard for scalability.
  • Without caching, your app will crash under load (e.g., 10K users = 10K DB queries/sec).

? Cache Hit Ratio

  • Definition: % of requests served from cache (not DB). Aim for >90%.
  • Production Insight:
  • Monitor CacheHitRate in CloudWatch. If <80%, your TTL is too short or data isn’t cacheable.

? TTL (Time-to-Live)

  • Definition: How long data stays in cache before expiring.
  • Production Insight:
  • Set TTL too short → cache thrashing (high DB load).
  • Set TTL too long → stale data (e.g., outdated prices).

? Eviction Policies

  • Definition: How the cache removes data when full (e.g., allkeys-lru for Redis, lru for Memcached).
  • Production Insight:
  • Default eviction (volatile-lru) only removes keys with TTL. Use allkeys-lru for session state.


3. Step-by-Step: Deploy Redis for Session State


Prerequisites

  • AWS account with admin IAM permissions.
  • VPC with private subnets (for security).
  • EC2 instance (or local machine) with AWS CLI configured.

Step 1: Create a Redis Cluster (CLI)

# Create a Redis cluster (Cluster Mode Disabled, 1 primary + 1 replica)
aws elasticache create-cache-cluster \
  --cache-cluster-id "session-cache" \
  --cache-node-type "cache.t3.micro" \
  --engine "redis" \
  --engine-version "6.2" \
  --num-cache-nodes 2 \  # 1 primary + 1 replica
  --az-mode "cross-az" \  # Multi-AZ failover
  --preferred-availability-zones "us-east-1a" "us-east-1b" \
  --security-group-ids "sg-12345678" \  # Allow EC2/app access
  --snapshot-retention-limit 7  # Daily backups for 7 days

Verify:


aws elasticache describe-cache-clusters --cache-cluster-id "session-cache"

Look for CacheClusterStatus: available.

Step 2: Configure Security Group

  • Allow inbound traffic from your app’s security group on port 6379 (Redis).
  • Deny all other traffic (use NACL if extra paranoid).

Step 3: Connect Your App to Redis

Python (Flask) Example:


import redis
import os

# Get Redis endpoint from AWS Console or CLI
REDIS_ENDPOINT = os.getenv("REDIS_ENDPOINT", "session-cache.abc123.ng.0001.use1.cache.amazonaws.com")

r = redis.Redis(
host=REDIS_ENDPOINT,
port=6379,
password=None, # Enable Redis AUTH in production!
decode_responses=True ) # Store session data r.set("user:123:cart", '{"items": ["book1", "book2"]}', ex=3600) # TTL: 1 hour # Retrieve session data cart = r.get("user:123:cart") print(cart) # Output: {"items": ["book1", "book2"]}

Node.js (Express) Example:


const redis = require("redis");
const client = redis.createClient({
  url: "redis://session-cache.abc123.ng.0001.use1.cache.amazonaws.com:6379"
});

client.connect().then(() => {
  client.set("user:123:cart", JSON.stringify({ items: ["book1"] }), { EX: 3600 });
  client.get("user:123:cart").then(console.log);  // Output: {"items": ["book1"]}
});

Step 4: Enable Redis AUTH (Security)

# Set a password (replace "yourpassword" with a strong secret)
aws elasticache modify-cache-cluster \
  --cache-cluster-id "session-cache" \
  --auth-token "yourpassword" \
  --apply-immediately

Update your app code to include the password:


r = redis.Redis(..., password="yourpassword")

Step 5: Monitor Cache Performance

  • CloudWatch Metrics to Watch:
  • CacheHitRate (aim for >90%).
  • CPUUtilization (scale up if >70%).
  • Evictions (increase node size if >0).
  • Enable Enhanced Monitoring (1-minute granularity): bash aws elasticache modify-cache-cluster \
    --cache-cluster-id "session-cache" \
    --enable-cloudwatch-metrics


4. ? Production-Ready Best Practices


Security

  • Enable Redis AUTH (even in private subnets).
  • Use TLS (set transit-encryption-enabled=true).
  • Restrict access via security groups (only allow app servers).
  • Rotate AUTH tokens quarterly (use AWS Secrets Manager).

Cost Optimization

  • Start small (cache.t3.micro) and scale up.
  • Use reserved nodes for long-term workloads (up to 75% discount).
  • Set TTL wisely (e.g., 1 hour for sessions, 5 minutes for API responses).

Reliability & Maintainability

  • Multi-AZ failover (for Redis, not Memcached).
  • Enable backups (snapshot-retention-limit=7).
  • Tag resources (e.g., Environment=prod, Purpose=session-cache).

Observability

  • CloudWatch Alarms:
  • CacheHitRate < 80% → Investigate TTL or cacheable data.
  • CPUUtilization > 70% → Scale up.
  • Enable slow-log (Redis) to debug slow queries.


5. ⚠️ Common Mistakes & Traps

Mistake Symptom Fix/Prevention
Using Memcached for sessions Session data disappears on node restart Use Redis (persistent) instead.
No TTL on session keys Cache grows infinitely, evictions spike Set EX (Redis) or expire (Memcached).
Single-AZ deployment App crashes if AZ fails Use cross-az for Redis.
No Redis AUTH Unauthorized access to session data Enable AUTH and rotate tokens.
Ignoring Evictions Cache thrashing, high DB load Monitor Evictions and scale up.


6. ? Exam/Certification Focus


Typical Question Patterns

  1. "Which ElastiCache engine supports multi-AZ failover?"
  2. Redis (Memcached is single-AZ only).
  3. ❌ Memcached.

  4. "You need to cache session data for a high-traffic app. Which engine is best?"

  5. Redis (persistent, supports replication).
  6. ❌ Memcached (loses data on restart).

  7. "How do you secure ElastiCache in a VPC?"

  8. Security groups + Redis AUTH + TLS.
  9. ❌ "Just use a private subnet" (not enough).

Key Trap Distinctions

  • Redis vs Memcached: | Feature | Redis | Memcached | |------------------|---------------------|--------------------| | Persistence | ✅ Yes | ❌ No | | Multi-AZ | ✅ Yes | ❌ No | | Data Types | ✅ Strings, Lists, Sets | ❌ Strings only | | Cost | Higher (replication) | Lower |

  • Cluster Mode Disabled vs Enabled:

  • Disabled: Simple, 1 primary + N replicas (good for sessions).
  • Enabled: Sharded, scales writes (good for high-throughput apps).


7. ? Hands-On Challenge

Scenario: Your Flask app stores user sessions in Redis. After a deployment, users report losing their shopping carts. Debug and fix the issue.

Solution: 1. Check Redis logs for evictions:
bash
aws elasticache describe-events --source-identifier "session-cache"
2. If Evictions > 0, increase node size:
bash
aws elasticache modify-cache-cluster \
--cache-cluster-id "session-cache" \
--cache-node-type "cache.t3.small" \
--apply-immediately
3. Why it works: Evictions happen when the cache is full. Scaling up gives more memory.


8. ? Rapid-Reference Crib Sheet

Item Value Notes
Redis default port 6379
Memcached default port 11211
Redis AUTH command AUTH yourpassword Enable in production!
Redis TTL command SET key value EX 3600 TTL in seconds.
Memcached TTL command set key value 0 3600 TTL in seconds.
Redis persistence ✅ Yes (RDB/AOF) ⚠️ Disabled by default.
Memcached persistence ❌ No Data lost on restart.
Multi-AZ support ✅ Redis only ⚠️ Memcached is single-AZ.
Cost (us-east-1, on-demand) cache.t3.micro: $0.017/hr
CloudWatch metric for hits CacheHitRate Aim for >90%.
CloudWatch metric for load CPUUtilization Scale up if >70%.


9. ? Where to Go Next

  1. AWS ElastiCache Documentation
  2. Redis vs Memcached: Deep Dive
  3. ElastiCache Best Practices (AWS Whitepaper)
  4. Session Management with Redis (Flask Tutorial)


ADVERTISEMENT