Golang Redis Client
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: go-redis.
- Redigo
- Go-Redis
The redigo
library is located in the https://github.com/gomodule/redigo
that you must import in your application.
Step 1. Import the redigo
module
go get github.com/gomodule/redigo/redis
import (
"fmt"
"context"
"github.com/gomodule/redigo/redis"
)
Step 2. Create a connection pool
func newPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 80,
MaxActive: 12000,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err.Error())
}
return c, err
},
}
}
Step 3. Write your application code
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
var pool = newPool()
func main() {
client := pool.Get()
defer client.Close()
_, err := client.Do("SET", "mykey", "Hello from redigo!")
if err != nil {
panic(err)
}
value, err := client.Do("GET", "mykey")
if err != nil {
panic(err)
}
fmt.Printf("%s \n", value)
_, err = client.Do("ZADD", "vehicles", 4, "car")
if err != nil {
panic(err)
}
_, err = client.Do("ZADD", "vehicles", 2, "bike")
if err != nil {
panic(err)
}
vehicles, err := client.Do("ZRANGE", "vehicles", 0, -1, "WITHSCORES")
if err != nil {
panic(err)
}
fmt.Printf("%s \n", vehicles)
}
func newPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 80,
MaxActive: 12000,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err.Error())
}
return c, err
},
}
}
Find more information about Golang & Redis connections in the "Redis Connect".
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 redis/redis-stack
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().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.
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**