Azure Service Fabric 有状态服务中的可靠集合简介Introduction to Reliable Collections in Azure Service Fabric stateful services

可靠集合可让你编写高度可用、可缩放且低延迟的云应用程序,就像编写单计算机应用程序一样。Reliable Collections enable you to write highly available, scalable, and low-latency cloud applications as though you were writing single computer applications. Microsoft.ServiceFabric.Data.Collections 命名空间中的类提供一组自动使状态具备高可用性的集合。The classes in the Microsoft.ServiceFabric.Data.Collections namespace provide a set of collections that automatically make your state highly available. 开发人员只需面向可靠集合 API 编程,并让可靠集合管理复制状态和本地状态。Developers need to program only to the Reliable Collection APIs and let Reliable Collections manage the replicated and local state.

Reliable Collections 与其他高可用性技术(如 Redis、Azure 表服务和 Azure 队列服务)的主要区别在于其状态以本地方式保存在服务实例中,同时仍实现高可用性。The key difference between Reliable Collections and other high-availability technologies (such as Redis, Azure Table service, and Azure Queue service) is that the state is kept locally in the service instance while also being made highly available. 这意味着:This means that:

  • 所有读取均在本地进行,可保障读取的低延迟和高吞吐量。All reads are local, which results in low latency and high-throughput reads.
  • 所有写入都只产生最少量的网络 IO,可保障写入的低延迟和高吞吐量。All writes incur the minimum number of network IOs, which results in low latency and high-throughput writes.


可以将可靠集合视作 System.Collections 类的自然演变:它们是一组新的集合,专为云应用程序和多计算机应用程序设计,且不会为开发人员增加复杂性。Reliable Collections can be thought of as the natural evolution of the System.Collections classes: a new set of collections that are designed for the cloud and multi-computer applications without increasing complexity for the developer. 因此,可靠集合的特性如下:As such, Reliable Collections are:

  • 可复制:复制状态更改以实现高可用性。Replicated: State changes are replicated for high availability.
  • 异步:API 采用异步模式,以确保在产生 IO 时不会阻止线程。Asynchronous: APIs are asynchronous to ensure that threads are not blocked when incurring IO.
  • 事务性:API 利用事务抽象方法,让可以在某个服务内轻松管理多个可靠集合。Transactional: APIs utilize the abstraction of transactions so you can manage multiple Reliable Collections within a service easily.
  • 持久化或易失性:数据会保存至磁盘,可在发生大规模中断(例如,数据中心断电)时保障持续性。Persisted or Volatile: Data can be persisted to disk for durability against large-scale outages (for example, a datacenter power outage). 某些可靠集合还支持易失性模式(带警告),在这种模式下,所有数据都保留在内存中,例如复制的内存中缓存。Some Reliable Collections also support a volatile mode (with Caveats) where all data is kept in-memory, such as a replicated in-memory cache.

Reliable Collections 提供全新的非常一致保证,使应用程序状态推断变得更轻松。Reliable Collections provide strong consistency guarantees out of the box to make reasoning about application state easier. 非常一致通过以下方法实现:确保仅对副本的多数仲裁(包括主副本)记录整个事务后,才完成事务提交。Strong consistency is achieved by ensuring transaction commits finish only after the entire transaction has been logged on a majority quorum of replicas, including the primary. 若要实现较弱的一致性,应用程序可以在异步提交返回之前,回头向客户端/请求者进行确认。To achieve weaker consistency, applications can acknowledge back to the client/requester before the asynchronous commit returns.

可靠集合 API 由并发集合 API(位于 System.Collections.Concurrent 命名空间中)演变而来:The Reliable Collections APIs are an evolution of concurrent collections APIs (found in the System.Collections.Concurrent namespace):

  • 异步:返回任务;不同于并发集合,其操作会受到复制及保存。Asynchronous: Returns a task since, unlike concurrent collections, the operations are replicated and persisted.
  • 没有 out 参数:使用 ConditionalValue<T> 返回 bool 和值,而不是返回 out 参数。No out parameters: Uses ConditionalValue<T> to return a bool and a value instead of out parameters. ConditionalValue<T>Nullable<T> 类似,但不要求 T 是一个 struct。ConditionalValue<T> is like Nullable<T> but does not require T to be a struct.
  • 事务:使用事务对象,让用户可以对事务中多个 Reliable Collections 上的操作分组。Transactions: Uses a transaction object to enable the user to group actions on multiple Reliable Collections in a transaction.

目前,Microsoft.ServiceFabric.Data.Collections 包含三个集合:Today, Microsoft.ServiceFabric.Data.Collections contains three collections:

  • Reliable Dictionary:表示可复制、事务性和异步的键/值对集合。Reliable Dictionary: Represents a replicated, transactional, and asynchronous collection of key/value pairs. 类似于 ConcurrentDictionary,键和值都可以是任何类型。Similar to ConcurrentDictionary, both the key and the value can be of any type.
  • 可靠队列:表示可复制、事务性和异步的严格先进先出 (FIFO) 队列。Reliable Queue: Represents a replicated, transactional, and asynchronous strict first-in, first-out (FIFO) queue. 类似于 ConcurrentQueue,值可以是任何类型。Similar to ConcurrentQueue, the value can be of any type.
  • 可靠的并发队列:表示用于实现高吞吐量的可复制、事务性和异步的“尽力排序”队列。Reliable Concurrent Queue: Represents a replicated, transactional, and asynchronous best effort ordering queue for high throughput. 它与 ConcurrentQueue 类似,值可以是任意类型。Similar to the ConcurrentQueue, the value can be of any type.

后续步骤Next steps