-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
title: "Cache Aside" | ||
date: 2024-11-29 | ||
|
||
categories: [learning note] | ||
tags: ["Cache"] | ||
keywords: ["Cache"] | ||
|
||
description: "Cache Aside" | ||
--- | ||
|
||
## Question & Answer | ||
1. 什么是 cache aside 模式? | ||
Cache aside 可能是最常用的缓存方法。此策略规定缓存必须位于一侧,并且应用程序将直接与缓存和数据库通信。Cache aside通常是通用的,最适合读取密集型工作负载。 | ||
流程 | ||
```text | ||
查询策略 | ||
+---------------------+ | ||
| 查询缓存数据 | | ||
+---------------------+ | ||
| | ||
v | ||
+---------------------+ | ||
| 缓存命中?(是/否) | | ||
+---------------------+ | ||
| | | ||
|是 |否 | ||
v v | ||
+---------+ +---------------------+ | ||
| 返回数据 | | 查询数据库 | | ||
+---------+ +---------------------+ | ||
| | ||
v | ||
+---------------------+ | ||
| 将数据写入缓存 | | ||
+---------------------+ | ||
| | ||
v | ||
+---------------------+ | ||
| 返回数据 | | ||
+---------------------+ | ||
更新策略 | ||
+---------------------+ | ||
| 数据更新请求 | | ||
+---------------------+ | ||
| | ||
v | ||
+---------------------+ | ||
| 更新数据库 | | ||
+---------------------+ | ||
| | ||
v | ||
+---------------------+ | ||
| 删除缓存或使之失效 | | ||
+---------------------+ | ||
``` | ||
|
||
|
||
|
||
2. 使用 cache aside 模式需要注意的问题 | ||
- 此策略的缺点:在缓存未命中后需要三次网络往返。 | ||
- 数据一致性,数据更新时,可能出现缓存和数据库不一致的短暂情况。 | ||
- 如果缓存容量有限,可能会因缓存驱逐策略(如 LRU)导致数据被提前移除。 | ||
- 缓存击穿,如果采用删除缓存的方案,在高并发场景下可能会导致缓存击穿。 | ||
- 缓存雪崩,大量缓存同时失效时,可能导致大量请求直接打到数据库。 | ||
- 缓存穿透,当查询的 key 在数据库中不存在,且未缓存时,可能导致对数据库的重复查询。 | ||
|
||
|
||
## 参考 | ||
[数据库缓存策略](https://dfordebugging.wordpress.com/2022/08/17/database-caching-strategies/) | ||
[聊聊数据库与缓存数据一致性问题](https://juejin.cn/post/6844903941646319623) |