0Pricing
AWS Solutions Architect · Lesson

ElastiCache Redis Replication Groups and Cluster Mode

Build a Redis replication group for read scaling and enable cluster mode to shard data across multiple node groups.

ElastiCache Redis Replication Groups and Cluster Mode is a free AWS Solutions Architect lesson on CoddyKit — lesson 2 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.

Redis Replication Groups Overview

An ElastiCache Replication Group is a logical grouping of one primary Redis node and up to 5 read replicas. The primary node handles all write operations; replicas receive asynchronous replication from the primary and serve read traffic. Replication groups enable two key capabilities: read scaling (distribute read requests across multiple replicas) and high availability with automatic failover (promote a replica to primary if the primary fails). All nodes in a replication group share the same dataset.

# Create a replication group with 1 primary and 2 read replicas
aws elasticache create-replication-group \
  --replication-group-id web-cache \
  --replication-group-description 'Web application cache' \
  --num-cache-clusters 3 \
  --cache-node-type cache.r7g.large \
  --engine redis \
  --engine-version '7.0' \
  --automatic-failover-enabled \
  --multi-az-enabled \
  --cache-subnet-group-name my-multi-az-subnet-group

Primary Endpoint vs Reader Endpoint

ElastiCache provides two DNS endpoints for a replication group: the primary endpoint always points to the current primary node (automatically updated during failover) — use this for all write operations. The reader endpoint load-balances read requests across all available replicas — use this for read operations to distribute load. Your application should maintain two connection pools: one for the primary endpoint for writes and one for the reader endpoint for reads. This is the recommended connection pattern for ElastiCache Redis replication groups.

# Get primary and reader endpoints
aws elasticache describe-replication-groups \
  --replication-group-id web-cache \
  --query 'ReplicationGroups[].{
    Primary:NodeGroups[].PrimaryEndpoint.Address,
    Reader:ReaderEndpoint.Address
  }'

# Application connection pattern:
# write_client = redis.Redis(host='primary-endpoint', port=6379)
# read_client = redis.Redis(host='reader-endpoint', port=6379)

Automatic Failover Process

When the primary node fails (detected by ElastiCache within seconds via health checks), automatic failover selects one of the read replicas to promote to primary. The promotion process: (1) the selected replica is promoted to primary, (2) the primary endpoint DNS record is updated to point to the new primary (TTL ~1 second), (3) the old primary is replaced with a new replica. Total failover time is typically 30-60 seconds. Applications using the primary endpoint DNS reconnect automatically once DNS propagates — no hardcoded IP addresses needed.

# Test failover manually (triggers primary failover)
aws elasticache test-failover \
  --replication-group-id web-cache \
  --node-group-id 0001

# Monitor failover events
aws elasticache describe-events \
  --source-identifier web-cache \
  --source-type replication-group \
  --duration 60 \
  --query 'Events[].{Time:Date,Message:Message}'

Multi-AZ Replica Placement

For maximum resilience, distribute replicas across multiple Availability Zones. When you enable Multi-AZ on a replication group, ElastiCache automatically places the primary and replicas in different AZs. If an entire AZ goes offline, failover promotes a replica from a surviving AZ. You can also explicitly specify the preferred AZs for each node when creating the replication group using the --preferred-cache-cluster-a-zs option.

# Create replication group with explicit AZ placement
aws elasticache create-replication-group \
  --replication-group-id ha-redis \
  --replication-group-description 'Multi-AZ Redis' \
  --num-cache-clusters 3 \
  --cache-node-type cache.r7g.xlarge \
  --engine redis \
  --automatic-failover-enabled \
  --multi-az-enabled \
  --preferred-cache-cluster-a-zs us-east-1a us-east-1b us-east-1c \
  --cache-subnet-group-name multi-az-subnets

What Is Redis Cluster Mode?

Redis Cluster Mode Enabled (CME) partitions the dataset across multiple node groups (shards), each containing a primary and up to 5 replicas. This is Redis's horizontal sharding solution. Cluster Mode allows you to exceed the memory of a single node — you can have up to 500 node groups each with 500 GB, allowing a single Redis cluster to hold up to 500 × 500 GB = 250 TB of data. Cluster Mode also multiplies write throughput since each node group independently processes writes for its key range.

# Create a Redis Cluster Mode Enabled replication group
# with 3 shards, each with 1 primary and 2 replicas
aws elasticache create-replication-group \
  --replication-group-id clustered-redis \
  --replication-group-description 'Cluster mode: 3 shards x 3 nodes' \
  --num-node-groups 3 \
  --replicas-per-node-group 2 \
  --cache-node-type cache.r7g.large \
  --engine redis \
  --automatic-failover-enabled \
  --multi-az-enabled \
  --cache-subnet-group-name multi-az-subnets

Hash Slots and Key Distribution

Redis Cluster Mode divides the key space into 16,384 hash slots. Each node group owns a contiguous range of hash slots. When a key is written, Redis computes CRC16(key) % 16384 to determine the hash slot and thus the node group responsible. Your application must use a cluster-aware Redis client (like redis-py-cluster or Jedis in cluster mode) that knows the slot map and routes each command to the correct node. Standard clients will return MOVED redirection errors if they send a key to the wrong shard.

# Python cluster-aware client example
# pip install redis[hiredis]

# from redis.cluster import RedisCluster
# cluster_client = RedisCluster(
#   host='clustered-redis.abc123.clustercfg.use1.cache.amazonaws.com',
#   port=6379,
#   decode_responses=True
# )

# The client automatically resolves slot-to-node mapping
# cluster_client.set('user:1', 'Alice')   # routes to correct shard
# cluster_client.get('user:1')            # routes to correct shard

Cluster Mode vs Non-Cluster Mode

For the SAA-C03 exam, choose non-cluster mode when: your data fits on a single node (< ~400 GB after headroom), you need simple primary/replica setup, or your application uses complex multi-key operations (transactions spanning multiple keys require all keys to be on the same slot). Choose cluster mode when: your dataset exceeds a single node's memory, you need horizontal write throughput scaling, or you anticipate future growth that will require online resharding. Cluster mode supports adding shards without downtime (online resharding).

# Scale out a cluster-mode Redis by adding shards
aws elasticache modify-replication-group-shard-configuration \
  --replication-group-id clustered-redis \
  --node-group-count 5 \
  --apply-immediately \
  --resharding-configuration \
    NodeGroupId=0004,PreferredAvailabilityZones=us-east-1a,us-east-1b,us-east-1c \
    NodeGroupId=0005,PreferredAvailabilityZones=us-east-1a,us-east-1b,us-east-1c

# No downtime — slots are migrated incrementally

Global Datastore for Cross-Region Replication

ElastiCache Global Datastore extends Redis replication across multiple AWS Regions. You designate one Region as the primary cluster and add secondary clusters in other Regions. Writes go to the primary; secondaries receive asynchronous replication with typical lag under 1 second. Secondary clusters can serve local reads with very low latency. Global Datastore enables global applications where users in different continents read from the nearest Region and cross-region DR where you can promote a secondary cluster to primary if the primary Region fails.

# Create a Global Datastore (adds a secondary region to an existing cluster)
aws elasticache create-global-replication-group \
  --global-replication-group-id-suffix my-global-cache \
  --primary-replication-group-id prod-redis

# Add a secondary cluster in another region
aws elasticache create-replication-group \
  --replication-group-id prod-redis-eu \
  --replication-group-description 'EU secondary' \
  --global-replication-group-id ldgnf-my-global-cache \
  --region eu-west-1

Redis Pub/Sub at Scale

In a non-clustered Redis replication group, pub/sub messages are distributed to all replicas — any subscriber on any node receives messages published to a channel. However, in Cluster Mode, pub/sub is limited to keyspace notifications and channel-based pub/sub only works within a single shard unless you use Redis 7+ with Pub/Sub sharding (SSUBSCRIBE / SPUBLISH for shard-aware pub/sub). This is an important limitation when designing pub/sub at scale with cluster mode.

# Keyspace notification (fires when a key expires)
# Enable in parameter group: notify-keyspace-events Ex

# Subscriber in Python:
# pubsub = redis_client.pubsub()
# pubsub.psubscribe('__keyevent@0__:expired')
# for message in pubsub.listen():
#     if message['type'] == 'pmessage':
#         expired_key = message['data']
#         print(f'Key expired: {expired_key}')

Monitoring Replication Lag

Monitor the ReplicationLag CloudWatch metric on read replicas to ensure they are keeping up with the primary. A lag above a few seconds indicates a bottleneck on the replica (overloaded node, network issues, or too many writes for the replica to process). In global datastore scenarios, monitor GlobalDatastoreReplicationLag. High replication lag means read replicas may return stale data — important for applications that expect eventual consistency within tight bounds.

# Monitor replication lag for all replicas
aws cloudwatch get-metric-statistics \
  --namespace AWS/ElastiCache \
  --metric-name ReplicationLag \
  --dimensions Name=ReplicationGroupId,Value=web-cache \
  --statistic Maximum \
  --period 60 \
  --start-time $(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%SZ) \
  --end-time $(date -u +%Y-%m-%dT%H:%M:%SZ)

# Alert if ReplicationLag > 10 seconds

Scaling Replication Groups

You can scale vertically (change node type) or scale horizontally (add or remove replicas). Changing node type requires a modify-replication-group call and causes a brief failover when applied immediately — the primary is replaced with a new node of the new type. Adding replicas is online with no downtime. When scaling from non-cluster to cluster mode, you must create a new cluster-mode group and migrate — there is no in-place conversion between cluster and non-cluster mode.

# Scale up node type with maintenance window
aws elasticache modify-replication-group \
  --replication-group-id web-cache \
  --cache-node-type cache.r7g.xlarge \
  --apply-immediately false

# Add a read replica
aws elasticache increase-replica-count \
  --replication-group-id web-cache \
  --new-replica-count 4 \
  --apply-immediately

Quick Check

Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.

Lesson Recap

In this lesson you learned: replication groups provide read scaling via replicas and high availability via automatic failover with primary/reader endpoints, Cluster Mode shards data across up to 500 node groups using 16,384 hash slots to scale horizontally beyond a single node's memory, and Global Datastore replicates data across Regions for global low-latency reads and cross-region DR. Next up we explore caching strategies: lazy loading and write-through.

Frequently asked questions

Is the “ElastiCache Redis Replication Groups and Cluster Mode” lesson free?

Yes — the full text of “ElastiCache Redis Replication Groups and Cluster Mode” 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 “ElastiCache Redis Replication Groups and Cluster Mode”?

Build a Redis replication group for read scaling and enable cluster mode to shard data across multiple node groups. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ElastiCache Redis Replication Groups and Cluster Mode” 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

  1. Redis vs Memcached: Choosing the Right Engine
  2. ElastiCache Redis Replication Groups and Cluster Mode
  3. Caching Strategies: Lazy Loading and Write-Through
  4. Session Storage and Leaderboard Patterns
← Back to AWS Solutions Architect