2023-07-30 04:42:20 +00:00
|
|
|
<!doctype html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
|
|
<link
|
|
|
|
rel="stylesheet"
|
2023-08-06 07:41:38 +00:00
|
|
|
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown.min.css"
|
2023-07-30 04:42:20 +00:00
|
|
|
/>
|
|
|
|
<title>haunt98 posts</title>
|
|
|
|
</head>
|
|
|
|
<style>
|
|
|
|
.markdown-body {
|
|
|
|
box-sizing: border-box;
|
|
|
|
min-width: 200px;
|
|
|
|
max-width: 980px;
|
|
|
|
margin: 0 auto;
|
|
|
|
padding: 45px;
|
|
|
|
}
|
|
|
|
|
|
|
|
@media (max-width: 767px) {
|
|
|
|
.markdown-body {
|
|
|
|
padding: 15px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<body class="markdown-body">
|
2023-08-09 07:22:58 +00:00
|
|
|
<h2>
|
|
|
|
<a href="index.html"><code>~</code></a>
|
|
|
|
</h2>
|
2023-09-22 16:40:26 +00:00
|
|
|
<h1 id="user-content-cache-shenanigan">
|
|
|
|
<a class="heading-link" href="#cache-shenanigan"
|
|
|
|
>Cache shenanigan<span
|
|
|
|
aria-hidden="true"
|
|
|
|
class="octicon octicon-link"
|
|
|
|
></span
|
|
|
|
></a>
|
2023-07-30 04:42:20 +00:00
|
|
|
</h1>
|
|
|
|
<p>
|
|
|
|
My notes/mistakes/... when using cache (mainly Redis) from time to time
|
|
|
|
</p>
|
|
|
|
<p>My default strategy is:</p>
|
|
|
|
<ul>
|
|
|
|
<li>Write to database first then to cache second</li>
|
|
|
|
<li>
|
|
|
|
Read from cache first, if not found then read from database second, then
|
|
|
|
re-write to cache
|
|
|
|
</li>
|
|
|
|
</ul>
|
2023-09-23 05:34:27 +00:00
|
|
|
<pre lang="mermaid"><code>sequenceDiagram
|
|
|
|
participant other
|
|
|
|
participant service
|
|
|
|
participant cache
|
|
|
|
participant db
|
2023-07-30 05:16:18 +00:00
|
|
|
|
2023-09-23 05:34:27 +00:00
|
|
|
other ->> service: get data
|
|
|
|
activate service
|
|
|
|
service ->> cache: get data
|
|
|
|
alt exist in cache
|
|
|
|
service -->> other: return data
|
|
|
|
else not exist in cache
|
|
|
|
service ->> db: get data
|
|
|
|
alt exist data in db
|
|
|
|
service -->> other: return data
|
|
|
|
else not exist data in db
|
|
|
|
service -->> other: return error not found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
deactivate service
|
2023-07-30 05:16:18 +00:00
|
|
|
|
2023-09-23 05:34:27 +00:00
|
|
|
other ->> service: set data
|
|
|
|
activate service
|
|
|
|
service ->> db: set data
|
|
|
|
service ->> cache: set data
|
|
|
|
deactivate service
|
|
|
|
</code></pre>
|
2023-07-30 04:42:20 +00:00
|
|
|
<p>It's good for general cases, for example with CRUD action.</p>
|
|
|
|
<p>
|
|
|
|
The bad things happen when cache and database are not consistent. For
|
|
|
|
example what happen if writing database OK then writing cache failed? Now
|
|
|
|
database has new value, but cache has old value Then when we read again,
|
|
|
|
we read cache first with old value, and that is disaster.
|
|
|
|
</p>
|
2023-09-22 16:40:26 +00:00
|
|
|
<h2 id="user-content-thanks">
|
|
|
|
<a class="heading-link" href="#thanks"
|
|
|
|
>Thanks<span aria-hidden="true" class="octicon octicon-link"></span
|
|
|
|
></a>
|
2023-07-30 04:42:20 +00:00
|
|
|
</h2>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<a
|
|
|
|
href="https://danielw.cn/cache-consistency-with-database"
|
|
|
|
rel="nofollow"
|
|
|
|
>Cache Consistency with Database</a
|
|
|
|
>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
Feel free to ask me via
|
|
|
|
<a href="mailto:hauvipapro+posts@gmail.com">email</a> or
|
2023-08-20 17:29:13 +00:00
|
|
|
<a rel="me" href="https://hachyderm.io/@haunguyen">Mastodon</a>.
|
|
|
|
<br />Source code is available on
|
2023-07-30 04:42:20 +00:00
|
|
|
<a href="https://github.com/haunt98/posts-go">GitHub</a>
|
2023-08-20 17:29:13 +00:00
|
|
|
<a href="https://codeberg.org/yoshie/posts-go">Codeberg</a>
|
|
|
|
<a href="https://git.sr.ht/~youngyoshie/posts-go">sourcehut</a>
|
|
|
|
<a href="https://gitea.treehouse.systems/yoshie/posts-go">Treehouse</a>
|
|
|
|
<a href="https://gitlab.com/youngyoshie/posts-go">GitLab</a>
|
2023-07-30 04:42:20 +00:00
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|