浏览模式 / 服务架构 / B013
cache aside

旁路缓存

按需从数据存储读取数据并填入缓存的模式。

已记录证据 13/14开始比较 →查看来源
01 / UNDERSTAND

结构与工作原理

按步骤查看箭头所示的流程。

缓存未命中时,从源数据库读取并填充缓存。需要定义 TTL、刷新与失效策略。

预期收益

降低源数据存储的延迟和争用,改善并发请求处理。

权衡

缓存与数据存储双写可能不一致,需要重试与校正逻辑。

不适合要求强一致性、写后立即读很频繁或缓存未命中代价很高的情况。

02 / APPLY

运维与应用指南

综合说明,与有原文摘录支持的条目分开。实际应用前请先验证。

何时使用

适合读多、重复查询多且能容忍短时数据滞后的商品目录、用户资料或 API 响应。

应用场景

大型电商或供应链平台每天处理 1 亿次读取和 5 万次写入,目标是商品与库存查询 P95 在 100 毫秒内,源数据库负载降低至少 70%。缓存未命中时应用读取数据库,并将数据连同 TTL 写入 Redis;库存变化时使缓存失效。允许价格和库存展示最多延迟 5 分钟,但结账时使用源数据库中的权威数据。

部署原则

  • 旁路查询逻辑放在应用服务一侧,共享 Redis 集群放在应用和源数据库之间
  • 静态和公开数据放到边缘 CDN,用户或领域数据放到应用缓存
  • 源数据库保留权威数据,缓存通过 TTL 与失效策略管理
  • Kubernetes 中分离应用 Deployment、Redis StatefulSet 或 Operator 或托管缓存,以及 CloudNativePG 或托管关系数据库
  • 热键和大键通过单独缓存层或分片隔离。

本地部署

本地环境用 Redis Sentinel 或 Cluster 构建缓存高可用,隔离应用、缓存和数据库之间的延迟路径及故障域。针对大量未命中调整连接池、超时和断路器,并统一失效、重试和监控流程。

03 / VERIFY

14 个维度的证据

反映调研时的证据收录情况,请另行判断来源是否准确、是否仍然有效。

定义与概览有证据

按需从数据存储读取数据并填入缓存的模式。

Load data on demand into a cache from a data store.

learn.microsoft.com 原文 ↗

解决的问题有证据

缓解读密集型场景反复查询相同记录而造成的源数据库 P95 延迟和负载上升。

Primary databases optimized for durability pay the full cost of a disk-based lookup on every request; as traffic grows, P95 latency degrades

redis.io 原文 ↗

关键组件与资源有证据

核心组成包括应用、缓存和源数据存储,应用负责访问后两者。

Cache-Aside uses a cache alongside the source data store, with the application managing access to both.

www.makonea.com 原文 ↗

数据与资产放置原则有证据

将边缘 CDN 放在读取优化的 Web 层前,然后布置应用的旁路缓存层,后端为数据库。

A read-optimized web tier fronts the database with an edge CDN and application cache-aside layer.

freeaidiagram.com 原文 ↗

代表性使用场景有证据

适用于商品目录、用户资料、API 响应等重复读取频繁的负载。

Read-heavy workloads (product catalogs, user profiles, API responses) hit the same records thousands of times between updates.

redis.io 原文 ↗

应用场景示例有证据

示例:供应链与计划平台中为读取优化的 Web 架构。

Design a Kubernetes architecture for a Supply Chain & Planning platform using the Cache-Aside Read-Heavy Web pattern.

freeaidiagram.com 原文 ↗

不适用情形与注意事项有证据

不适合要求强一致性、写后立即读很频繁或缓存未命中代价很高的情况。

You need strong consistency between cache and database

redis.antirez.com 原文 ↗

本地部署:开发、发布与运维未确认

未记录已确认的原文证据,这不代表模式不适用。

云环境:开发、发布与运维有证据

云端托管缓存的客户端连接配置错误可能导致反复故障,应验证配置、重试与超时。

a small number of client-side misconfigurations in how applications connected to ElastiCache were causing repeated disruptions.

aws.amazon.com 原文 ↗

Kubernetes:开发、发布与运维有证据

在 Kubernetes 中,可以将旁路缓存应用作为 Deployment,搭配 Redis、数据库及 Ingress 或 CDN 部署。

Design a Kubernetes architecture for a Supply Chain & Planning platform using the Cache-Aside Read-Heavy Web pattern.

freeaidiagram.com 原文 ↗

故障应对有证据

Redis 连接失败时,透明回退到数据库,而不是向用户返回错误。

If the Redis connection fails, the application must transparently fall back to the database instead of returning an error to the user.

www.mironsoft.de 原文 ↗

优势有证据

降低源数据存储的延迟和争用,改善并发请求处理。

Caching reduces the latency and contention that are associated with handling large volumes of concurrent requests in the original data store.

learn.microsoft.com 原文 ↗

不足与权衡有证据

缓存与数据存储双写可能不一致,需要重试与校正逻辑。

Imagine that you’ve successfully updated the cache but the datastore fails to update. The code needs to implement retries.

hazelcast.com 原文 ↗

代表性技术与工具有证据

Redis 是代表性的内存缓存。

Redis In-memory cache

freeaidiagram.com 原文 ↗

更多参考资料 (23)