Sobes.tech
Senior

Tell me how Redis works, what it is?

sobes.tech AI

Answer from AI

Redis is a high-performance in-memory data store supporting various data structures (strings, lists, sets, hashes, sorted sets, etc.).

Main features:

  • In-memory: data is stored in RAM, ensuring very fast operations.
  • Persistence support: data can be saved to disk for recovery after restart.
  • Variety of data structures: allows efficient solving of different tasks (caching, queues, counters).
  • Single-threaded server: Redis processes commands sequentially, simplifying the concurrency model and increasing performance.
  • Pub/Sub and transactions: supports publish/subscribe mechanisms and atomic operations.

Example of using Redis in Go:

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 is a fast in-memory datastore with rich functionality, often used for caching, sessions, queues, and other tasks.