Amazon ElastiCache for Redis - Global Datastore

Objective

This exercise will show you the steps for using the Global Datastore mechanism for Amazon ElastiCache instances. If you want more information click here.

By the end of this exercise, you will be able to:

  • Create Amazon ElastiCache for Redis
  • Replicate Cluster to Secondary Region
  • Test Amazon ElastiCache across regions
  • Promote Secondary Region to Primary

Estimated Duration: 40 minutes

Approximate Cost: 10 USD

Execution

Go to AWS Cloud9

  1. Create AWS Cloud9 environment in Primary Region N. Virginia

    For more instructions click here.

  2. Install Redis CLI using Cloud9 Terminal

    In order to execute Redis commands, install Redis CLI in your Cloud9 environment.

     # Install command-line JSON processor
     sudo yum install jq -y
     # Install dependencies
     sudo amazon-linux-extras install epel -y
     sudo yum install gcc jemalloc-devel openssl-devel tcl tcl-devel -y
     # Download the redis source code
     sudo wget http://download.redis.io/redis-stable.tar.gz
     sudo tar xvzf redis-stable.tar.gz
     cd redis-stable
     # Build Redis components
     sudo make BUILD_TLS=yes
    
  3. Repeat steps 1 and 2 using the Secondary Region N. California

    You’ll need the two AWS Cloud9 environments in order to access ElastiCache nodes from different regions.

Create an Amazon ElastiCache for Redis cluster

  1. Using AWS Cloud9 terminal in N. Virginia, create a new Redis Cluster (cluster-primary) in the Primary Region.

    aws elasticache create-replication-group \
     --replication-group-id cluster-primary \
     --replication-group-description "DR Workshop Labs" \
     --engine redis \
     --multi-az-enabled \
     --cache-node-type cache.r6g.large \
     --num-cache-clusters 2 \
     --region us-east-1
    

    Please wait about 5 minutes to cluster be available.

  2. Check the cluster status

    aws elasticache describe-replication-groups \
     --replication-group-id cluster-primary \
     --region us-east-1 |\
     jq -r .ReplicationGroups[0].Status
    
  3. Copy environment variables

    # Export variables
    export PRIMARY_ENDPOINT=$(aws elasticache describe-replication-groups --replication-group-id cluster-primary | jq -r '.ReplicationGroups[0].NodeGroups[0].PrimaryEndpoint.Address')
    export PORT_NUMBER=$(aws elasticache describe-replication-groups --replication-group-id cluster-primary | jq -r '.ReplicationGroups[0].NodeGroups[0].PrimaryEndpoint.Port')
    # Show variables values
    echo $PRIMARY_ENDPOINT $PORT_NUMBER
    

Store keys on ElastiCache Cluster

  1. Connect to Cluster

    src/redis-cli -h $PRIMARY_ENDPOINT -p $PORT_NUMBER
    
  2. Test the connection

     PING
    
  3. Write key-value pairs

    With the connection opened perform the REDIS command below:

     SET counter 100
    
     INCR counter
    
     INCRBY counter 10
    
     SET userid 5000 
    
     KEYS * 
    
     GET counter 
    
     GET userid 
    
     QUIT
    

Create a Global Datastore from Regional Cluster

  1. Create a Global Datastore using the primary replication group.

    aws elasticache create-global-replication-group \
     --global-replication-group-id-suffix multi-region \
     --primary-replication-group-id cluster-primary \
     --region us-east-1
    

    Please wait about 5 minutes to cluster be available.

  2. Create new cluster in the Secondary Region N. California and add to Global Datastore

     aws elasticache create-replication-group \
     --replication-group-id cluster-secondary \
     --replication-group-description "DR Workshop Labs" \
     --global-replication-group-id ldgnf-multi-region \
     --multi-az-enabled \
     --num-cache-clusters 2 \
     --region us-west-1
    

    Each suffix identifies one region. The suffix “ldgnf” identifies the region N. Virginia and guarantees uniqueness of the global datastore name across multiple regions. See documentation for more details.

  3. Check if both clusters are with Status “associated”

     aws elasticache describe-global-replication-groups \
      --global-replication-group-id ldgnf-multi-region \
      --show-member-info --region us-east-1 |\
      jq -r .GlobalReplicationGroups[0].Members
    

    Please wait about 10-15 minutes to new cluster be associated to Global Datastore.

Test the Secondary Region cluster

  1. Using the AWS Cloud9 in the secondary region N. California

    # Export variables
    export PRIMARY_ENDPOINT=$(aws elasticache describe-replication-groups --replication-group-id cluster-secondary --region us-west-1 | jq -r '.ReplicationGroups[0].NodeGroups[0].PrimaryEndpoint.Address')
    export PORT_NUMBER=$(aws elasticache describe-replication-groups --replication-group-id cluster-secondary --region us-west-1 | jq -r '.ReplicationGroups[0].NodeGroups[0].PrimaryEndpoint.Port')
    # Show variables values
    echo $PRIMARY_ENDPOINT $PORT_NUMBER
    
    
  2. Connect to Secondary Cluster

    src/redis-cli -h $PRIMARY_ENDPOINT -p $PORT_NUMBER
    
  3. Write key-value pairs

    With the connection opened perform the REDIS command below:

     KEYS *
    
     GET counter
    
     GET userid
    
  4. Try to create a new key-value pair

     SET new-key somevalue
    

    The message: "(error) READONLY You can’t write against a read only replica” will be shown, because it’s not permitted to execute write operation on READONLY instances. This instance ROLE is SLAVE mode.

    You can check the instance operation mode by ROLE attribute using the command below:

     INFO replication
    

Promote Secondary Cluster to Primary

  1. Promote the cluster-secondary in N. California to primary role in the Global Datastore. Using the Cloud9 Terminal execute the command below:

    aws elasticache failover-global-replication-group \
     --global-replication-group-id ldgnf-multi-region \
     --primary-region us-west-1 \
     --primary-replication-group-id cluster-secondary \
    --region us-east-1
    

    The failover operation on Global Datastore has an estimated RTO of less than 1 minute.

  2. Using AWS Cloud9 Terminal in N. California connect to Redis again:

    src/redis-cli -h $PRIMARY_ENDPOINT -p $PORT_NUMBER
    
  3. Now, try to create a new key-value pair again:

     SET new-key somevalue
    

    Now the write operations could be performed on cluster-secondary in N. California. The same command failover-global-replication-group could be used for failback operation*.

Cleaning up

  1. Remove the cluster-primary from Global Datastore. After the failover command on last step, its Role is Secondary in this point.

     aws elasticache disassociate-global-replication-group \
      --global-replication-group-id ldgnf-multi-region \
      --replication-group-id cluster-primary \
      --replication-group-region us-east-1 \
      --region us-east-1
    
  2. Wait until the cluster be disassociated.

  3. Delete cluster-primary.

     aws elasticache delete-replication-group \
      --replication-group-id cluster-primary \
      --no-retain-primary-cluster \
      --region us-east-1
    
  4. Delete Global Datastore.

     aws elasticache delete-global-replication-group \
      --global-replication-group-id ldgnf-multi-region \
      --retain-primary-replication-group \
      --region us-east-1
    
  5. Check Global DataStore status.

     aws elasticache describe-global-replication-groups \
      --show-member-info \
      --region us-east-1
    
  6. Delete cluster-secondary.

     aws elasticache delete-replication-group \
      --replication-group-id cluster-secondary \
      --no-retain-primary-cluster \
      --region us-west-1
    

Conclusion

With this exercise, it was possible to verify how use Global Datastore to replicate data of Amazon ElastiCache for Redis across regions. Global Datastore instances can be used in a strategy of the type Warm Standby, in order to implement disaster recovery capabilities improving the availability.

References: