雲端設計模式Cloud Design Patterns

這些設計模式有助於在雲端中建置可靠、可擴充且安全的應用程式。These design patterns are useful for building reliable, scalable, secure applications in the cloud.

每個模式都會說明模式處理的問題、套用模式的考量,以及以 Microsoft Azure 為基礎的範例。Each pattern describes the problem that the pattern addresses, considerations for applying the pattern, and an example based on Microsoft Azure. 大部分的模式會包括程式碼範例或程式碼片段,示範如何在 Azure 上實作模式。Most of the patterns include code samples or snippets that show how to implement the pattern on Azure. 無論如何,大部分的模式都適用於任一分散式系統 (無論是裝載於 Azure 或其他雲端平台上)。However, most of the patterns are relevant to any distributed system, whether hosted on Azure or on other cloud platforms.

雲端開發中的挑戰Challenges in cloud development

Availability

可用性Availability

可用性是系統執行功能及運作的時間比例,通常是以執行時間的百分比表示。Availability is the proportion of time that the system is functional and working, usually measured as a percentage of uptime. 可能會受到系統錯誤、基礎結構問題、惡意攻擊和負載系統的影響。It can be affected by system errors, infrastructure problems, malicious attacks, and system load. 雲端應用程式一般會提供使用者服務等級協定 (SLA),因此應用程式必須設計為可充分提高可用性。Cloud applications typically provide users with a service level agreement (SLA), so applications must be designed to maximize availability.

Data Management

資料管理Data Management

資料管理是雲端應用程式的關鍵元素,並且會影響多數的品質屬性。Data management is the key element of cloud applications, and influences most of the quality attributes. 因為效能、延展性或可用性之類的原因,資料通常裝載在不同位置以及在多個伺服器上,而這可能出現一些挑戰。Data is typically hosted in different locations and across multiple servers for reasons such as performance, scalability or availability, and this can present a range of challenges. 例如,必須維持資料的一致性,並且通常需要同步處理跨不同位置的資料。For example, data consistency must be maintained, and data will typically need to be synchronized across different locations.

Design and Implementation

設計與實作Design and Implementation

良好的設計會包含一些要素,例如元件設計和部署中的一致性及連貫性、用於簡化管理及開發的可維護性,以及可讓元件和子系統在其他應用程式和其他案例中使用的重複使用性。Good design encompasses factors such as consistency and coherence in component design and deployment, maintainability to simplify administration and development, and reusability to allow components and subsystems to be used in other applications and in other scenarios. 設計和實作階段所做的決策,會對雲端上裝載的應用程式和服務在品質和擁有權總成本上產生重大影響。Decisions made during the design and implementation phase have a huge impact on the quality and the total cost of ownership of cloud hosted applications and services.

Messaging

傳訊Messaging

雲端應用程式的分散式本質需要可連接元件和服務的傳訊基礎結構,最理想的情況是使用鬆散耦合的方式來達到延展性最大化。The distributed nature of cloud applications requires a messaging infrastructure that connects the components and services, ideally in a loosely coupled manner in order to maximize scalability. 已廣泛使用的非同步傳訊提供許多優點,但也帶來傳訊的順序安排、有害訊息的管理和冪等性 (idempotency) 等挑戰。Asynchronous messaging is widely used, and provides many benefits, but also brings challenges such as the ordering of messages, poison message management, idempotency, and more.

Management and Monitoring

管理與監視Management and Monitoring

雲端應用程式會在遠端資料中心內執行,而您並沒有其基礎結構或作業系統 (有時候) 的完整控制權。Cloud applications run in a remote datacenter where you do not have full control of the infrastructure or, in some cases, the operating system. 比起內部部署,這可能會讓管理和監視作業變得更困難。This can make management and monitoring more difficult than an on-premises deployment. 應用程式必須公開執行階段資訊,讓系統管理員及操作員可以使用該資訊來管理及監視系統,以及支援變更商業需求和自訂,而不需要停止或重新部署應用程式。Applications must expose runtime information that administrators and operators can use to manage and monitor the system, as well as supporting changing business requirements and customization without requiring the application to be stopped or redeployed.

Performance and Scalability

效能和延展性Performance and Scalability

效能是指系統在指定時間間隔內執行任何動作的回應能力,而延展性則是系統在不影響效能情況下處理負載增量的能力,或是系統處理可用資源快速增加的能力。Performance is an indication of the responsiveness of a system to execute any action within a given time interval, while scalability is ability of a system either to handle increases in load without impact on performance or for the available resources to be readily increased. 雲端應用程式通常會遇到變動的工作負載和活動尖峰。Cloud applications typically encounter variable workloads and peaks in activity. 要預測這些問題 (尤其是在多租用戶案例中) 幾乎不可能。Predicting these, especially in a multitenant scenario, is almost impossible. 相反地,應用程式應該能夠在限制範圍內相應放大以符合尖峰需求,並在需求降低時相應縮小。Instead, applications should be able to scale out within limits to meet peaks in demand, and scale in when demand decreases. 延展性不只要考量運算執行個體,還有其他資料儲存體和傳訊基礎結構等項目。Scalability concerns not just compute instances, but other elements such as data storage, messaging infrastructure, and more.

Resiliency

復原Resiliency

復原是指系統正常處理並從失敗中復原的能力。Resiliency is the ability of a system to gracefully handle and recover from failures. 雲端裝載的本質,像是其中的應用程式通常是多租用戶、使用共用平台服務、爭用資源和頻寬、透過網際網路通訊及採用商用硬體,這表示發生暫時性和更多永久性錯誤的可能性會同時增加。The nature of cloud hosting, where applications are often multitenant, use shared platform services, compete for resources and bandwidth, communicate over the Internet, and run on commodity hardware means there is an increased likelihood that both transient and more permanent faults will arise. 必須能偵測失敗並快速而有效地復原,才能保有復原功能。Detecting failures, and recovering quickly and efficiently, is necessary to maintain resiliency.

Security

安全性Security

安全性是指系統有能力防止正確用法以外的惡意或意外動作,並且防止洩漏或遺失資訊。Security is the capability of a system to prevent malicious or accidental actions outside of the designed usage, and to prevent disclosure or loss of information. 雲端應用程式皆暴露在受信任內部部署範圍外的網際網路上,且開放給大眾使用,也可能服務不受信任的使用者。Cloud applications are exposed on the Internet outside trusted on-premises boundaries, are often open to the public, and may serve untrusted users. 應用程式的設計和部署方式應要能保護應用程式不受到惡意攻擊、僅限核准的使用者可存取應用程式,以及保護敏感性資料。Applications must be designed and deployed in a way that protects them from malicious attacks, restricts access to only approved users, and protects sensitive data.

模式的目錄Catalog of patterns

模式Pattern 總結Summary
外交官 (Ambassador)Ambassador 建立會代表取用者服務或應用程式傳送網路要求的協助程式服務。Create helper services that send network requests on behalf of a consumer service or application.
防損毀層Anti-Corruption Layer 在現代應用程式和舊版系統間實作外觀或配接器層。Implement a façade or adapter layer between a modern application and a legacy system.
前端的後端Backends for Frontends 建立由特定前端應用程式或介面取用的個別後端服務。Create separate backend services to be consumed by specific frontend applications or interfaces.
隔艙Bulkhead 將應用程式的元素隔離到集區中,以便其中一個元素失敗時,其他元素可以繼續運作。Isolate elements of an application into pools so that if one fails, the others will continue to function.
另行快取Cache-Aside 依需要從資料存放區將資料載入快取中Load data on demand into a cache from a data store
斷路器Circuit Breaker 在連線到遠端服務或資源時,處理可能需要不同時間來修復的錯誤。Handle faults that might take a variable amount of time to fix when connecting to a remote service or resource.
提領票證Claim Check 將大型訊息分割成提領票證與承載,以免癱瘓訊息匯流排。Split a large message into a claim check and a payload to avoid overwhelming a message bus.
補償交易Compensating Transaction 復原由一系列步驟執行的工作,這些步驟共同定義結果一致的作業。Undo the work performed by a series of steps, which together define an eventually consistent operation.
競爭取用者Competing Consumers 讓多個並行取用者處理在相同傳訊通道上接收的訊息。Enable multiple concurrent consumers to process messages received on the same messaging channel.
計算資源彙總Compute Resource Consolidation 將多個工作或作業合併為單一計算單位Consolidate multiple tasks or operations into a single computational unit
CQRSCQRS 隔離自使用個別介面來更新資料的作業中讀取資料的作業。Segregate operations that read data from operations that update data by using separate interfaces.
事件來源Event Sourcing 使用附加專用存放區記錄完整系列的事件,其描述對網域中的資料採取的動作。Use an append-only store to record the full series of events that describe actions taken on data in a domain.
外部設定存放區External Configuration Store 將設定資訊從應用程式部署套件移至集中位置。Move configuration information out of the application deployment package to a centralized location.
同盟身分識別Federated Identity 將驗證委派給外部身分識別提供者。Delegate authentication to an external identity provider.
閘道管理員Gatekeeper 可保護應用程式和服務,方法是使用專用的主機執行個體,其會作為用戶端和應用程式或服務之間的代理程式、會驗證和處理要求,並在兩者之間傳遞要求和資料。Protect applications and services by using a dedicated host instance that acts as a broker between clients and the application or service, validates and sanitizes requests, and passes requests and data between them.
閘道彙總Gateway Aggregation 您可以使用閘道來將多個個別的要求彙總成單一要求。Use a gateway to aggregate multiple individual requests into a single request.
閘道卸載Gateway Offloading 將共用或特殊服務功能卸載至閘道 Proxy。Offload shared or specialized service functionality to a gateway proxy.
閘道路由Gateway Routing 使用單一端點將要求路由至多個服務。Route requests to multiple services using a single endpoint.
健康情況端點監視Health Endpoint Monitoring 實作應用程式中的功能檢查,而外部工具可透過公開的端點定期存取此應用程式。Implement functional checks in an application that external tools can access through exposed endpoints at regular intervals.
索引資料表Index Table 針對資料存放區中查詢經常參考的欄位建立索引。Create indexes over the fields in data stores that are frequently referenced by queries.
選出領導者Leader Election 選取一個執行個體作為領導者,負責管理其他執行個體,協調分散式應用程式中共同作業工作執行個體集合執行的動作。Coordinate the actions performed by a collection of collaborating task instances in a distributed application by electing one instance as the leader that assumes responsibility for managing the other instances.
具體化檢視模式Materialized View 當資料格式對必要的查詢作業而言不理想時,對一或多個資料存放區中的資料產生預先填入的檢視。Generate prepopulated views over the data in one or more data stores when the data isn't ideally formatted for required query operations.
管道與篩選器Pipes and Filters 將執行複雜處理程序的工作,細分成一系列可重複使用的個別元素。Break down a task that performs complex processing into a series of separate elements that can be reused.
優先順序佇列Priority Queue 針對傳送給服務的要求排列優先順序,讓高優先順序要求的接收和處理順序在低優先順序要求之前。Prioritize requests sent to services so that requests with a higher priority are received and processed more quickly than those with a lower priority.
發行者/訂閱者Publisher/Subscriber 讓應用程式能夠非同步地向多個感興趣的取用者宣告事件,而不需要將傳送者與取用者結合。Enable an application to announce events to multiple interested consumers asynchronously, without coupling the senders to the receivers.
佇列型負載調節Queue-Based Load Leveling 使用佇列來作為工作與其所叫用服務之間的緩衝區,以使間歇性的繁重負載順暢。Use a queue that acts as a buffer between a task and a service that it invokes in order to smooth intermittent heavy loads.
重試Retry 讓應用程式可以在嘗試連線到服務或網路資源時,藉由明確地重試先前失敗的作業,處理預期的暫時性失敗。Enable an application to handle anticipated, temporary failures when it tries to connect to a service or network resource by transparently retrying an operation that's previously failed.
排程器代理程式監督員Scheduler Agent Supervisor 在一組分散的服務和其他遠端資源中協調一組動作。Coordinate a set of actions across a distributed set of services and other remote resources.
分區化Sharding 將資料存放區分割為一組水平分割或分區。Divide a data store into a set of horizontal partitions or shards.
側車Sidecar 將應用程式的元件部署到個別的處理序或容器,以提供隔離和封裝。Deploy components of an application into a separate process or container to provide isolation and encapsulation.
靜態內容裝載Static Content Hosting 將靜態內容部署到可以直接將其交付給用戶端的雲端儲存體服務。Deploy static content to a cloud-based storage service that can deliver them directly to the client.
StranglerStrangler 透過將功能的特定片段逐漸取代成新的應用程式和服務,來逐步移轉舊有系統。Incrementally migrate a legacy system by gradually replacing specific pieces of functionality with new applications and services.
節流Throttling 控制應用程式執行個體、個別租用戶或整個服務所使用的資源耗用量。Control the consumption of resources used by an instance of an application, an individual tenant, or an entire service.
Valet 金鑰Valet Key 使用可提供用戶端對特定資源或服務受限制的直接存取的權杖或金鑰。Use a token or key that provides clients with restricted direct access to a specific resource or service.