Redis client for Go (Golang): go-redis and radix
Redis is one of the most popular in-memory databases, used for caching, queues, sessions, and real-time analytics. In the Go (Golang) ecosystem, there are several mature clients for working with Redis. In this article, we will thoroughly examine two main libraries: go-redis (the most popular client) and radix (a more performant and low-level client). You will learn how to install them, connect, perform basic operations, use pipelines, Pub/Sub, and work with a cluster.
1. Why Redis is important for a Go developer
Go is often chosen for building high-load server applications, microservices, and event handlers. Redis complements Go perfectly due to:
- Speed — operations are performed in microseconds.
- Simplicity — a minimalistic API with built-in data structures (strings, lists, sets, hashes).
- Support for Pub/Sub, transactions, and Lua scripts.
The right choice of client determines performance and development convenience.
2. go-redis — the de facto standard
go-redis (repository github.com/redis/go-redis/v9) is the most popular Redis client for Go. It provides a high-level API, support for clusters, sentinels, pipelines, Pub/Sub, and distributed locks.
2.1 Installation
go get github.com/redis/go-redis/v92.2 Connecting to Redis
package main
import ( "context" "fmt" "github.com/redis/go-redis/v9")
func main() { ctx := context.Background()
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}2.3 Basic operations
// Set a stringerr := rdb.Set(ctx, "key", "value", 0).Err()if err != nil { panic(err)}
// Get a stringval, err := rdb.Get(ctx, "key").Result()if err != nil { panic(err)}fmt.Println("key:", val)
// Working with hashesrdb.HSet(ctx, "user:1", "name", "Alice", "age", 30)name, _ := rdb.HGet(ctx, "user:1", "name").Result()fmt.Println("Name:", name)
// Delete a keyrdb.Del(ctx, "key")2.4 Pipelines
Pipelines allow sending multiple commands in a single network call, significantly speeding up batch 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("New counter value:", incr.Val())2.5 Pub/Sub
// Subscribersub := rdb.Subscribe(ctx, "mychannel")ch := sub.Channel()
for msg := range ch { fmt.Printf("Received: %s", msg.Payload)}
// Publisher (in another goroutine)rdb.Publish(ctx, "mychannel", "Hello from Go!")2.6 Working with a cluster
clusterClient := redis.NewClusterClient(&redis.ClusterOptions{ Addrs: []string{ "localhost:7000", "localhost:7001", "localhost:7002", },})
// Used like a regular clienterr := clusterClient.Set(ctx, "cluster_key", "value", 0).Err()if err != nil { panic(err)}3. radix — lightweight and fast
radix (github.com/mediocregopher/radix/v4) is a minimalistic client with a low level of abstraction. It has no dependencies, works faster than go-redis on simple operations, and gives full control over the network layer.
3.1 Installation
go get github.com/mediocregopher/radix/v43
Also in library
PDFKit for JavaScript (Node.js): Complete Guide with Examples
QR Code Generator in Rust: Creation and Integration into Web Applications
Working with Excel in Rust: Libraries for Reading and Writing
ASP.NET Core for C# (.NET): Complete Guide with Examples
Image Processing in C++: CImg, OpenCV, and Magick++ Libraries