golang.org/x/sync/singleflight is one of those packages I reach for maybe twice a year, and each time I wonder why it is not in the standard library. The situation: a cache miss for a hot key triggers 400 identical upstream calls in the same second. Singleflight lets exactly one of them run; the other 399 block on the shared result.

The API is a single Do call keyed by a string. Use the key the cache uses. The function returns (value, error, shared bool), where shared is true if your caller piggybacked on someone else’s work.

var g singleflight.Group

func GetUser(ctx context.Context, id string) (*User, error) {
	if u, ok := cache.Get(id); ok {
		return u, nil
	}
	v, err, _ := g.Do("user:"+id, func() (any, error) {
		u, err := fetchUserFromDB(ctx, id)
		if err != nil {
			return nil, err
		}
		cache.Set(id, u, 30*time.Second)
		return u, nil
	})
	if err != nil {
		return nil, err
	}
	return v.(*User), nil
}

Caveat: Do returns the same error to every waiter. If the first caller had a transient context error, you might want g.Forget(key) on error so the next caller actually retries. See also /posts/context-context-is-not-a-cache/.