> For the complete documentation index, see [llms.txt](https://bing.gitbook.io/phper/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bing.gitbook.io/phper/data-base/cache/ru-he-bao-zheng-huan-cun-yu-shu-ju-ku-de-shuang-xie-yi-zhi-xing.md).

# 如何保证缓存与数据库的双写一致性

## 方案

* 低效做法：使用事务，数据库与缓存的更新

​ 符合ACID

* 业务普遍做法：先更新数据库，再删除缓存
* 严谨但复杂的做法：消息队列、订阅binlog

## 旁路缓存原则（**Cache Aside Pattern**）

读的时候，先读缓存，缓存没有的话，就读数据库，然后取出数据后放入缓存，同时返回响应。更新的时候，先更新数据库，然后再删除缓存。

## 参考资料

<https://mp.weixin.qq.com/s/3TjMkXdgWzM-1vVdyqOGfA>
