Junior — Middle
How is data storage implemented in the Redis system?
sobes.tech AI
Answer from AI
Redis is an in-memory data store that operates as a key-value data structure. Data in Redis is stored in various structures: strings, lists, sets, hashes, sorted sets, and others. All data is stored in RAM for high-speed access.
For persistent storage, Redis uses snapshot mechanisms (RDB) and command logs (AOF), which periodically save the database state to disk.
In Go, client libraries such as go-redis are commonly used to work with Redis. An example of writing and reading a string:
import (
"github.com/go-redis/redis/v8"
"context"
"fmt"
)
func main() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
err := rdb.Set(ctx, "key", "value", 0).Err()
if err != nil {
panic(err)
}
val, err := rdb.Get(ctx, "key").Result()
if err != nil {
panic(err)
}
fmt.Println("key", val)
}
Thus, Redis stores data in memory with the ability to periodically save to disk, ensuring high performance and flexibility in working with different data types.