博客 / 詳情

返回

軟件架構中“彈性”的多種含義

在軟件架構領域的中文文檔、書籍中,經常可以看到“彈性”這個專業術語,但在不同的語境下含義可能會不同。

在英語中,elastic 和 resilient 兩個單詞都可以翻譯為“彈性的”,但是它們在軟件架構中代表的含義卻完全不同,需要避免混淆。

Elastic

Elastic 代表的“彈性”強調的是可伸縮性

在《Designing Data-Intensive Applications》一書中,對 elastic 的定義:

Some systems are elastic, meaning that they can automatically add computing resources when they detect a load increase, whereas other systems are scaled manually (a human analyzes the capacity and decides to add more machines to the system). An elastic system can be useful if load is highly unpredictable, but manually scaled systems are simpler and may have fewer operational surprises.

翻譯:某些系統具有彈性,意味着它檢測到負載增加時,可以自動化地增加計算資源。而非彈性的系統則需要手動擴展(人工分析性能並決定向系統中添加更多的機器)。當系統負載很難預測時,彈性系統會非常有用,而手動擴展的系統更加簡單,並且可以減少操作上的意外。

Elastic 表示系統可以根據負載情況和相關策略自動調整計算資源,所以也稱為 Auto Scaling。例如一個電商應用會在大促時有更大的負載量,則需要自動添加更多的服務器等資源保障系統正常提供服務,而在平時負載量小的時候,則自動減少資源來控制成本。當我們看到“彈性伸縮”這個術語時,要知道這裏的“彈性”代表的意思就是 elastic。

例如,Kubernetes 提供了 HorizontalPodAutoscaler,支持 Pod 水平自動擴縮容。阿里雲等商業雲平台也都提供了類似的彈性伸縮服務(Elastic Scaling Service),可根據負載情況和策略自動調整計算能力(即實例數量)。

Resilient

Elastic 代表的“彈性”強調的是復原能力

在《Designing Data-Intensive Applications》一書中,對 resilient 的定義:

The things that can go wrong are called faults, and systems that anticipate faults and can cope with them are called fault-tolerant or resilient.

翻譯:可能出錯的事情被稱為故障,系統能夠預測並應對故障的能力稱為容錯或彈性。

Resilient 表示系統有容錯和故障恢復能力,從而系統具有可靠性。

例如,Java 著名的開源庫 Hystrix 的介紹是這樣的:

Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

這裏的 resilience 當然指的不是彈性伸縮能力,而是容錯能力。

除 Hystrix 外,其它開源的容錯庫:

  • Resilience4j: Resilience4j is a fault tolerance library designed for Java8 and functional programming.
  • Sentinel: A powerful flow control component enabling reliability, resilience and monitoring for microservices.
  • Polly: Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.
  • go-resiliency: Resiliency patterns for golang.
  • Semian: Resiliency toolkit for Ruby for failing fast.

實現 resilient 的策略通常包括斷路器(Circuit Breaker)、限流器(Rate Limiter)、重試(Retry)、艙壁(Bulkhead)等,更多可參考:https://github.com/App-vNext/... 。

下面簡單介紹常用的幾種容錯的策略:

  • 重試(Retry):很多錯誤是短暫的並且可以自動恢復的,對這種問題採用重試策略。
  • 斷路器(Circuit Breaker):類似於電路或股市中的“熔斷”概念,當系統發生嚴重故障(大量超時或失敗)時,為了避免後續持續不斷的請求導致故障系統過載,超時導致網絡、線程資源佔用,最終產生雪崩,而在一段時間內直接 fail fast(快速失敗,即直接返回錯誤而不再去請求故障的系統模塊)。
  • 艙壁(Bulkhead):《泰坦尼克號》電影中有一段對船體的描述:船體包含 16 個相互隔離的水密艙,即使有 4 個水密艙受損進水也能保證船漂浮在海面上。架構設計中艙壁模式參考的就是這種方式,將資源進行隔離,例如可以為調用多個服務的消費者分配每個服務獨立的連接池,從而保證一種故障只會影響到其對應的資源,而不會造成級聯故障。
  • 限流器(Rate Limiter):通過限流算法(如令牌桶算法、漏桶算法),限制在特定時間段內的執行次數、數據量等指標,從而防止系統過載。

掃碼關注我

掃碼關注

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.