I struggled through trying to piece any of this together as I was building out my Redis cluster so sharing what I learned to help others who may hit the same issues.
TL;DR - if you want to create replication groups in different regions with a global datastore you have to first create the primary replication group, then create a global store that uses that primary - then attach secondary/additional ones to the global.
An ElastiCache cluster can use either Redis or Memcached as the engine. Some of the CloudFormation AWS::ElastiCache properties are used for only one engine and some have different meanings for each engine - the AWS documentation does a garbage job of specifying which is which. This breakdown is specifically for Redis.
In a real world application, you would probably want your application to be Highly Available (multiple availability zones) as well as durable (easy failover between regions with a global datastore). To do this, you would need 2 replication groups with Multi-AZ enabled and a global datastore. Here’s how all those pieces fit together in cloudformation.
CacheCluster v. ReplicationGroup
Creating a AWS::ElastiCache::ReplicationGroup does not require an AWS::ElastiCache::CacheCluster.
The CacheCluster is a 0 shard, 1 node “cluster” - that’s what gives you the limitation of NumCacheNodes = 1. If you need more than a single node (which you probably do or will need in the future) a replication group CFN template creates the primary node and associated replicas, thus creating what you would consider a “cluster” seen in the console view.
| Console Term | CFN Property |
|---|---|
| Cluster | CacheCluster or ReplicationGroup |
| Shards | NumNodeGroups |
| Nodes | ReplicasPerNodeGroup + 1 (for primary node) |
Here is a sample Cache Cluster (assume remaining !Ref and !GetAtt are passed as outputs from other resources or nested stacks):
CacheCluster:
Type: AWS::ElastiCache::CacheCluster
Properties:
ClusterName: redis-dev
CacheNodeType: cache.r5.large
CacheSubnetGroupName: !Ref CacheSubnetGroup
Engine: redis
EngineVersion: 6.x
NumCacheNodes: 1 #has to be 1 for redis
VpcSecurityGroupIds:
- !GetAtt [ SecurityGroupStack, Outputs.RedisSecurityGroup ]
And here is a sample Replication Group (assume remaining !Ref and !GetAtt are passed as outputs from other resources or nested stacks):
ReplicationGroupStack:
Type: AWS::ElastiCache::ReplicationGroup
Properties:
ReplicationGroupId: primary-redis-dev
ReplicationGroupDescription: Primary replication group for Redis
Engine: redis
EngineVersion: 6.x
CacheSubnetGroupName: !Ref CacheSubnetGroup
CacheNodeType: cache.r5.large
NumNodeGroups: 2 #remember this is shards in the console
ReplicasPerNodeGroup: 2 #creates 2 replica nodes, resulting in total 3 nodes seen in the cluster (add 1 for the primary)
AtRestEncryptionEnabled: true
TransitEncryptionEnabled: true
MultiAZEnabled: true
CacheParameterGroupName: !Ref CacheParameterGroup
SecurityGroupIds:
- !GetAtt [ SecurityGroupStack, Outputs.RedisSecurityGroup ]
You only need to use cluster-mode enabled if you need multiple shards. A replication group can exist with cluster mode disabled, it will just have 1 shard.
Even if you are planning to use a global datastore, do not specify a GlobalReplicationGroupId in the primary replication group (more on this below).
Global Datastores
Create a global datastore using AWS::ElastiCache::GlobalReplicationGroup. If you are planning to use a global datastore, be sure the node type you’ve selected is available in all regions - this can be found in the Prerequisites and Limitations.
For Members - only specify the primary replication group ID.
Here is a basic example (assume remaining !Ref is passed as an output from another nested stack):
Resources:
GlobalReplicationGroup:
Type: AWS::ElastiCache::GlobalReplicationGroup
Properties:
GlobalReplicationGroupIdSuffix: redis-globaldatastore-dev
GlobalReplicationGroupDescription:The global datastore for the Redis cache.
Members:
- ReplicationGroupId: !Ref PrimaryReplicationGroup
ReplicationGroupRegion: us-east-1
Role: PRIMARY
Creating Secondary/Additional Replication Groups
The GlobalReplicationGroupId in the replication group properties is used in the secondary replication group to associate it with the global datastore. You do not need to specify parameters such as Engine, EngineVersion, CacheNodeType, etc… because ElastiCache will know to use the same values as the ones provided in the primary replication group of the global datastore.
Sample Secondary (assume remaining !Ref and !GetAtt are passed as outputs from other resources or nested stacks)
SecondaryReplicationGroupStack:
Type: AWS::ElastiCache::ReplicationGroup
Properties:
ReplicationGroupId: secondary-redis-dev
ReplicationGroupDescription: Secondary replication group for Redis
GlobalReplicationGroupId: ldgnf-redis-globaldatastore-dev
ReplicasPerNodeGroup: 2
MultiAZEnabled: true
CacheSubnetGroupName: !Ref CacheSubnetGroup
SecurityGroupIds:
- !GetAtt [ SecurityGroupStack, Outputs.RedisSecurityGroup ]
Ideal Configuration
Plan to use nested stacks to build all of this out. The nesting structure should look like this:
- Redis Cluster Stack
- Primary Replication Group Stack(output the id of the replication group)
- Replication Group
- Security Group Stack
- CacheSubnetGroup
- CacheParameterGroup
- Global DataStore Stack (uses !GetAtt to pass the primary replication group into the global datastore members)
If you’re trying to set up a secondary/additional replication group in another region, you’ll need to run the script for the secondary replication group in that region. Alternatively, you could consider using StackSets to run the same cfn scripts in both regions, but use conditions to turn on/off sections of the script based on the region.