Senior
Postgres is the master data for the directory. A new nomenclature (new product) has appeared in Postgres — should it appear in Redis?
sobes.tech AI
Answer from AI
If Postgres is the source of truth (master data) for the directory, then when a new nomenclature (new product) appears in Postgres, the data should be synchronized with Redis if Redis is used as a cache or for quick access to the directory.
Typically, one of the following approaches is implemented:
- Event-driven synchronization — after adding a product to Postgres, an event is generated, which is processed by a service and updates Redis.
- Periodic synchronization — data from Postgres is exported on a schedule and updates Redis.
- Write-through cache — when writing to Postgres, Redis is updated simultaneously.
An example in Go (simplified):
// After inserting a product into Postgres
func AddProduct(product Product) error {
err := insertIntoPostgres(product)
if err != nil {
return err
}
// Update Redis
err = redisClient.Set(ctx, product.ID, product, 0).Err()
return err
}
Thus, the new nomenclature will appear in Redis, ensuring data relevance.