微服務緩存原理與最佳實踐

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"爲什麼需要緩存?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先從一個老生常談的問題開始談起:我們的程序是如何運行起來的?","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"程序存儲在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"disk","attrs":{}}],"attrs":{}},{"type":"text","text":" 中","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"程序是運行在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RAM","attrs":{}}],"attrs":{}},{"type":"text","text":" 之中,也就是我們所說的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"main memory","attrs":{}}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"程序的計算邏輯在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"CPU","attrs":{}}],"attrs":{}},{"type":"text","text":" 中執行","attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來看一個最簡單的例子:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"a = a + 1","attrs":{}}],"attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"load x:","attrs":{}}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"x0 = x0 + 1","attrs":{}}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"load x0 -> RAM","attrs":{}}],"attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3c/3c8ded50cfa9ca2329d11e920c060648.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面提到了3種存儲介質。我們都知道,三類的讀寫速度和成本成反比,所以我們在克服速度問題上需要引入一個 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"中間層","attrs":{}},{"type":"text","text":"。這個中間層,需要高速存取的速度,但是成本可接受。於是乎,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache","attrs":{}}],"attrs":{}},{"type":"text","text":" 被引入","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/97/97bb5a74a529c1b25eb1c150b2db77d5.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而在計算機系統中,有兩種默認緩存:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CPU 裏面的末級緩存,即 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"LLC","attrs":{}}],"attrs":{}},{"type":"text","text":"。緩存內存中的數據","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內存中的高速頁緩存,即 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"page cache","attrs":{}}],"attrs":{}},{"type":"text","text":"。緩存磁盤中的數據","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"緩存讀寫策略","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"引入 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache","attrs":{}}],"attrs":{}},{"type":"text","text":" 之後,我們繼續來看看操作緩存會發生什麼。因爲存在存取速度的差異「而且差異很大」,從而在操作數據時,延遲或程序失敗等都會導致緩存和實際存儲層數據不一致。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們就以標準的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache+DB","attrs":{}}],"attrs":{}},{"type":"text","text":" 來看看經典讀寫策略和應用場景。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Cache Aside","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先來考慮一種最簡單的業務場景,比如用戶表:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"userId:用戶id, phone:用戶電話token,avtoar:用戶頭像url","attrs":{}}],"attrs":{}},{"type":"text","text":",緩存中我們用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"phone","attrs":{}}],"attrs":{}},{"type":"text","text":" 作爲key存儲用戶頭像。當用戶修改頭像url該如何做?","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"更新","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DB","attrs":{}}],"attrs":{}},{"type":"text","text":"數據,再更新","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache","attrs":{}}],"attrs":{}},{"type":"text","text":" 數據","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"更新 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DB","attrs":{}}],"attrs":{}},{"type":"text","text":" 數據,再刪除 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache","attrs":{}}],"attrs":{}},{"type":"text","text":" 數據","attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"變更數據庫","attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"變更緩存","attrs":{}},{"type":"text","text":" 是兩個獨立的操作,而我們並沒有對操作做任何的併發控制。那麼當兩個線程併發更新它們的時候,就會因爲寫入順序的不同造成數據不一致。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以更好的方案是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"2","attrs":{}}],"attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"更新數據時不更新緩存,而是直接刪除緩存","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"後續的請求發現緩存缺失,回去查詢 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DB","attrs":{}}],"attrs":{}},{"type":"text","text":" ,並將結果 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"load cache","attrs":{}}],"attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/00/00e00fa2009ce1a79e2a98d00c9add9b.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個策略就是我們使用緩存最常見的策略:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache Aside","attrs":{}}],"attrs":{}},{"type":"text","text":"。這個策略數據以數據庫中的數據爲準,緩存中的數據是按需加載的,分爲讀策略和寫策略。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是可見的問題也就出現了:頻繁的讀寫操作會導致 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache","attrs":{}}],"attrs":{}},{"type":"text","text":" 反覆地替換,緩存命中率降低。當然如果在業務中對命中率有監控報警時,可以考慮以下方案:","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"更新數據時同時更新緩存,但是在更新緩存前加一個 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"分佈式鎖","attrs":{}},{"type":"text","text":"。這樣同一時間只有一個線程操作緩存,解決了併發問題。同時在後續讀請求中時讀到最新的緩存,解決了不一致的問題。","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"更新數據時同時更新緩存,但是給緩存一個較短的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TTL","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然除了這個策略,在計算機體系還有其他幾種經典的緩存策略,它們也有各自適用的使用場景。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Write Through","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先查詢寫入數據key是否擊中緩存,如果在 -> 更新緩存,同時緩存組件同步數據至DB;不存在,則觸發 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Write Miss","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而一般 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Write Miss","attrs":{}}],"attrs":{}},{"type":"text","text":" 有兩種方式:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Write Allocate","attrs":{}}],"attrs":{}},{"type":"text","text":":寫時直接分配 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache line","attrs":{}}],"attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"No-write allocate","attrs":{}}],"attrs":{}},{"type":"text","text":":寫時不寫入緩存,直接寫入DB,return","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Write Through","attrs":{}}],"attrs":{}},{"type":"text","text":" 中,一般採取 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"No-write allocate","attrs":{}}],"attrs":{}},{"type":"text","text":" 。因爲其實無論哪種,最終數據都會持久化到DB中,省去一步緩存的寫入,提升寫性能。而緩存由 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Read Through","attrs":{}}],"attrs":{}},{"type":"text","text":" 寫入緩存。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b3/b3bb927001d3b3fa392535e479251bf8.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個策略的核心原則:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"用戶只與緩存打交道,由緩存組件和DB通信,寫入或者讀取數據","attrs":{}},{"type":"text","text":"。在一些本地進程緩存組件可以考慮這種策略。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Write Back","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相信你也看出上述方案的缺陷:寫數據時緩存和數據庫同步,但是我們知道這兩塊存儲介質的速度差幾個數量級,對寫入性能是有很大影響。那我們是否異步更新數據庫?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Write back","attrs":{}}],"attrs":{}},{"type":"text","text":" 就是在寫數據時只更新該 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache Line","attrs":{}}],"attrs":{}},{"type":"text","text":" 對應的數據,並把該行標記爲 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Dirty","attrs":{}}],"attrs":{}},{"type":"text","text":"。在讀數據時或是在緩存滿時換出「緩存替換策略」時,將 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Dirty","attrs":{}}],"attrs":{}},{"type":"text","text":" 寫入存儲。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要注意的是:在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Write Miss","attrs":{}}],"attrs":{}},{"type":"text","text":" 情況下,採取的是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Write Allocate","attrs":{}}],"attrs":{}},{"type":"text","text":",即寫入存儲同時寫入緩存,這樣我們在之後的寫請求只需要更新緩存。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e1/e15dedac3c226030fd959b9e5d350606.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"async purge","attrs":{}}],"attrs":{}},{"type":"text","text":" 此類概念其實存在計算機體系中。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Mysql","attrs":{}}],"attrs":{}},{"type":"text","text":" 中刷髒頁,本質都是儘可能防止隨機寫,統一寫磁盤時機。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Redis","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Redis","attrs":{}}],"attrs":{}},{"type":"text","text":"是一個獨立的系統軟件,和我們寫的業務程序是兩個軟件。當我們部署了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Redis","attrs":{}}],"attrs":{}},{"type":"text","text":" 實例後,它只會被動地等待客戶端發送請求,然後再進行處理。所以,如果應用程序想要使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Redis","attrs":{}}],"attrs":{}},{"type":"text","text":" 緩存,我們就要在程序中增加相應的緩存操作代碼。所以我們也把 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Redis","attrs":{}}],"attrs":{}},{"type":"text","text":" 稱爲 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"旁路緩存","attrs":{}},{"type":"text","text":",也就是說:讀取緩存、讀取數據庫和更新緩存的操作都需要在應用程序中來完成。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而作爲緩存的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Redis","attrs":{}}],"attrs":{}},{"type":"text","text":",同樣需要面臨常見的問題:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"緩存的容量終究有限","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上游併發請求衝擊","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"緩存與後端存儲數據一致性","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"替換策略","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般來說,緩存對於選定的被淘汰數據,會根據其是乾淨數據還是髒數據,選擇直接刪除還是寫回數據庫。但是,在 Redis 中,被淘汰數據無論乾淨與否都會被刪除,所以,這是我們在使用 Redis 緩存時要特別注意的:當數據修改成爲髒數據時,需要在數據庫中也把數據修改過來。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以不管替換策略是什麼,髒數據有可能在換入換出中丟失。那我們在產生髒數據就應該刪除緩存,而不是更新緩存,一切數據應該以數據庫爲準。這也很好理解,緩存寫入應該交給讀請求來完成;寫請求儘可能保證數據一致性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至於替換策略有哪些,網上已經有很多文章歸納之間的優劣,這裏就不再贅述。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"ShardCalls","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"併發場景下,可能會有多個線程(協程)同時請求同一份資源,如果每個請求都要走一遍資源的請求過程,除了比較低效之外,還會對資源服務造成併發的壓力。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"go-zero","attrs":{}}],"attrs":{}},{"type":"text","text":" 中的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ShardCalls","attrs":{}}],"attrs":{}},{"type":"text","text":" 可以使得同時多個請求只需要發起一次拿結果的調用,其他請求\"坐享其成\",這種設計有效減少了資源服務的併發壓力,可以有效防止緩存擊穿。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於防止暴增的接口請求對下游服務造成瞬時高負載,可以在你的函數包裹:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"fn = func() (interface{}, error) {\n // 業務查詢\n}\ndata, err = g.Do(apiKey, fn)\n// 就獲得到data,之後的方法或者邏輯就可以使用這個data","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實原理也很簡單:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"func (g *sharedGroup) Do(key string, fn func() (interface{}, error)) (interface{}, error) {\n // done: false,纔會去執行下面的業務邏輯;爲 true,直接返回之前獲取的data\n c, done := g.createCall(key)\n if done {\n return c.val, c.err\n }\n\n // 執行調用者傳入的業務邏輯\n g.makeCall(c, key, fn)\n return c.val, c.err\n}\n\nfunc (g *sharedGroup) createCall(key string) (c *call, done bool) {\n // 只讓一個請求進來進行操作\n g.lock.Lock()\n // 如果攜帶標示一系列請求的key在 calls 這個map中已經存在,\n // 則解鎖並同時等待之前請求獲取數據,返回\n if c, ok := g.calls[key]; ok {\n g.lock.Unlock()\n c.wg.Wait()\n return c, true\n }\n\n // 說明本次請求是首次請求\n c = new(call)\n c.wg.Add(1)\n // 標註請求,因爲持有鎖,不用擔心併發問題\n g.calls[key] = c\n g.lock.Unlock()\n\n return c, false\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這種 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"map+lock","attrs":{}}],"attrs":{}},{"type":"text","text":" 存儲並限制請求操作,和","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/golang/groupcache/tree/master/singleflight","title":null},"content":[{"type":"text","text":"groupcache","attrs":{}}]},{"type":"text","text":"中的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"singleflight","attrs":{}}],"attrs":{}},{"type":"text","text":" 類似,都是防止緩存擊穿的利器","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"源碼地址:","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/tal-tech/go-zero/blob/master/core/syncx/sharedcalls.go#L45","title":null},"content":[{"type":"text","text":"sharedcalls.go","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"緩存和存儲更新順序","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這是開發中常見糾結問題:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"到底是先刪除緩存還是先更新存儲?","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"情況一:先刪除緩存,再更新存儲;","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"A","attrs":{}}],"attrs":{}},{"type":"text","text":" 刪除緩存,更新存儲時網絡延遲","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"B","attrs":{}}],"attrs":{}},{"type":"text","text":" 讀請求,發現緩存缺失,讀存儲 -> 此時讀到舊數據","attrs":{}}]}],"attrs":{}}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣會產生兩個問題:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"B","attrs":{}}],"attrs":{}},{"type":"text","text":" 讀取舊值","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"B","attrs":{}}],"attrs":{}},{"type":"text","text":" 同時讀請求會把舊值寫入緩存,導致後續讀請求讀到舊值","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"既然是緩存可能是舊值,那就不管刪除。有一個並不優雅的解決方案:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"在寫請求更新完存儲值以後,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sleep()","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 一小段時間,再進行一次緩存刪除操作","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"sleep","attrs":{}}],"attrs":{}},{"type":"text","text":" 是爲了確保讀請求結束,寫請求可以刪除讀請求造成的緩存髒數據,當然也要考慮到 redis 主從同步的耗時。不過還是要根據實際業務而定。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個方案會在第一次刪除緩存值後,延遲一段時間再次進行刪除,被稱爲:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"延遲雙刪","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"情況二:先更新數據庫值,再刪除緩存值:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"A","attrs":{}}],"attrs":{}},{"type":"text","text":" 刪除存儲值,但是刪除緩存網絡延遲","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"B","attrs":{}}],"attrs":{}},{"type":"text","text":" 讀請求時,緩存擊中,就直接返回舊值","attrs":{}}]}],"attrs":{}}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這種情況對業務的影響較小,而絕大多數緩存組件都是採取此種更新順序,滿足最終一致性要求。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"情況三:新用戶註冊,直接寫入數據庫,同時緩存中肯定沒有。如果程序此時讀從庫,由於主從延遲,導致讀取不到用戶數據。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這種情況就需要針對 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Insert","attrs":{}}],"attrs":{}},{"type":"text","text":" 這種操作:插入新數據入數據庫同時寫緩存。使得後續讀請求可以直接讀緩存,同時因爲是剛插入的新數據,在一段時間修改的可能性不大。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"以上方案在複雜的情況或多或少都有潛在問題,需要貼合業務做具體的修改","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"如何設計好用的緩存操作層?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面說了這麼多,回到我們開發角度,如果我們需要考慮這麼多問題,顯然太麻煩了。所以如何把這些緩存策略和替換策略封裝起來,簡化開發過程?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"明確幾點:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"將業務邏輯和緩存操作分離,留給開發這一個寫入邏輯的點","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"緩存操作需要考慮流量衝擊,緩存策略等問題。。。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們從讀和寫兩個角度去聊聊 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"go-zero","attrs":{}}],"attrs":{}},{"type":"text","text":"是如何封裝。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"QueryRow","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"// res: query result\n// cacheKey: redis key\nerr := m.QueryRow(&res, cacheKey, func(conn sqlx.SqlConn, v interface{}) error {\n querySQL := `select * from your_table where campus_id = ? and student_id = ?`\n return conn.QueryRow(v, querySQL, campusId, studentId)\n})","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們將開發查詢業務邏輯用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"func(conn sqlx.SqlConn, v interface{})","attrs":{}}],"attrs":{}},{"type":"text","text":" 封裝。用戶無需考慮緩存寫入,只需要傳入需要寫入的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"cacheKey","attrs":{}}],"attrs":{}},{"type":"text","text":"。同時把查詢結果 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"res","attrs":{}}],"attrs":{}},{"type":"text","text":" 返回。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那緩存操作是如何被封裝在內部呢?來看看函數內部:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"func (c cacheNode) QueryRow(v interface{}, key string, query func(conn sqlx.SqlConn, v interface{}) error) error {\n cacheVal := func(v interface{}) error {\n return c.SetCache(key, v)\n }\n // 1. cache hit -> return\n // 2. cache miss -> err\n if err := c.doGetCache(key, v); err != nil {\n // 2.1 err defalut val {*}\n if err == errPlaceholder {\n return c.errNotFound\n } else if err != c.errNotFound {\n return err\n }\n // 2.2 cache miss -> query db\n // 2.2.1 query db return err {NotFound} -> return err defalut val「see 2.1」\n if err = query(c.db, v); err == c.errNotFound {\n if err = c.setCacheWithNotFound(key); err != nil {\n logx.Error(err)\n }\n\n return c.errNotFound\n } else if err != nil {\n c.stat.IncrementDbFails()\n return err\n }\n // 2.3 query db success -> set val to cache\n if err = cacheVal(v); err != nil {\n logx.Error(err)\n return err\n }\n }\n // 1.1 cache hit -> IncrementHit\n c.stat.IncrementHit()\n\n return nil\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/10/10d8401ed6f67347d1f7b8beb45ba685.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從流程上恰好對應緩存策略中的:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Read Through","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"源碼地址:","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/tal-tech/go-zero/blob/master/core/stores/sqlc/cachedsql.go#L75","title":null},"content":[{"type":"text","text":"cachedsql.go","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Exec","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而寫請求,使用的就是之前緩存策略中的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cache Aside","attrs":{}}],"attrs":{}},{"type":"text","text":" -> 先寫數據庫,再刪除緩存。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {\n execSQL := fmt.Sprintf(\"update your_table set %s where 1=1\", m.table, AuthRows)\n return conn.Exec(execSQL, data.RangeId, data.AuthContentId)\n}, keys...)\n\nfunc (cc CachedConn) Exec(exec ExecFn, keys ...string) (sql.Result, error) {\n res, err := exec(cc.db)\n if err != nil {\n return nil, err\n }\n\n if err := cc.DelCache(keys...); err != nil {\n return nil, err\n }\n\n return res, nil\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"QueryRow","attrs":{}}],"attrs":{}},{"type":"text","text":" 一樣,調用者只需要負責業務邏輯,緩存寫入和刪除對調用透明。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"源碼地址:","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/tal-tech/go-zero/blob/master/core/stores/sqlc/cachedsql.go#L58","title":null},"content":[{"type":"text","text":"cachedsql.go","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"線上的緩存","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"開篇第一句話:脫離業務將技術都是耍流氓。以上都是在對緩存模式分析,但是實際業務中緩存是否起到應有的加速作用?最直觀就是緩存擊中率,而如何觀測到服務的緩存擊中?這就涉及到監控。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下圖是我們線上環境的某個服務的緩存記錄情況:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a4/a4fe408d0198f19c15fcebbc6a7afdd5.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還記得上面 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"QueryRow","attrs":{}}],"attrs":{}},{"type":"text","text":" 中:查詢緩存擊中,會調用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"c.stat.IncrementHit()","attrs":{}}],"attrs":{}},{"type":"text","text":"。其中的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"stat","attrs":{}}],"attrs":{}},{"type":"text","text":" 就是作爲監控指標,不斷在計算擊中率和失敗率。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1e/1e4fc3d4d52e0c7c36b870e230003665.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"源碼地址:","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/tal-tech/go-zero/blob/master/core/stores/cache/cachestat.go#L47","title":null},"content":[{"type":"text","text":"cachestat.go","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在其他的業務場景中:比如首頁信息瀏覽業務中,大量請求不可避免。所以緩存首頁的信息在用戶體驗上尤其重要。但是又不像之前提到的一些單一的key,這裏可能涉及大量消息,這個時候就需要其他緩存類型加入:","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"拆分緩存:可以分 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"消息id","attrs":{}}],"attrs":{}},{"type":"text","text":" -> 由 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"消息id","attrs":{}}],"attrs":{}},{"type":"text","text":" 查詢消息,並緩存插入","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"消息list","attrs":{}}],"attrs":{}},{"type":"text","text":"中。","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"消息過期:設置消息過期時間,做到不佔用過長時間緩存。","attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏也就是涉及緩存的最佳實踐:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不允許不過期的緩存「尤爲重要」","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分佈式緩存,易伸縮","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自動生成,自帶統計","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文從緩存的引入,常見緩存讀寫策略,如何保證數據的最終一致性,如何封裝一個好用的緩存操作層,也展示了線上緩存的情況以及監控。所有上面談到的這些緩存細節都可以參考 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"go-zero","attrs":{}}],"attrs":{}},{"type":"text","text":" 源碼實現,見 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"go-zero","attrs":{}}],"attrs":{}},{"type":"text","text":" 源碼的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"core/stores","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"項目地址","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://github.com/tal-tech/go-zero","title":null},"content":[{"type":"text","text":"https://github.com/tal-tech/go-zero","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"歡迎使用 go-zero 並 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"star","attrs":{}},{"type":"text","text":" 鼓勵我們!👏🏻","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章