Skip to main content

Profile picture for Ajeet Raina
Author:
Ajeet Raina, was Developer Growth Manager at Redis

Go and Redis

Find tutorials, examples and technical articles that will help you to develop with Redis and Golang.

Getting Started

Golang community has built many client libraries that you can find here. For your first steps with Golang and Redis, this article will show how to use the recommended library: redigo.

Go-redis is a type-safe, Redis client library for Go with support for features like Pub/Sub, sentinel, and pipelining.It is a Redis client able to support a Redis cluster and is designed to store and update slot info automatically with a cluster change. Below are the attractive features of Go-redis:

  • Go-redis has pooling capabilities.(Pools allow you to safely handle go-routines, auto reconnect if any error occurs)
  • It supports both standard, OSS cluster AIP, and Sentinel
  • Comes with Auto reconnects / Auto-rediscovers cluster slots on error/migration
  • Support instrumentations
  • Allows for a custom dialer (this is useful for Enterprise)
  • Support for Redis Sentinel

The go-redis library is located in the https://github.com/go-redis/redis that you must import in your application. Do check Redis Cache Library for Golang

Step 1. Run a Redis server

Redis is an open source, in-memory, key-value data store most commonly used as a primary database, cache, message broker, and queue. Redis delivers sub-millisecond response times, enabling fast and powerful real-time applications in industries such as gaming, fintech, ad-tech, social media, healthcare, and IoT. You can run a Redis database directly over your local mac os or in a container. If you have Docker installed in your sytem, type the following command:

 docker run -d -p 6379:6379 redislabs/redismod

You can connect to Redis server using the redis-cli command like this:

 redis-cli

The above command will make a connection to the Redis server. It will then present a prompt that allows you to run Redis commands.

Step 2. Initialise the Go Module

In order to connect to the Redis instance and return some data value, first you need to initialize the Go module as shown:

 go mod init github.com/my/repo

Step 3. Install redis/v8

 go get github.com/go-redis/redis/v8

Step 4. Create a main.go file

Let us create a main.go file and write the following code to check for your Redis instance connection

 package main

import (
"fmt"
"github.com/go-redis/redis"
)

func main() {
fmt.Println("Testing Golang Redis")

client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})

pong, err := client.Ping(client.Context()).Result()
fmt.Println(pong, err)

}

Step 5. Begin the compilation

 go run main.go

By now, the Go application should successfully connect to the Redis instance and return data value (a successful "PONG" response).

Redis Launchpad

Redis Launchpad is like an “App Store” for Redis sample apps. You can easily find apps for your preferred frameworks and languages. Check out a few of these apps below, or click here to access the complete list.

Rate-Limiting app in Go

launchpad

Rate Limiting app built in Go

Leaderboard app in Go

launchpad

How to implement leaderboard app in Go

Technical Articles & Whitepapers

Redis and Golang: Designed to Improve Performance Redisqueue - A producer and consumer of a queue that uses Redis streamsA High Performance Recommendation Engine with Redis and Go**