Design for failure常見的12種設計思想

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通常情況下,我們的一個請求會經過三個服務來處理。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fd/fdea120b05fad643ff5b096123954e82.png","alt":null,"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},"content":[{"type":"text","text":"請 求從客戶端發出,到達Proxy Layer(執行一些公共的邏輯,如邏輯、流控、審計等),完成後,發往App Layer(執行具體業務邏輯),執行完畢後,發向Data Laye(進行數據持久化)。","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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,我們需要:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Design For Failure","attrs":{}},{"type":"text","text":"。即當你的系統將錯誤當作正常流時,系統便已經對錯誤免疫了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在此,跟大家介紹常見的12種設計思想。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1、防禦性設計(Defensive Design)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所謂的防禦性設計實際上就是“防呆”,英文叫Idiot Proofing。說白了就是用戶有時候會不自覺的做一些蠢事,我們在設計的時候要儘量考慮到一些不規範的交互行爲,如果你的用戶是一隻猴子,你要寫包單保證系統不被玩壞。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如,在Android開發中使用到的Monkey Test就是用於這樣的目的。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2、邊界情況(Edge Case)","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":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3、防誤措施(Mistake Proofing)","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":"4、解耦(Decoupling)","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":"Spring的IOC就是爲了把對象創建及維護從原來的由引用類負責這種強耦合模式轉成通過spring容器負責。且解耦一般的做法是通過把內部邏輯封裝起來,暴露對外統一API接口,調用方不需要了解被調用方的內部邏輯實現,只需要知道提供什麼功能即可。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"再引申一下,解耦的作用就在於複用,把所有的高內聚功能獨立成一個個模塊,然後就可以像樂高積木一樣根據調用方的實際需求進行組裝。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5、冗餘(Redundancy)","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":"6、重試(Retry)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"重試是在分佈式系統下處理瞬態故障的一個基本手段,簡單有效(當然重試的前提是要求冪等)。但是重試也是可以很危險的,它能夠引起把一個局部小時間迅速升級爲一個系統重大故障,嚴重者導致系統假死。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/71/714e105d7d0aabd47c00b30dd5987a10.png","alt":null,"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},"content":[{"type":"text","text":"舉個簡單例子:如果我們的鏈路類似上圖,這裏會發生什麼問題?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在極端情況下,重試次數達到5*5*5*5=625次。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當鏈路中的其中一個服務故障率異常的時候,那重試風暴便開啓了,因爲重試爲服務器帶來額外的開銷和線程的佔用,然後其他新來的請求又形成排隊,這樣的話就形成了類似的DDos惡性事件。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"7、冷備(Cold Standby)","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":"8、熔斷(Derating)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"熔斷本質上就是一種防禦性設計或者策略。假設一個微服務體系下的系統,其中A服務調用B服務。系統的QPS是千級別,當時如果B服務掛掉的話A的線程絕對在短時間內佔滿耗盡而導致假死,從而形成大量A請求積壓而導致情況惡化,最終形成雪崩。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"9、容錯(Error Tolerance)","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":"廣義的容錯應該是兩個具有明確邊界的事物(如服務間,系統間)交互時候針對可能發生的一切主客觀異常情況的防禦性手段。常見的容錯機制有failsafe、failback、failover、failfast。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"failfast 更多指的是快速失敗,避免線程積壓導致系統滾雪球式崩潰。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"failover 指的是失效轉移。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"failsafe 指的是失效安全。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"failback 指的是失效自動恢復,將故障實例切換到備實例。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"10、失效安全(Fail safe)","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":"例如:我們使用token進行安全登錄也是一種失效安全的體現,如果token失效了(如時間過期),用戶是無法登錄的,因爲正常登錄需要token有一種約束因素,這種因素就是時間。如果時間過了,代表這種約束因素不存在或者不再有效了,登錄功能就不能正常工作了。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"11、優雅降級(Graceful Degradation)","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":"在微服務框架中,一般會使用Hystrix的@HystrixCommand或Feign的@FeignClient對服務進行聲明,然後爲每個服務配置相應的fallback類,最終結合起來進行服務降級。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"12、耐用性(Durability)","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}},{"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":"作爲一名designer或者developer,應該要對墨菲定律心存敬畏。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另外,需要額外補充一點的就是:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"監控(Monitoring)","attrs":{}},{"type":"text","text":"。","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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"horizontalrule","attrs":{}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者:架構精進之路,十年研發風雨路,大廠架構師,CSDN 博客專家,專注架構技術沉澱學習及分享,職業與認知升級,堅持分享接地氣兒的乾貨文章,期待與你一起成長","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關注並私信我回復“01”,送你一份程序員成長進階大禮包,歡迎勾搭。","attrs":{}}]}],"attrs":{}},{"type":"horizontalrule","attrs":{}},{"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":"Thanks for reading!","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章