From 1eb0c04c6e4b882fca5344b7dabff5db59d8f33f Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Thu, 25 Jan 2024 01:25:03 +0700 Subject: [PATCH] feat: more draft --- docs/2024-01-20-backend-thinking.html | 102 +++++++++++++++++++++----- posts/2024-01-20-backend-thinking.md | 60 +++++++++++++-- 2 files changed, 138 insertions(+), 24 deletions(-) diff --git a/docs/2024-01-20-backend-thinking.html b/docs/2024-01-20-backend-thinking.html index f260b5e..98c2b40 100644 --- a/docs/2024-01-20-backend-thinking.html +++ b/docs/2024-01-20-backend-thinking.html @@ -453,34 +453,99 @@

System Design + >System Design Concept

-

+

Start with basic: getting data from database.

+
+
sequenceDiagram
+    participant service
+    participant database
+    
+    service ->> database: get (100ms)
+
+

Getting data from cache first, then database later.

+
+
sequenceDiagram
+    participant service
+    participant cache
+    participant database
+    
+    service ->> cache: get (5ms)
+    alt not exist in cache
+        service ->> database: get (100ms)
+    end
+
+

+ If data is already in cache, we can get it so fast (5ms), nearly instant. + But if not, we hit penalty, must get database after then re-update cache + if need (>105ms). The best case is worth even if hitting penalty + sometimes. +

+

Basic cache strategy: combine Write Through and Read Through

+
+
sequenceDiagram
+    participant service
+    participant cache
+    participant database
+    
+    note over service,database: Read Through
+    service ->> cache: get
+    alt not exist in cache
+        service ->> database: get
+        service ->> cache: set
+    end
+
+    note over service,database: Write Through
+    service ->> database: set
+    service ->> cache: set
+
+

Performance -

-

- Security -

+ >References + +

Damage control

-

TODO: Take care incident

> database: get (100ms) +``` + +Getting data from cache first, then database later. + +```mermaid +sequenceDiagram + participant service + participant cache + participant database + + service ->> cache: get (5ms) + alt not exist in cache + service ->> database: get (100ms) + end +``` + +If data is already in cache, we can get it so fast (5ms), nearly instant. But if +not, we hit penalty, must get database after then re-update cache if need +(>105ms). The best case is worth even if hitting penalty sometimes. + +Basic cache strategy: combine Write Through and Read Through + +```mermaid +sequenceDiagram + participant service + participant cache + participant database + + note over service,database: Read Through + service ->> cache: get + alt not exist in cache + service ->> database: get + service ->> cache: set + end + + note over service,database: Write Through + service ->> database: set + service ->> cache: set +``` + +### References + +- [Systems design explains the world: volume 1](https://apenwarr.ca/log/20201227) +- [Systems design 2: What we hope we know](https://apenwarr.ca/log/20230415) +- [How to do distributed locking](https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html) +- [Is Redlock safe?](http://antirez.com/news/101) +- [Cache Consistency with Database](https://danielw.cn/cache-consistency-with-database#cache-patterns) ## Damage control -TODO: Take care incident - ## Bonus - [IntelliJ IDEA Ultimate](https://www.jetbrains.com/idea/download/): Java