# Demystifying Redis in CloudFormation - Multi-AZ and Multi-Region

**URL:** <https://awshq.com/t/demystifying-redis-in-cloudformation-multi-az-and-multi-region/14190>\
**Category:** Architecture & IaC\
**Tags:** cloudformation, elasticache\
**Created:** [January 4, 2022, 12:42am UTC](https://awshq.com/t/demystifying-redis-in-cloudformation-multi-az-and-multi-region/14190 "2022-01-04T00:42:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saru2929](https://awshq.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Saru2929](https://awshq.com/u/Saru2929)\
**Post date:** [January 4, 2022, 12:42am UTC](https://awshq.com/t/demystifying-redis-in-cloudformation-multi-az-and-multi-region/14190/1 "2022-01-04T00:42:11Z")

</div>

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](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/Redis-Global-Datastores-Getting-Started.html).

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.

* * *

**Related discussions:**

- [Whats the cheapest cache you can think of on aws?](https://awshq.com/t/whats-the-cheapest-cache-you-can-think-of-on-aws/18073)
- [Preventing DDoS on Lambda without AWS Shield Advanced](https://awshq.com/t/preventing-ddos-on-lambda-without-aws-shield-advanced/20502)
- [Is it actually possible to stay in the free tier?](https://awshq.com/t/is-it-actually-possible-to-stay-in-the-free-tier/18982)
- [How did you build stronger AWS design instincts while still doing mostly ops work?](https://awshq.com/t/how-did-you-build-stronger-aws-design-instincts-while-still-doing-mostly-ops-work/21240)
- [URL Shortener (Hexagonal & Serverless Architecture in AWS)](https://awshq.com/t/url-shortener-hexagonal-serverless-architecture-in-aws/17699)

---

<div class="post-metadata">

**Author:** ![davladdit](https://awshq.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@davladdit](https://awshq.com/u/davladdit)\
**Post date:** [December 30, 2022, 3:03am UTC](https://awshq.com/t/demystifying-redis-in-cloudformation-multi-az-and-multi-region/14190/2 "2022-12-30T03:03:54Z")

</div>

Incredibly helpful and insightful! Wish I had stumbled upon this post earlier.

---

<div class="post-metadata">

**Author:** ![Saru2929](https://awshq.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Saru2929](https://awshq.com/u/Saru2929)\
**Post date:** [December 30, 2022, 4:08am UTC](https://awshq.com/t/demystifying-redis-in-cloudformation-multi-az-and-multi-region/14190/3 "2022-12-30T04:08:27Z")

</div>

> [@davladdit](#):
>
> Incredibly helpful and insightful! Wish I had stumbled upon this post earlier.

Glad I could help! The same concepts apply to a lot of other global database systems.

---

<div class="post-metadata">

**Author:** ![idiots\_dot\_fm](https://awshq.com/letter_avatar_proxy/v4/letter/i/a183cd/32.png) [@idiots\_dot\_fm](https://awshq.com/u/idiots_dot_fm)\
**Post date:** [January 21, 2023, 7:02am UTC](https://awshq.com/t/demystifying-redis-in-cloudformation-multi-az-and-multi-region/14190/4 "2023-01-21T07:02:49Z")

</div>

Thanks for this.

---

<div class="post-metadata">

**Author:** ![thedutchbag](https://awshq.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@thedutchbag](https://awshq.com/u/thedutchbag)\
**Post date:** [July 30, 2024, 5:25am UTC](https://awshq.com/t/demystifying-redis-in-cloudformation-multi-az-and-multi-region/14190/5 "2024-07-30T05:25:51Z")

</div>

> [@Saru2929](#):
>
> Glad I could help! The same concepts apply to a lot of other global database systems.

Any tips on insight on updating the “propagated” values after global cluster replication is enabled?

I notice in the UI, it blocks off editing those directly. I tried editing the CacheNodeType on the primary via CF, and it blocked that. I edited it on the GlobalReplicationGroup, and it appears to have been applied, but now I have the CacheNodeType defined in two places (one, outdated, on the ReplicationGroup - the other, current, via the GlobalReplicationGroup).

I feel weird “progressing” a CF template over time like this - having to create it, then edit the template to remove parts of it and add other parts to change a node type.

---

<div class="post-metadata">

**Author:** ![Saru2929](https://awshq.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Saru2929](https://awshq.com/u/Saru2929)\
**Post date:** [July 30, 2024, 3:46pm UTC](https://awshq.com/t/demystifying-redis-in-cloudformation-multi-az-and-multi-region/14190/6 "2024-07-30T15:46:20Z")

</div>

> [@thedutchbag](#):
>
> Any tips on insight on updating the “propagated” values after global cluster replication is enabled? I notice in the UI, it blocks off editing those directly. I tried editing the CacheNodeType on the primary via CF, and …

One of the unfortunate limitations of CloudFormation. Most changes after enabling global replication have to be done via the global settings or via command line. This becomes “stack drift” and then when you have a difference you can no longer modify the original stack. It’s really cumbersome and frustrating. You could explore the drift detection functionality in CloudFormation but I haven’t had any luck with it yet.  
Was trying to figure some of this with terraform and it’s a better experience but it still not a graceful solution.
