Scripts2026年7月18日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Groupcache Overview
直接安装命令
npx -y tokrepo@latest install 65c960d8-8242-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产