GeeCache是一个高性能的分布式缓存系统,支持一致性哈希、服务发现、数据压缩等特性。
-
缓存核心 (geecache)
- Group: 缓存命名空间
- lru: LRU缓存淘汰算法实现
- cache: 并发安全的缓存
- singleflight: 防止缓存击穿的并发控制组件
- consistenthash: 一致性哈希实现,确保分布式环境下的负载均衡
-
通信和协议 (geecache/geecachepb)
- Protocol Buffers定义的缓存数据通信协议
- HTTP: 基于HTTP的节点间通信
-
服务发现 (geecache/registry)
- 基于etcd的服务注册与发现机制
- 支持自动注册服务和发现其他节点
-
压缩模块 (geecache/compression)
- 支持多种压缩算法:Gzip、Snappy、LZ4、Zstd
- 提供统一的压缩接口,可根据性能/压缩率需求选择算法
-
监控指标 (geecache/metrics)
- 支持Prometheus接口
- 收集请求延迟、缓存命中率等关键指标
-
存储接口 (geecache/storage)
- 抽象存储层,可扩展不同的后端存储
┌─────────┐ ┌─────────┐
│ Client │ │ Client │
└─────────┘ └─────────┘
│ │
▼ ▼
┌──────────────────────────────────────────────────────┐
│ HTTP │
├──────────────────────────────────────────────────────┤
│ │
│ ┌─────────┐ ┌─────────────┐ ┌──────────────┐ │
│ │ Group │───▶│SingleFlight │───▶│ Remote │ │
│ └─────────┘ └─────────────┘ │ Nodes │ │
│ │ └──────────────┘ │
│ │ ▲ │
│ ▼ │ │
│ ┌─────────┐ ┌──────────────┐ │
│ │ Cache │ │ Consistent │ │
│ └─────────┘ │ Hash │ │
│ │ └──────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────┐ │
│ │ LRU │ │
│ └─────────┘ │
│ GeeCache │
└──────────────────────────────────────────────────────┘
│ │
▼ ▼
┌─────────────┐ ┌────────────┐
│ etcd │ │ Prometheus │
│ (服务发现) │ │ (监控) │
└─────────────┘ └────────────┘
- 一致性哈希:使用一致性哈希算法进行缓存数据的分布,确保数据均匀分布并减少节点变化时的缓存迁移
- 防止缓存击穿:使用singleflight确保对同一个key的多次请求只会触发一次底层查询
- 服务发现:集成etcd实现服务的自动注册与发现,支持动态伸缩
- 数据压缩:支持多种压缩算法,优化网络传输和存储效率
- 监控指标:集成Prometheus监控系统,实时监控缓存性能
- 可扩展性:良好的接口设计,可轻松扩展不同的后端存储和功能
- Go 1.13+
- etcd (服务发现需要)
-
启动etcd
etcd
-
启动缓存节点
# 启动节点1(端口8001) go run main.go -port=8001 -etcd-endpoints=localhost:2379 # 启动节点2(端口8002) go run main.go -port=8002 -etcd-endpoints=localhost:2379 # 启动API节点(端口9999) go run main.go -port=8003 -api=true -etcd-endpoints=localhost:2379
-
使用自动化脚本启动
# Linux/MacOS chmod +x tests/start_servers.sh ./tests/start_servers.sh # Windows ./tests/start_servers.ps1
-
运行性能测试
cd tests/perf go run main.go
-
测试API
# 查询缓存 curl "http://localhost:9999/api?key=Tom"
通过tests/perf
目录下的测试工具,可以测试缓存系统的性能指标:
- 压缩算法性能对比
- 本地缓存性能(吞吐量、响应时间)
- 分布式缓存性能(吞吐量、成功率)
测试结果会保存在tests/perf/performance_test.log
文件中。
geecache/
├── byteview.go # 缓存值的不可变视图
├── cache.go # 并发安全的缓存
├── compression/ # 压缩模块
│ ├── compression.go # 压缩接口
│ ├── gzip.go # Gzip压缩实现
│ ├── lz4.go # LZ4压缩实现
│ ├── none.go # 无压缩实现
│ ├── snappy.go # Snappy压缩实现
│ └── zstd.go # Zstd压缩实现
├── consistenthash/ # 一致性哈希
├── geecache.go # 核心缓存逻辑
├── geecachepb/ # 协议定义
├── go.mod # 模块定义
├── http.go # HTTP通信
├── http_pool_discovery.go # 服务发现增强的HTTP节点池
├── lru/ # LRU缓存实现
├── metrics/ # 监控指标
│ └── prometheus.go # Prometheus客户端
├── peers.go # 节点接口
├── registry/ # 服务注册与发现
│ ├── discovery.go # 服务发现
│ ├── etcd.go # etcd实现
│ └── registry.go # 服务注册
├── singleflight/ # 防缓存击穿实现
└── storage/ # 存储接口
-
实现新的压缩算法
// 实现Compressor接口 type MyCompressor struct {} func (c *MyCompressor) Compress(data []byte) ([]byte, error) { // 实现压缩逻辑 } func (c *MyCompressor) Decompress(data []byte) ([]byte, error) { // 实现解压逻辑 }
-
添加新存储后端
// 实现Storage接口 type MyStorage struct {} func (s *MyStorage) Get(key string) ([]byte, error) { // 实现获取逻辑 } func (s *MyStorage) Set(key string, value []byte) error { // 实现设置逻辑 }
如果遇到服务发现问题,可以通过etcd命令行检查服务注册情况:
etcdctl get --prefix /geecache/registry/