Redis Enterprise and Go

Developing apps using the Go programming language (also known as Golang), and Redis Enterprise is extremely easy. Redis is a popular in-memory database among the Go community, commonly used for caching, session stores, messaging, data ingest, real-time analytics and high speed transactions. The open source community actively supports quite a few client libraries, including Redigo, Radix and go-redis/redis. In addition, RediSearch, the high-speed search engine that comes built in with Redis Enterprise, has a dedicated client library developed and supported by Redis Labs.

Redigo

Redigo offers a comprehensive set of features for any program. These range from connection pooling, data type conversions and pipelining to messaging via publish/subscribe. Its API reference page has detailed information about the functions and data types in the Redigo library, along with a few program examples. The RediSearch Go library is built using Redigo.

Radix

The Radix library follows a minimalistic approach. It provides single purpose, easy-to-get-started packages for most Redis commands, including pipelining, connection pooling, publish/subscribe, clustering and scripting. The Redis package page lists all of these functions and data types. Radix also supports multi-threading inside the Radix pool package.

go-redis/redis

Go-redis/redis is a type-safe library for Golang. The library exposes Redis commands as function calls, thus avoiding the need to parse commands at run-time. It provides APIs to deliver pipelining, transactions, publish/subscribe, pooling with circuit breaker, distributed locks, rate limiter and other features. The library’s API reference page lists all the functions it supports.

Getting Started with Go and Redis Enterprise

If you do not already have Redis Enterprise, you can sign up for a free cloud account and create a new Redis Enterprise database on any of the popular cloud platforms. If you are using Redis Enterprise software, or Redis Enterprise in your VPC, please follow those instructions to create a new database.

The examples shown below use the Redigo client library. Once you have your Redis Enterprise database set up, you can use this sample program to test your connection.

Sample test program, main.rs:

package main

import (
    "fmt"
    "log"
    "github.com/garyburd/redigo/redis"
)

func main() {
    conn, err := redis.Dial("tcp",
        "redis-1111.c11.us-east-1-2.ec2.cloud.redislabs.com:1111",
        redis.DialPassword("password"))

    if err != nil {
        log.Fatal(err)
    }

    defer conn.Close()

    if _, err = conn.Do("SET", "key", "Hello World"); err != nil {
        log.Fatal(err)
    }

    str, err := redis.String(conn.Do("GET","key"))
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(str)
}

How to Get URLs to Connect to Your Database

If you’re connecting to Redis Enterprise Cloud, log in to your Redis Enterprise account, open the dashboard for your database instance, and select the Configuration tab.

Copy the endpoint and password from the screen, and call the redis.Dial function as shown below:

redis.Dial("tcp","<endpoint>:<port>", redis.DialPassword("<password>"))

For example, in the screenshot above, the URL would look like the one here (this assumes “password” as the password):

redis.Dial("tcp",
    "redis-1111.c11.us-east-1-1.ec2.cloud.redislabs.com:1111",
    redis.DialPassword("password"))

You can also authenticate inside the Do function as shown below:

redis.Dial("tcp",
    "redis-1111.c11.us-east-1-1.ec2.cloud.redislabs.com:1111")

if err != nil {
    log.Fatal(err)
}

defer conn.Close()

if _, err = conn.Do("AUTH", "password"); err != nil {
    log.Fatal(err)
}

How to Use an SSL Connection with Redis Enterprise

You can secure your connection between your app and the Redis Enterprise server by enabling SSL for your database endpoint. Once you enable SSL, your Redis Enterprise database stops accepting non-SSL connections. With the help of Go’s crypto/tls and crypto/x509 packages, you can establish an SSL connection from your app to the server, as shown below:

conn, err := redis.Dial("tcp","<endpoint>:<port>",
    redis.DialPassword("<password>"),
    redis.DialTLSConfig(&tlsConfig),
    redis.DialTLSSkipVerify(true),
    redis.DialUseTLS(true))

Here tlsConfig is of type tls.Config. Depending on how you manage your certificates, you can use one of the examples to initialize tlsConfig.

Visit the clients page on redislabs.com to explore other popular client libraries that work with Redis Enterprise.