# Groupcache — Distributed Caching Library for Go by Google > A distributed caching and cache-filling library for Go, designed as a replacement for memcached in many use cases, with automatic peer-to-peer replication. ## Install Save as a script file and run: # Groupcache — Distributed Caching Library for Go by Google ## Quick Use ```bash go get github.com/golang/groupcache # Usage import "github.com/golang/groupcache" pool := groupcache.NewHTTPPool("http://localhost:8080") pool.Set("http://peer1:8080", "http://peer2:8080") group := groupcache.NewGroup("thumbnails", 64<<20, groupcache.GetterFunc( func(ctx context.Context, key string, dest groupcache.Sink) error { // fetch from origin on cache miss data := fetchFromDB(key) dest.SetBytes(data) return nil }, )) ``` ## Introduction Groupcache is a distributed caching library created by Brad Fitzpatrick at Google. It was built as a memcached replacement for certain workloads at dl.google.com, providing automatic replication across peers and a cache-filling mechanism that prevents thundering herd problems. ## What Groupcache Does - Distributes cached data across a set of peer processes using consistent hashing - Fills cache automatically on miss by calling a user-defined getter function - Deduplicates concurrent requests for the same key, preventing thundering herds - Replicates hot keys across peers to reduce cross-peer traffic - Serves as an in-process library with no external daemon to manage ## Architecture Overview Each groupcache instance runs inside your application process. Peers discover each other via an HTTP-based protocol. When a Get request arrives, groupcache checks the local cache first, then uses consistent hashing to determine which peer owns the key. If the key belongs to a remote peer, it fetches over HTTP. The singleflight mechanism ensures only one goroutine fetches a given key at a time, even under concurrent load. ## Self-Hosting & Configuration - Add to your project with `go get github.com/golang/groupcache` - Create an `HTTPPool` with the current node address and set peer URLs - Define named groups with a max cache size and a `GetterFunc` for origin fetches - Peers communicate over HTTP — deploy behind a load balancer or service mesh - Cache eviction uses LRU; set the byte limit per group to control memory usage ## Key Features - Singleflight deduplication prevents duplicate work on concurrent cache misses - Hot cache automatically replicates frequently accessed keys to reduce peer hops - No separate server process — runs embedded in your Go application - Consistent hashing distributes keys evenly and handles peer changes gracefully - Immutable cache design — values are set once and never updated, simplifying consistency ## Comparison with Similar Tools - **Memcached** — external daemon with manual invalidation; groupcache is embedded with automatic fill - **BigCache** — single-node concurrent map; groupcache adds distributed coordination - **Ristretto** — admission-based local cache; groupcache focuses on distributed peer replication - **Redis** — full-featured data store; groupcache is a lighter embedded caching layer ## FAQ **Q: Can I update or delete cached values?** A: Groupcache treats values as immutable. For mutable data, use a versioned key scheme or a different solution. **Q: How do peers discover each other?** A: You provide the list of peer URLs via `pool.Set()`. Integrate with your service discovery system to keep this list current. **Q: Is groupcache still maintained?** A: The library is stable and used in production at Google. It receives maintenance updates but the API is considered complete. **Q: How large can the cache be?** A: Each group has a configurable byte limit. The total memory used is the sum of all group limits on each process. ## Sources - https://github.com/golang/groupcache - https://pkg.go.dev/github.com/golang/groupcache --- Source: https://tokrepo.com/en/workflows/asset-65c960d8 Author: Script Depot