Redis client for Go (Golang): go-redis and redigo — complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Redis client for Go (Golang): go-redis and redigo



Redis is a high-performance in-memory data store often used as a database, cache, and message broker. There are several popular clients for working with Redis in Go (Golang). In this article, we will take a detailed look at two main libraries: go-redis (the most popular and modern option) and redigo (a lower-level but time-tested library).



We will show how to install each of them, connect to a server, perform basic operations, work with a connection pool, use pub/sub, and pipeline processing. This material will be useful for both beginners and experienced developers who want to integrate Redis into their Go applications.



1. go-redis — a modern and feature-rich client



go-redis is a Redis client library written in Go. It supports all major features of Redis 6 and 7, including clustering, Sentinel, pub/sub, transactions, pipeline, and Lua scripts. The library is actively developed and has a convenient API.



Installing go-redis

To install, run the following command in your terminal:

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


Connection and basic operations

Example of a simple application that connects to Redis, writes, and reads data:

package main

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

func main() { ctx := context.Background()

// Create a client rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password DB: 0, // default database })

// Check connection pong, err := rdb.Ping(ctx).Result() if err != nil { panic(err) } fmt.Println(pong) // PONG

// Write a value err = rdb.Set(ctx, "key", "hello from Go", 0).Err() if err != nil { panic(err) }

// Read a value val, err := rdb.Get(ctx, "key").Result() if err != nil { panic(err) } fmt.Println("key:", val)}


Working with hashes, lists, and sets

go-redis supports all Redis data types. Example of working with a hash:

// Write a hasherr := rdb.HSet(ctx, "user:1000", map[string]interface{}{    "name":  "Alice",    "age":   30,    "city":  "Moscow",}).Err()if err != nil {    panic(err)}

// Read a single fieldname, err := rdb.HGet(ctx, "user:1000", "name").Result()fmt.Println("Name:", name)

// Read the entire hashuser, err := rdb.HGetAll(ctx, "user:1000").Result()fmt.Println("User:", user)


Connection pool and configuration

go-redis automatically manages the connection pool. You can configure its size:

rdb := redis.NewClient(&redis.Options{    Addr:         "localhost:6379",    PoolSize:     10, // maximum number of connections in the pool    MinIdleConns: 3,  // minimum number of idle connections})


Pub/Sub (publisher-subscriber)

Example of subscribing to a channel and publishing a message:

// Subscribesub := rdb.Subscribe(ctx, "mychannel")ch := sub.Channel()

// Listen for messages in a separate goroutinego func() { for msg := range ch { fmt.Printf("Received: %s from %s", msg.Payload, msg.Channel) }}()

// Publisherr := rdb.Publish(ctx, "mychannel", "Hello subscribers!").Err()


Pipeline

Allows sending multiple commands in a single request, significantly speeding up operations:

pipe := rdb.Pipeline()

incr := pipe.Incr(ctx, "counter")pipe.Expire(ctx, "counter", time.Hour)

// Execute all commands_, err := pipe.Exec(ctx)if err != nil { panic(err)}fmt.Println("Counter:", incr.Val())


2. Redig

Recommendations