Ace Cloud Interviews
Home/AWS Tutorial/ElastiCache
🗃️

AWS Database

ElastiCache

Managed in-memory caching with Redis or Memcached to accelerate application performance

Amazon ElastiCache is a fully managed in-memory caching service that supports two engines: Redis and Memcached. It is used to accelerate applications by storing frequently accessed data in memory, reducing database load and cutting response times from milliseconds to microseconds. ElastiCache is a core component of almost every high-scale AWS architecture, handling session storage, query result caching, leaderboards, pub/sub messaging, and rate limiting.

Redis vs Memcached: Choosing the Right Engine

Redis and Memcached solve different problems. Redis is richer and more durable; Memcached is simpler and horizontally scalable for pure caching. In practice, Redis is chosen for almost all new workloads because of its data structure support and persistence options.

FeatureRedisMemcached
Data structuresStrings, hashes, lists, sets, sorted sets, streams, bitmaps, HyperLogLogStrings only
PersistenceRDB snapshots + AOF (append-only file)None - data lost on restart
ReplicationPrimary-replica with automatic failover (Cluster Mode or Sentinel)None
Multi-AZ failoverYes (automatic with Redis Cluster Mode)No
Pub/Sub messagingYesNo
Lua scriptingYesNo
Max memory per nodeUp to 419 GB (r6g.16xlarge)Up to 419 GB
Horizontal scaling (data sharding)Yes (Cluster Mode Enabled)Yes (native multi-threaded)
Use caseSessions, leaderboards, rate limiting, queues, pub/sub, cachingSimple object caching, horizontal scaling simplicity
💡

If you need any of: replication, failover, persistence, complex data types, or pub/sub - choose Redis. Memcached is only preferable if you need the simplest possible horizontal scaling and do not need any of the above features.

Redis Cluster Architecture: Cluster Mode Disabled vs Enabled

ElastiCache Redis can run in two modes. The mode you choose determines how data is sharded and how you scale.

AttributeCluster Mode DisabledCluster Mode Enabled
ShardingSingle shard (one primary)Up to 500 shards
Max data sizeLimited to one node's memoryTotal memory across all shards
Scaling writesScale up (larger instance)Scale out (add shards)
Read replicasUp to 5 per primaryUp to 5 per shard
Multi-AZYes (replica in different AZ)Yes (replicas distributed across AZs)
Replication typeAsync to replicasAsync to replicas within each shard
Client complexitySingle endpointMust use cluster-aware client
⚠️

You cannot enable or disable cluster mode on an existing cluster. You must create a new cluster and migrate data. Plan for cluster mode from the start if you anticipate needing to scale beyond a single node's memory.

Caching Strategies: Lazy Loading, Write-Through, Write-Behind

How you populate and invalidate the cache is as important as the cache itself. The wrong strategy leads to stale data, cache stampedes, or high cache miss rates.

StrategyHow It WorksProsCons
Lazy Loading (Cache-Aside)Read cache; on miss, fetch from DB, store in cacheOnly caches accessed data; resilient to cache failureCache miss adds latency; stale data possible
Write-ThroughWrite to cache and DB simultaneously on every writeCache always fresh; no stale readsWrite penalty; caches data that may never be read
Write-Behind (Write-Back)Write to cache immediately; flush to DB asynchronouslyLowest write latencyRisk of data loss if cache node fails before flush
Refresh-AheadProactively refresh cache before TTL expiresEliminates cache miss latency for popular keysComplex to implement; may fetch unused data
💡

Lazy Loading with a TTL is the most common and safest pattern. Use Write-Through when you cannot tolerate stale reads. Never use Write-Behind for data you cannot afford to lose.

ElastiCache as a Session Store

Storing user sessions in ElastiCache Redis is one of the most common and well-understood patterns. It solves the sticky session problem in horizontally scaled applications.

ApproachProblemElastiCache Solution
Sticky sessions on ALBSingle instance holds session; loses data on instance failureAll instances share Redis; any instance can serve any user
Local in-memory sessionSession lost if process restartsRedis persists session with configurable TTL
RDS session storageDatabase becomes a bottleneck for session readsMicrosecond reads from Redis; no DB query needed
bash
# Example: set a session in Redis with 30-minute TTL
# Using redis-cli
SET session:abc123 '{"userId": "user#456", "role": "admin"}' EX 1800

# Get session
GET session:abc123

# Delete session on logout
DEL session:abc123

# Check TTL remaining
TTL session:abc123

Security: Encryption, Auth, and Network Isolation

ElastiCache runs inside your VPC. Security is layered: network isolation via security groups, in-transit encryption via TLS, and authentication via Redis AUTH or IAM (for newer engine versions).

Security LayerMechanismNotes
NetworkVPC subnet groups + security groupsNever expose ElastiCache endpoints to the internet
In-transit encryptionTLS (enable at cluster creation)Requires TLS-capable client; small performance overhead
At-rest encryptionAES-256 via KMSFor Redis Cluster Mode Enabled
AuthenticationRedis AUTH token or IAM auth (Redis 7+)AUTH token is a shared password; IAM is preferred
RBACRedis ACLs (users/passwords per command/key pattern)Available on Redis 6+; replaces single AUTH token
⚠️

Encryption in transit must be enabled at cluster creation. You cannot enable TLS on an existing cluster without creating a new one. Always enable TLS for any cluster handling sensitive data.

Pricing Model

ComponentPricing BasisOptimization Tip
Node hoursPer hour by node type (cache.t4g, cache.r7g, etc.)Reserve 1-3 years for production savings up to 55%
Backup storagePer GB-month beyond free allowance (1x cluster size free)Reduce backup retention for dev clusters
Data transferIntra-AZ free; cross-AZ charged per GBPlace application and cache in same AZ
Serverless ElastiCachePer ECU (ElastiCache Compute Unit) and GB storedNo capacity planning needed; pay for what you use
💡

ElastiCache Serverless (Redis and Memcached) launched in 2023 and automatically scales capacity. It charges per ECU and data stored - useful for unpredictable workloads where you want to avoid over-provisioning.

🎯

Interview Focus Points

  • 1What is the difference between Redis and Memcached in ElastiCache? When would you choose each?
  • 2Explain lazy loading vs write-through caching. What are the trade-offs?
  • 3What is the difference between Cluster Mode Enabled and Cluster Mode Disabled in Redis?
  • 4How would you implement session management using ElastiCache Redis in a horizontally scaled application?
  • 5How do you handle cache invalidation? What strategies exist and what are the pitfalls?
  • 6What is a cache stampede and how do you prevent it?
  • 7How do you secure an ElastiCache cluster? Walk through each layer.
  • 8When would you use ElastiCache vs DynamoDB DAX vs CloudFront for caching?
  • 9A Redis cluster is running out of memory. What are your options and how do you decide?