136 lines
5.7 KiB
HTML
136 lines
5.7 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<link
|
|
rel="stylesheet"
|
|
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.6.1/github-markdown.min.css"
|
|
/>
|
|
<title>haunt98 posts</title>
|
|
</head>
|
|
<style>
|
|
.markdown-body {
|
|
box-sizing: border-box;
|
|
min-width: 200px;
|
|
max-width: 980px;
|
|
margin: 0 auto;
|
|
padding: 45px;
|
|
font-family:
|
|
Shantell Sans Normal,
|
|
Rec Mono Casual,
|
|
SF Pro,
|
|
Inter,
|
|
sans-serif;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.markdown-body pre {
|
|
font-family:
|
|
Berkeley Mono,
|
|
IBM Plex Mono,
|
|
SF Mono,
|
|
Jetbrains Mono,
|
|
monospace;
|
|
}
|
|
|
|
@media (max-width: 767px) {
|
|
.markdown-body {
|
|
padding: 15px;
|
|
}
|
|
}
|
|
</style>
|
|
<body class="markdown-body">
|
|
<h2>
|
|
<a href="index.html"><code>~</code></a>
|
|
</h2>
|
|
<div class="markdown-heading">
|
|
<h1 class="heading-element">Cache shenanigan</h1>
|
|
<a
|
|
id="user-content-cache-shenanigan"
|
|
class="anchor"
|
|
aria-label="Permalink: Cache shenanigan"
|
|
href="#cache-shenanigan"
|
|
><span aria-hidden="true" class="octicon octicon-link"></span
|
|
></a>
|
|
</div>
|
|
<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>
|
|
<div class="highlight highlight-source-mermaid">
|
|
<pre><span class="pl-k">sequenceDiagram</span>
|
|
<span class="pl-k">participant</span> <span class="pl-ent">other</span>
|
|
<span class="pl-k">participant</span> <span class="pl-ent">service</span>
|
|
<span class="pl-k">participant</span> <span class="pl-ent">cache</span>
|
|
<span class="pl-k">participant</span> <span class="pl-ent">db</span>
|
|
|
|
<span class="pl-ent">other </span><span class="pl-k">->></span> <span class="pl-ent">service</span><span class="pl-k">:</span> <span class="pl-s">get data</span>
|
|
<span class="pl-k">activate</span> <span class="pl-ent">service</span>
|
|
<span class="pl-ent">service </span><span class="pl-k">->></span> <span class="pl-ent">cache</span><span class="pl-k">:</span> <span class="pl-s">get data</span>
|
|
<span class="pl-k">alt</span> <span class="pl-s">exist in cache</span>
|
|
<span class="pl-ent">service </span><span class="pl-k">-->></span> <span class="pl-ent">other</span><span class="pl-k">:</span> <span class="pl-s">return data</span>
|
|
<span class="pl-k">else</span> <span class="pl-s">not exist in cache</span>
|
|
<span class="pl-ent">service </span><span class="pl-k">->></span> <span class="pl-ent">db</span><span class="pl-k">:</span> <span class="pl-s">get data</span>
|
|
<span class="pl-k">alt</span> <span class="pl-s">exist data in db</span>
|
|
<span class="pl-ent">service </span><span class="pl-k">-->></span> <span class="pl-ent">other</span><span class="pl-k">:</span> <span class="pl-s">return data</span>
|
|
<span class="pl-k">else</span> <span class="pl-s">not exist data in db</span>
|
|
<span class="pl-ent">service </span><span class="pl-k">-->></span> <span class="pl-ent">other</span><span class="pl-k">:</span> <span class="pl-s">return error not found</span>
|
|
<span class="pl-k">end</span>
|
|
<span class="pl-k">end</span>
|
|
<span class="pl-k">deactivate</span> <span class="pl-ent">service</span>
|
|
|
|
<span class="pl-ent">other </span><span class="pl-k">->></span> <span class="pl-ent">service</span><span class="pl-k">:</span> <span class="pl-s">set data</span>
|
|
<span class="pl-k">activate</span> <span class="pl-ent">service</span>
|
|
<span class="pl-ent">service </span><span class="pl-k">->></span> <span class="pl-ent">db</span><span class="pl-k">:</span> <span class="pl-s">set data</span>
|
|
<span class="pl-ent">service </span><span class="pl-k">->></span> <span class="pl-ent">cache</span><span class="pl-k">:</span> <span class="pl-s">set data</span>
|
|
<span class="pl-k">deactivate</span> <span class="pl-ent">service</span></pre>
|
|
</div>
|
|
<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>
|
|
<div class="markdown-heading">
|
|
<h2 class="heading-element">Thanks</h2>
|
|
<a
|
|
id="user-content-thanks"
|
|
class="anchor"
|
|
aria-label="Permalink: Thanks"
|
|
href="#thanks"
|
|
><span aria-hidden="true" class="octicon octicon-link"></span
|
|
></a>
|
|
</div>
|
|
<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
|
|
<a rel="me" href="https://hachyderm.io/@haunguyen">Mastodon</a>.
|
|
<br />Source code is available on
|
|
<a href="https://github.com/haunt98/posts-go">GitHub</a>
|
|
<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>
|
|
</div>
|
|
</body>
|
|
</html>
|