Redis vs Memcached: Choosing the Right Engine
Compare Redis (persistence, replication, sorted sets, pub/sub) with Memcached (simplicity, multi-threading) and select based on use-case requirements.
Redis vs Memcached: Choosing the Right Engine is a free AWS Solutions Architect lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Amazon ElastiCache?
Amazon ElastiCache is a fully managed, in-memory caching service that makes it easy to deploy, manage, and scale popular open-source in-memory data stores in the cloud. It supports two engines: Redis and Memcached. By serving frequently requested data from memory rather than from a database, ElastiCache can reduce database load by orders of magnitude and cut response latency from milliseconds to microseconds. Choosing between Redis and Memcached is a common SAA-C03 exam question.
Memcached: Pure Simplicity
Memcached is a distributed, in-memory key-value store focused on simplicity and horizontal scalability. Key characteristics: multi-threaded (can use all CPU cores on a single node), simple key-value storage (strings only, no complex data structures), no persistence (data is lost when a node restarts), no replication (no standby or replicas), and horizontal sharding via client-side consistent hashing. Memcached is the right choice when you need a simple, large-scale cache and have no need for persistence, complex data types, or high availability.
# Create a Memcached cluster with 3 nodes
aws elasticache create-cache-cluster \
--cache-cluster-id my-memcached \
--engine memcached \
--cache-node-type cache.r7g.large \
--num-cache-nodes 3 \
--cache-subnet-group-name my-subnet-group
# Memcached auto-discovers nodes via the config endpoint
# Application connects to: my-memcached.cfg.use1.cache.amazonaws.com:11211Redis: Feature-Rich In-Memory Store
Redis (Remote Dictionary Server) is a single-threaded (per-instance), feature-rich in-memory data structure store that supports strings, hashes, lists, sets, sorted sets, bitmaps, HyperLogLogs, streams, and geospatial indexes. Key advantages over Memcached: optional persistence (AOF and RDB snapshots), replication (read replicas and automatic failover), pub/sub messaging, Lua scripting, transactions (MULTI/EXEC), and cluster mode for horizontal sharding. Redis is the right choice for most real-world caching scenarios.
# Redis data structure examples
# String
# SET user:1:name 'Alice'
# GET user:1:name
# Hash (object-like)
# HSET product:42 name 'Widget' price '9.99' stock '100'
# HGETALL product:42
# Sorted set (leaderboard)
# ZADD leaderboard 9500 'alice' 8700 'bob' 9100 'carol'
# ZREVRANGE leaderboard 0 2 WITHSCORES
# Pub/Sub
# PUBLISH notifications 'order_shipped:12345'
# SUBSCRIBE notificationsRedis Persistence: AOF and RDB
Redis supports two persistence mechanisms: RDB (Redis Database) — periodic point-in-time snapshots saved to disk at configurable intervals (fast restarts, but can lose data between snapshots). AOF (Append-Only File) — logs every write operation; on restart Redis replays the log to rebuild state (durability at the cost of larger files and slower restarts). In ElastiCache, you can enable AOF to persist data across node restarts. This is critical when ElastiCache is used not just as a cache but as a primary data store (for session data, for example).
# Create an ElastiCache Redis cluster with AOF enabled
aws elasticache create-replication-group \
--replication-group-id my-redis \
--replication-group-description 'Redis with persistence' \
--cache-node-type cache.r7g.large \
--engine redis \
--num-cache-clusters 2 \
--cache-parameter-group-name default.redis7 \
--snapshot-retention-limit 5
# Note: Enable AOF via parameter group: appendonly=yesRedis Replication and Automatic Failover
Redis supports replication groups with one primary node that handles all writes and up to 5 read replicas that handle reads. With Multi-AZ and automatic failover enabled, ElastiCache promotes a read replica to primary automatically when the primary fails — typically completing the failover in under 60 seconds. This gives Redis-backed applications high availability that Memcached cannot match (Memcached has no replication). For production applications requiring HA caching, always use Redis with automatic failover.
# Create a Redis replication group with Multi-AZ failover
aws elasticache create-replication-group \
--replication-group-id prod-redis \
--description 'Production Redis with HA' \
--cache-node-type cache.r7g.xlarge \
--engine redis \
--multi-az-enabled \
--automatic-failover-enabled \
--num-cache-clusters 3 \
--cache-subnet-group-name multi-az-subnet-group
# 1 primary + 2 replicas across 3 AZsRedis Sorted Sets for Leaderboards
Sorted sets are one of Redis's most powerful data structures. Each member has an associated floating-point score, and members are always kept in sorted order. This makes sorted sets ideal for leaderboards (rank by score), priority queues (process highest-priority jobs first), and rate limiting (sliding window with timestamps as scores). The operations ZADD, ZRANGE, ZREVRANGE, ZRANK, and ZRANGEBYSCORE are all O(log n) — extremely efficient even for millions of entries.
# Leaderboard operations using Redis sorted sets
# Add/update scores
# ZADD game:leaderboard 10500 'player:alice'
# ZADD game:leaderboard 9800 'player:bob'
# ZADD game:leaderboard 11200 'player:carol'
# Get top 3 players (highest scores first)
# ZREVRANGE game:leaderboard 0 2 WITHSCORES
# Result: carol 11200, alice 10500, bob 9800
# Get a player's rank (0-indexed)
# ZREVRANK game:leaderboard 'player:alice'
# Result: 1 (second place)Redis Pub/Sub for Messaging
Redis pub/sub allows publishers to broadcast messages to channels without knowing who is subscribed. Subscribers receive all messages published to channels they subscribe to, in real time. Pub/sub messages are not persisted — if a subscriber is offline, it misses the message. For persistent messaging with guaranteed delivery, use Redis Streams (a more robust data structure added in Redis 5) or a purpose-built messaging service like SQS or SNS. For the SAA-C03 exam, Redis pub/sub is an option for lightweight real-time notifications between services.
# Publisher side (broadcasts to 'notifications' channel)
# PUBLISH notifications '{"type":"order_shipped","orderId":"12345"}'
# Subscriber side (listens for messages)
# SUBSCRIBE notifications
# Pattern subscribe (wildcard channel matching)
# PSUBSCRIBE order:*
# Receives messages from: order:created, order:shipped, order:delivered
# Note: Unlike SQS, pub/sub is fire-and-forget — no acknowledgementMemcached vs Redis: Decision Table
Use this framework to choose for the SAA-C03 exam: Choose Memcached when you need: simple cache with no HA requirement, multi-threaded performance, horizontal scaling by adding nodes (pure cache). Choose Redis when you need: persistence (session store), replication and failover (HA), complex data types (sorted sets for leaderboards, sets, lists), pub/sub, Lua transactions, Cluster Mode for multi-shard horizontal scaling. If the exam question mentions any feature beyond simple key-value caching, Redis is almost always the answer.
Cache Node Types and Families
ElastiCache offers several node type families: r7g (Graviton 3, memory-optimised — best price/performance, recommended for most caching), m7g (balanced compute and memory), and t4g (burstable, low cost for dev/test). Node sizes range from cache.t4g.micro (500 MB) to cache.r7g.16xlarge (425 GB). For production, choose a node size that keeps your dataset in memory with 20-25% headroom. Running out of memory causes evictions (Memcached) or OOM errors (Redis).
# Get available cache node types
aws elasticache describe-cache-engine-versions \
--engine redis \
--query 'CacheEngineVersions[?contains(EngineVersion, '7')].{Engine:Engine,Version:EngineVersion}'
# Check memory and vCPU for a node type
aws elasticache describe-cache-engine-versions \
--cache-parameter-group-family redis7
# Monitor evictions to detect memory pressure
# CloudWatch: Evictions metric > 0 means cache is fullElastiCache Security
ElastiCache is deployed inside your VPC with security groups controlling access — no public internet access by default. For Redis, enable AUTH tokens (a password that clients must provide) and in-transit encryption (TLS) for connections, plus at-rest encryption using KMS. For Memcached, only in-transit TLS is available (no at-rest encryption or AUTH). When migrating from unencrypted to encrypted Redis, there is no in-place migration — you must create a new encrypted cluster and warm it up.
# Create a Redis cluster with TLS and AUTH token
aws elasticache create-replication-group \
--replication-group-id secure-redis \
--description 'Encrypted Redis' \
--cache-node-type cache.r7g.large \
--engine redis \
--transit-encryption-enabled \
--at-rest-encryption-enabled \
--auth-token 'MySecretPassword123!'
# Store the AUTH token in Secrets Manager, not in your app codeEviction Policies
When a Redis or Memcached cache is full, the eviction policy determines which keys are removed to make space. Common Redis eviction policies: noeviction (returns OOM error — good when cache is primary store), allkeys-lru (evict least recently used across all keys — good general cache), volatile-lru (LRU among keys with TTL set — preserves non-expiring keys), allkeys-random (random eviction), allkeys-lfu (least frequently used). For typical caching use cases, allkeys-lru or allkeys-lfu are recommended.
# Set eviction policy via parameter group
aws elasticache create-cache-parameter-group \
--cache-parameter-group-name redis7-lru \
--cache-parameter-group-family redis7 \
--description 'LRU eviction policy'
aws elasticache modify-cache-parameter-group \
--cache-parameter-group-name redis7-lru \
--parameter-name-values ParameterName=maxmemory-policy,ParameterValue=allkeys-lru
# Apply the parameter group to your cluster:
aws elasticache modify-replication-group \
--replication-group-id prod-redis \
--cache-parameter-group-name redis7-lruQuick Check
Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.
Lesson Recap
In this lesson you learned: Memcached is simple, multi-threaded, and has no HA — best for pure horizontal scaling of simple caches, Redis supports complex data structures, persistence, pub/sub, and Multi-AZ failover — best for most production caching scenarios, and sorted sets make Redis the natural choice for leaderboards and priority queues. Next up we explore Redis Replication Groups and Cluster Mode for horizontal sharding.
Frequently asked questions
Is the “Redis vs Memcached: Choosing the Right Engine” lesson free?
Yes — the full text of “Redis vs Memcached: Choosing the Right Engine” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.
What will I learn in “Redis vs Memcached: Choosing the Right Engine”?
Compare Redis (persistence, replication, sorted sets, pub/sub) with Memcached (simplicity, multi-threading) and select based on use-case requirements. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AWS Solutions Architect?
No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Redis vs Memcached: Choosing the Right Engine” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AWS Solutions Architect lesson?
Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Redis vs Memcached: Choosing the Right Engine
- ElastiCache Redis Replication Groups and Cluster Mode
- Caching Strategies: Lazy Loading and Write-Through
- Session Storage and Leaderboard Patterns