Senior
How extensive is your experience working with Redis as a cache and message broker?
sobes.tech AI
Answer from AI
I have extensive experience working with Redis both as a cache and as a message broker.
As a cache, Redis was used to store frequently requested data with a time-to-live (TTL) limit, which significantly reduced database load and sped up service responses.
As a message broker, Redis was employed through data structures like lists (LPUSH/BRPOP) and Pub/Sub to organize asynchronous interactions between services.
Example of using Redis in Go for caching with TTL:
import (
"github.com/go-redis/redis/v8"
"context"
"time"
)
ctx := context.Background()
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
// Set a value with a TTL of 10 minutes
err := client.Set(ctx, "key", "value", 10*time.Minute).Err()
if err != nil {
panic(err)
}
// Get the value
val, err := client.Get(ctx, "key").Result()
if err != nil {
// handle error
}
fmt.Println(val)
This experience allows for effective use of Redis to enhance application performance and reliability.