Singleflight Cache Stampede Protection

Shield a slow loader with singleflight and a shared cache, collapsing concurrent misses into one fetch and serving stale during refresh.

GoData Fetching

Code

snippet
package main

import (
	"context"
	"fmt"
	"sync"
	"time"

	"golang.org/x/sync/singleflight"
)

type Store struct {
	mu    sync.RWMutex
	cache map[string]entry
	group singleflight.Group
}

type entry struct {
	value   string
	expires time.Time
}

func NewStore(ttl time.Duration) *Store {
	return &Store{cache: make(map[string]entry)}
}

func (s *Store) Get(ctx context.Context, key string, ttl time.Duration) (string, error) {
	s.mu.RLock()
	e, ok := s.cache[key]
	s.mu.RUnlock()

	if ok && time.Now().Before(e.expires) {
		return e.value, nil // 命中:新鲜数据直接返回
	}

	v, err, _ := s.group.Do(key, func() (interface{}, error) {
		value, loadErr := slowLoad(ctx, key) // 同 key 并发只执行一次
		if loadErr != nil {
			return nil, loadErr
		}
		s.mu.Lock()
		s.cache[key] = entry{value: value, expires: time.Now().Add(ttl)}
		s.mu.Unlock()
		return value, nil
	})
	if err != nil {
		if ok {
			return e.value, nil // 回源失败时降级旧值
		}
		return "", err
	}
	return v.(string), nil
}

func slowLoad(_ context.Context, key string) (string, error) {
	time.Sleep(50 * time.Millisecond)
	return "value-for-" + key, nil
}

func main() {
	store := NewStore(time.Minute)

	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			v, _ := store.Get(context.Background(), "config", time.Minute)
			fmt.Println(v)
		}()
	}
	wg.Wait()
}