From 3ad375308d688c547fb0f645fa82eb1af3b5e627 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Sun, 30 Jul 2023 12:16:18 +0700 Subject: [PATCH] chore: add diagram --- docs/2023-07-30-cache-shenanigan.html | 28 ++++++++++++++++++++++++++ posts/2023-07-30-cache-shenanigan.md | 29 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/docs/2023-07-30-cache-shenanigan.html b/docs/2023-07-30-cache-shenanigan.html index 69c3cd8..985dc73 100644 --- a/docs/2023-07-30-cache-shenanigan.html +++ b/docs/2023-07-30-cache-shenanigan.html @@ -46,6 +46,34 @@ re-write to cache +
+
sequenceDiagram
+    participant other
+    participant service
+    participant cache
+    participant db
+
+    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
+
+    other ->> service: set data
+    activate service
+    service ->> db: set data
+    service ->> cache: set data
+    deactivate service
+

It's good for general cases, for example with CRUD action.

The bad things happen when cache and database are not consistent. For diff --git a/posts/2023-07-30-cache-shenanigan.md b/posts/2023-07-30-cache-shenanigan.md index 3d85b1c..613cdcf 100644 --- a/posts/2023-07-30-cache-shenanigan.md +++ b/posts/2023-07-30-cache-shenanigan.md @@ -7,6 +7,35 @@ My default strategy is: - Write to database first then to cache second - Read from cache first, if not found then read from database second, then re-write to cache +```mermaid +sequenceDiagram + participant other + participant service + participant cache + participant db + + 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 + + other ->> service: set data + activate service + service ->> db: set data + service ->> cache: set data + deactivate service +``` + It's good for general cases, for example with CRUD action. The bad things happen when cache and database are not consistent.