Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this article, you learn how to use an Azure Redis cache with the Go language and connect using Microsoft Entra ID.
- Azure subscription - create one
- Install Go language environment
- Add two imports from Redis to your project and to your development environment
entraid "github.com/redis/go-redis-entraid"
"github.com/redis/go-redis/v9"
First, create a cache. You can create a cache using Azure Managed Redis or Azure Cache for Redis using the Azure portal. In this Quickstart, we use Azure Managed Redis.
When you create the cache, Microsoft Entra ID is enabled by default making it secure from the start. Your cache must also use a public endpoint for this QuickStart.
To create a cache with the portal, follow one of these procedures:
Optionally, you can create a cache using Azure CLI, PowerShell, whichever you prefer.
In the first part of the code sample, set your connection to the cache:
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"time"
entraid "github.com/redis/go-redis-entraid"
"github.com/redis/go-redis/v9"
)
func main() {
ctx := context.Background()
// Set your Redis endpoint (hostname:port) from the cache you created.
redisHost := "<host >:<public port number>"
// Create a credentials provider using DefaultAzureCredential
provider, err := entraid.NewDefaultAzureCredentialsProvider(entraid.DefaultAzureCredentialsProviderOptions{})
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create Redis client with Entra ID authentication
client := redis.NewClient(&redis.Options{
Addr: redisHost,
TLSConfig: &tls.Config{MinVersion: tls.VersionTLS12},
WriteTimeout: 5 * time.Second,
StreamingCredentialsProvider: provider,
})
defer client.Close()
In the next section, test the connection using the Redis command ping
that returns the pong
string.
// Ping the Redis server to test the connection
pong, err := client.Ping(ctx).Result()
if err != nil {
log.Fatal("Failed to connect to Redis:", err)
}
fmt.Println("Ping returned: ", pong)
In this section, use a basic set
and get
sequence to start using the Redis cache in the simplest way to get started.
// Do something with Redis and a key-value pair
result, err := client.Set(ctx, "Message", "Hello, The cache is working with Go!", 0).Result()
if err != nil {
log.Fatal("SET Message failed:", err)
}
fmt.Println("SET Message succeeded:", result)
value, err := client.Get(ctx, "Message").Result()
if err != nil {
if err == redis.Nil {
fmt.Println("GET Message returned: key does not exist")
} else {
log.Fatal("GET Message failed:", err)
}
} else {
fmt.Println("GET Message returned:", value)
}
}
Before you can run this code, you must add yourself as a Redis user.
You must also authorize your connection to Azure from the command line using the Azure command line or Azure developer command line (azd).
You should also add users or a System principal to your cache. Add anyone who might run the program as a user on the Redis cache.
The result looks like this:
Ping returned: PONG
SET Message succeeded: OK
GET Message returned: Hello, The cache is working with Go!
Here, you can see this code sample in its entirety.
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"time"
entraid "github.com/redis/go-redis-entraid"
"github.com/redis/go-redis/v9"
)
func main() {
ctx := context.Background()
// Set your Redis host (hostname:port)
redisHost := "<host >:<public port number>"
// Create a credentials provider using DefaultAzureCredential
provider, err := entraid.NewDefaultAzureCredentialsProvider(entraid.DefaultAzureCredentialsProviderOptions{})
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create Redis client with Entra ID authentication
client := redis.NewClient(&redis.Options{
Addr: redisHost,
TLSConfig: &tls.Config{MinVersion: tls.VersionTLS12},
WriteTimeout: 5 * time.Second,
StreamingCredentialsProvider: provider,
})
defer client.Close()
// Ping the Redis server to test the connection
pong, err := client.Ping(ctx).Result()
if err != nil {
log.Fatal("Failed to connect to Redis:", err)
}
fmt.Println("Ping returned: ", pong)
// Do something with Redis and a key-value pair
result, err := client.Set(ctx, "Message", "Hello, The cache is working with Go!", 0).Result()
if err != nil {
log.Fatal("SET Message failed:", err)
}
fmt.Println("SET Message succeeded:", result)
value, err := client.Get(ctx, "Message").Result()
if err != nil {
if err == redis.Nil {
fmt.Println("GET Message returned: key does not exist")
} else {
log.Fatal("GET Message failed:", err)
}
} else {
fmt.Println("GET Message returned:", value)
}
}
If you want to continue to use the resources you created in this article, keep the resource group.
Otherwise, if you're finished with the resources, you can delete the Azure resource group that you created to avoid charges.
Important
Deleting a resource group is irreversible. When you delete a resource group, all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources inside an existing resource group that contains resources you want to keep, you can delete each resource individually instead of deleting the resource group.
Sign in to the Azure portal, and then select Resource groups.
Select the resource group you want to delete.
If there are many resource groups, use the Filter for any field... box, type the name of your resource group you created for this article. Select the resource group in the results list.
Select Delete resource group.
You're asked to confirm the deletion of the resource group. Type the name of your resource group to confirm, and then select Delete.
After a few moments, the resource group and all of its resources are deleted.