Cache-aside

Cache-aside is a caching pattern in which an application checks the cache before loading data from its primary database. On a cache miss, the application loads the data, stores it with a TTL and returns it; write paths delete the affected cached key.

Minimal example

go
data, err := rdb.Get(ctx, key).Bytes()
if err == redis.Nil {
	data, err = load(ctx)
	if err == nil {
		_ = rdb.Set(ctx, key, data, ttl).Err()
	}
}

Learn this concept

Lesson 6.1 · Cache-aside with jitter and singleflight

Used in exercises

Exercise 6.2 · Menu cache and stock updater Exercise 6.17 · XFetch cache refresh decider