旁路缓存
按需从数据存储读取数据并填入缓存的模式。
结构与工作原理
按步骤查看箭头所示的流程。
缓存未命中时,从源数据库读取并填充缓存。需要定义 TTL、刷新与失效策略。
预期收益
降低源数据存储的延迟和争用,改善并发请求处理。
权衡
缓存与数据存储双写可能不一致,需要重试与校正逻辑。
不适合要求强一致性、写后立即读很频繁或缓存未命中代价很高的情况。
运维与应用指南
综合说明,与有原文摘录支持的条目分开。实际应用前请先验证。
何时使用
适合读多、重复查询多且能容忍短时数据滞后的商品目录、用户资料或 API 响应。
应用场景
大型电商或供应链平台每天处理 1 亿次读取和 5 万次写入,目标是商品与库存查询 P95 在 100 毫秒内,源数据库负载降低至少 70%。缓存未命中时应用读取数据库,并将数据连同 TTL 写入 Redis;库存变化时使缓存失效。允许价格和库存展示最多延迟 5 分钟,但结账时使用源数据库中的权威数据。
部署原则
- 旁路查询逻辑放在应用服务一侧,共享 Redis 集群放在应用和源数据库之间
- 静态和公开数据放到边缘 CDN,用户或领域数据放到应用缓存
- 源数据库保留权威数据,缓存通过 TTL 与失效策略管理
- Kubernetes 中分离应用 Deployment、Redis StatefulSet 或 Operator 或托管缓存,以及 CloudNativePG 或托管关系数据库
- 热键和大键通过单独缓存层或分片隔离。
本地部署
本地环境用 Redis Sentinel 或 Cluster 构建缓存高可用,隔离应用、缓存和数据库之间的延迟路径及故障域。针对大量未命中调整连接池、超时和断路器,并统一失效、重试和监控流程。
云环境
将 ElastiCache、MemoryDB 或 Azure Cache for Redis 等托管缓存部署在多个可用区,使用 VPC、安全组与 IAM 实施最小权限。即使使用托管服务,也验证客户端连接、重试和超时,并在发布前测试数据库回退与容量保护。
Kubernetes
使用 Deployment、HPA、PDB 和 readiness 探针运行无状态应用。Redis 单独使用 Operator、StatefulSet 或外部托管缓存。使用 ConfigMap 与 Secret 管理连接,通过 NetworkPolicy 限制应用、数据库和缓存间的访问,并以超时、断路和限流保护数据库。
故障应对
典型故障包括缓存节点失效、大量淘汰、热键、缓存击穿、失效遗漏而导致数据陈旧,以及缓存与数据库双写不一致。查看命中率、淘汰、延迟、错误和数据库 QPS。缓存连接失败时超时后按未命中处理,回退至数据库,同时通过限流和断路器保护数据库。必要时清空缓存、暂时缩短 TTL 后逐步预热,并事后分析原因和防复发措施。
14 个维度的证据
反映调研时的证据收录情况,请另行判断来源是否准确、是否仍然有效。
定义与概览有证据
按需从数据存储读取数据并填入缓存的模式。
Load data on demand into a cache from a data store.解决的问题有证据
缓解读密集型场景反复查询相同记录而造成的源数据库 P95 延迟和负载上升。
Primary databases optimized for durability pay the full cost of a disk-based lookup on every request; as traffic grows, P95 latency degrades关键组件与资源有证据
核心组成包括应用、缓存和源数据存储,应用负责访问后两者。
Cache-Aside uses a cache alongside the source data store, with the application managing access to both.数据与资产放置原则有证据
将边缘 CDN 放在读取优化的 Web 层前,然后布置应用的旁路缓存层,后端为数据库。
A read-optimized web tier fronts the database with an edge CDN and application cache-aside layer.代表性使用场景有证据
适用于商品目录、用户资料、API 响应等重复读取频繁的负载。
Read-heavy workloads (product catalogs, user profiles, API responses) hit the same records thousands of times between updates.应用场景示例有证据
示例:供应链与计划平台中为读取优化的 Web 架构。
Design a Kubernetes architecture for a Supply Chain & Planning platform using the Cache-Aside Read-Heavy Web pattern.不适用情形与注意事项有证据
不适合要求强一致性、写后立即读很频繁或缓存未命中代价很高的情况。
You need strong consistency between cache and database本地部署:开发、发布与运维未确认
未记录已确认的原文证据,这不代表模式不适用。
云环境:开发、发布与运维有证据
云端托管缓存的客户端连接配置错误可能导致反复故障,应验证配置、重试与超时。
a small number of client-side misconfigurations in how applications connected to ElastiCache were causing repeated disruptions.Kubernetes:开发、发布与运维有证据
在 Kubernetes 中,可以将旁路缓存应用作为 Deployment,搭配 Redis、数据库及 Ingress 或 CDN 部署。
Design a Kubernetes architecture for a Supply Chain & Planning platform using the Cache-Aside Read-Heavy Web pattern.故障应对有证据
Redis 连接失败时,透明回退到数据库,而不是向用户返回错误。
If the Redis connection fails, the application must transparently fall back to the database instead of returning an error to the user.优势有证据
降低源数据存储的延迟和争用,改善并发请求处理。
Caching reduces the latency and contention that are associated with handling large volumes of concurrent requests in the original data store.不足与权衡有证据
缓存与数据存储双写可能不一致,需要重试与校正逻辑。
Imagine that you’ve successfully updated the cache but the datastore fails to update. The code needs to implement retries.更多参考资料 (23)
- Cache-Aside Pattern - Azure Architecture Center | Microsoft Learn ↗
- Redis cache-aside ↗
- Caching Best Practices | Amazon Web Services ↗
- How to use Redis for Query Caching ↗
- Cache-Aside ↗
- Cache-Aside Pattern (Lazy Loading) - Redis Patterns ↗
- A Hitchhiker's Guide to Caching Patterns ↗
- The Architect's Guide to Common Caching Patterns ↗
- Caching Guidance - Azure Architecture Center ↗
- Deploy Amazon EKS clusters across cloud and on-premises environments - Amazon EKS ↗
- How to Run Kubernetes Across On-Premises and Public Cloud: Four Hybrid Architecture Models and the Seven Layers to Standardize ↗
- Supply Chain & Planning — Cache-Aside Read-Heavy Web (Kubernetes) — Architecture Diagram Template ↗
- Reference Architecture: GKE Enterprise hybrid environment - part 2 - Dec '24 - Google Docs ↗
- Deploy Containerized Apps with AKS Hybrid and Edge by Using GitOps - Azure Architecture Center | Microsoft Learn ↗
- Understanding the Cache-Aside Pattern: A Practical Guide | by Mallikarjuna Mallesha ↗
- Kubernetes Deployment Options for On-Prem Clusters ↗
- How Intuit and AWS systematically improved resiliency on ElastiCache using AWS Fault Injection Service | AWS Database Blog ↗
- When the cache fails: fail-open, fail-closed and the cascade | ShardCraft ↗
- Google SRE - Cascading Failures: Reducing System Outage ↗
- Implementing the Cache-Aside Pattern Correctly ↗
- Cache-Aside | Engineer Atlas ↗
- Design a distributed cache · Stanley Jacob ↗
- Cache Invalidation: TTLs, Write Paths, and the Thundering Herd | Scixa ↗