Azure redis cache creation with replica using terraform

197 Views Asked by At

I'm trying to create Azure Cache for Redis using Terraform.

I need to set a replica in the Redis configuration.

There are 3 terms that I come across in Azure portal:

  • Replica count
  • Shard Count ( Under Redis Cluster section )
  • Geo-Replication

enter image description here

I saw that Terraform supports these 3 parameters:

  • replicas_per_master ( In ARM, replicasPerMaster )
  • replicas_per_primary ( In ARM, replicasPerPrimary )
  • shard_count ( In ARM, shardCount )

Which is the correct terraform configuration corresponding to set Replica count shown in Azure Portal?

ARM template for setting Replica count = 2 in portal, gives replicasPerMaster: 2

So I decided to go with replicas_per_master = 2 in Terraform. Should I set replicas_per_primary also to 2?

What is the difference between these options?

1

There are 1 best solutions below

0
Vinay B On

I tried to provision Azure rdis cache creation with replica using terraform using replicas_per_master and I was able to provision the requirement successfully.

Azure Cache for Redis uses the settings replicas_per_master and replicas_per_primary to set up replication, but they work for different situations in the service. This is how they are different:

  1. replicas_per_master: This setting applies to Azure Cache for Redis instances that do not use clustering. It lets you specify the number of replicas for each master node. This way, the cache can fail over to a replica if the master node fails, which improves the cache's availability.

  2. replicas_per_primary: This setting applies to Azure Cache for Redis instances that have clustering enabled. Clustering partitions the Redis data into multiple shards, each with a primary node and optionally one or more replicas. The replicas_per_primary setting determines the number of replicas for each primary node in a shard.

The number of shards and replicas depends on whether you have clustering enabled or not. With clustering enabled, you can use shard_count to set the number of shards and replicas_per_primary to set the number of replicas for each shard. Without clustering, you only need to use replicas_per_master to set the number of replicas for the master node.

If you are not using clustering, you probably do not need to set replicas_per_primary. This setting is only relevant for clustered configurations. Since you said that setting replicas_per_master to 2 worked for you, this seems to be your case.

The settings for non-clustered and clustered caches are different. For a non-clustered cache, you only need to specify replicas_per_master. For a clustered cache, which requires a Premium SKU, you may need to specify both shard_count and replicas_per_primary. Do not use replicas_per_master for a clustered cache.

My demo terraform configuration:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "demorg-vkd"
  location = "West Europe" # Replace with your actual location
}

# Primary Redis Cache
resource "azurerm_redis_cache" "example_primary" {
  name                = "demovksd-cache-primary"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  capacity            = 1 # Choose based on your needs (P1, P2, P3, etc.)
  family              = "P" # Indicates Premium tier
  sku_name            = "Premium" # Set to Premium SKU
  enable_non_ssl_port = false
  minimum_tls_version = "1.2"

  redis_configuration {
    // No data persistence settings are configured
  }

  replicas_per_master = 2
  
  // Uncomment and set the shard count if clustering is enabled
  // shard_count = <number_of_shards>
}

# Secondary Redis Cache for Geo-Replication
resource "azurerm_redis_cache" "example_secondary" {
  name                = "demovksd-cache-secondary"
  location            = "North Europe" # Choose a different location suitable for geo-replication
  resource_group_name = azurerm_resource_group.example.name
  capacity            = 1 # Should match the primary's capacity
  family              = "P" # Should match the primary
  sku_name            = "Premium" # Should be the same as the primary
  enable_non_ssl_port = false
  minimum_tls_version = "1.2"

  redis_configuration {}

}

Output:

enter image description here

enter image description here

enter image description here