Sobes.tech
Senior

Your service was an API used by other services. In Redis, you stored data for enrichment — why didn't you call the directory service (about warehouses, etc.) on each request?

sobes.tech AI

Answer from AI

The main reason for not referring to the directory service for each request and instead using Redis to store enrichment data is performance and reduced latency. Requests to external services are usually more time-consuming and resource-intensive, which can create bottlenecks and increase load.

Redis is a fast in-memory cache that allows storing frequently used data (for example, warehouse information) and retrieving it quickly. Thus, when processing API requests, data can be enriched rapidly without making unnecessary network calls.

This reduces the service response time and increases its stability, as data in Redis remains accessible even if the directory service is unavailable.

Example of using Redis in Go:

import (
    "github.com/go-redis/redis/v8"
    "context"
)

ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
    Addr: "localhost:6379",
})

// Getting data from Redis
val, err := rdb.Get(ctx, "warehouse:123").Result()
if err == redis.Nil {
    // Data is missing, you can request the directory and update the cache
} else if err != nil {
    // Handle error
} else {
    // Use val for enrichment
}