Go Redis client library¶
The Go Redis client library is the community standard for connecting Go applications to a Redis database.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md] It allows developers to replace local file-based storage with a Redis instance to handle data persistence and state management.1
Installation and Setup¶
To integrate Redis into a Go project, the library must be imported and installed.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md]
go get github.com/go-redis/redis/v8
Within the application code, the library is imported alongside the standard context package, which is required to manage timeouts and deadlines for database operations.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md]
import (
"context"
"github.com/go-redis/redis/v8"
)
Client Configuration¶
Establishing a connection requires configuring the Redis client with specific environment variables, such as the database address, port, and credentials.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md]
A common configuration involves using a Sentinel Client for high availability. This requires splitting the Sentinel addresses (retrieved from an environment variable like REDIS_SENTINELS) into a slice and passing them to NewFailoverClient along with the master name and password.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md]
sentinelAddrs := strings.Split(redis_sentinels, ",")
rdb := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: redis_master,
SentinelAddrs: sentinelAddrs,
Password: redis_password,
DB: 0,
})
It is standard practice to declare the client as a global variable (var redisClient *redis.Client) so it can be accessed throughout the application's handlers.2^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md] The connection can be verified using the Ping() method.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md]
Storing Data¶
To store structured data, such as a video struct, the object is first marshaled into JSON. The Set method is then used to store the byte slice in Redis, using the object's unique ID as the key.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md]
func saveVideo(video video) {
videoBytes, err := json.Marshal(video)
// error handling...
err = redisClient.Set(ctx, video.Id, videoBytes, 0).Err()
// error handling...
}
Retrieving Data¶
Retrieving data involves fetching the value by its key and unmarshaling the JSON back into the original Go struct.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md]
To get a specific item, the Get method is used:
func getVideo(id string) video {
value, err := redisClient.Get(ctx, id).Result()
// error handling...
var video video
err = json.Unmarshal([]byte(value), &video)
return video
}
To retrieve all records, the Keys method can be used with a wildcard pattern (*) to iterate over all keys in the database, fetching and appending the results to a slice.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md]
Networking Considerations¶
When running the Go application in a Docker container, it must share the same network as the Redis containers to communicate successfully.3^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md] This is typically configured using the --net flag in the docker run command.
Related Concepts¶
- [[Docker networking]]
- [[Go]]
- [[Redis]]
- [[JSON]]
Sources¶
400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md
-
While file storage is useful for learning, it poses issues for concurrency and state retention during crashes.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md] ↩
-
This allows the handler functions to ensure connectivity using the global client variable.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md] ↩
-
Without network alignment, the Go container cannot resolve the hostnames of the Redis Sentinels or Master nodes.^[400-devops-09-scripting-language-golang-introduction-part-5databaseredis-readme.md] ↩