go-redis-client-library-v8¶
go-redis/redis/v8 is a Go client library for connecting and interacting with a Redis database^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. It is commonly used in Go applications to offload data storage and state management, allowing the application to remain stateless^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Installation¶
To include the library in a Go project, it can be retrieved using go get^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]:
go get github.com/go-redis/redis/v8
It must then be imported into the code^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]:
import (
"github.com/go-redis/redis/v8"
)
Configuration¶
Connecting to a Redis instance typically requires configuration details such as the database address, port, username, and password^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. These values are often managed via environment variables^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
The client also requires a Go context to manage timeouts and deadlines^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
High Availability with Sentinel¶
The library supports high availability configurations using Redis Sentinel. By creating a FailoverClient, the application can automatically handle failovers^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
The setup involves specifying the master name and a list of sentinel addresses^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]:
sentinelAddrs := strings.Split(redis_sentinels, ",")
rdb := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: redis_master,
SentinelAddrs: sentinelAddrs,
Password: redis_password,
DB: 0,
})
Usage Examples¶
Connecting and Pinging¶
Once the client is initialized, it is standard practice to verify the connection using the Ping() method^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Storing Data¶
The library provides methods for interacting with the data store. To store a value, the Set() method is used^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. This is often combined with JSON serialization to store complex data structures^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
// Example: storing a JSON object
videoBytes, err := json.Marshal(video)
err = redisClient.Set(ctx, video.Id, videoBytes, 0).Err()
Retrieving Data¶
To retrieve data, the Get() method is used, which requires the key and a context^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. The returned result can then be unmarshaled from JSON back into a Go struct^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
// Example: retrieving a value
value, err := redisClient.Get(ctx, id).Result()
err = json.Unmarshal([]byte(value), &video)
To retrieve all keys matching a pattern (e.g., "*"), the Keys() method can be used^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Related Concepts¶
- [[Redis]]: The underlying in-memory data structure store used by the client.
- [[Stateless Architecture]]: The architectural benefit of externalizing state to a database like Redis.
Sources¶
^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]