Compensating Transaction Pattern(事务修正模式)

By admin in 亚洲必赢app在哪下载 on 2018年10月14日

Undo the work performed by a series of steps, which together define an
eventually consistent operation, if one or more of the steps fail.
Operations that follow the eventual consistency model are commonly found
in cloud-hosted applications that implement complex business processes
and workflows.

收回由同名目繁多步骤执行之干活,一起定义一个尾声一致的操作,如果一个或者多个步骤失败。在云托管的应用程序,实现复杂的事体及办事流程,通常操作是按部就班的最后一致性模型。

Context and Problem 背景与问题

Applications running in the cloud frequently modify data. This data may
be spread across an assortment of data sources held in a variety of
geographic locations. To avoid contention and improve performance in a
distributed environment such as this, an application should not attempt
to provide strong transactional consistency. Rather, the application
should implement eventual consistency. In this model, a typical
business operation consists of a series of autonomous steps. While these
steps are being performed the overall view of the system state may be
inconsistent, but when the operation has completed and all of the steps
have been executed the system should become consistent again.

于云端运行的应用程序经常修改数据,此数据也许会见分散在不同地理位置的多少源中。为了避免竞争及增强性能,如以分布式环境遭到,应用程序不应当计算提供强大的事体一致性。相反,应用程序应该实现最终之一致性。在是模型中,一个名列前茅的作业操作由同多级的独立步骤做。虽然这些手续正在开展系统状态的完整视图可能是勿均等的,但当操作都完成,并一度尽的具备手续的系应成同。

Note:

The Data Consistency
Primer provides
more information about why distributed transactions do not scale well,
and the principles that underpin the eventual consistency model.

A significant challenge in the eventual consistency model is how to
handle a step that has failed irrecoverably. In this case it may be
necessary to undo all of the work completed by the previous steps in the
operation. However, the data cannot simply be rolled back because other
concurrent instances of the application may have since changed it. Even
in cases where the data has not been changed by a concurrent instance,
undoing a step might not simply be a matter of restoring the original
state. It may be necessary to apply various business-specific rules (see
the travel website described in the
Example
section).

于结尾一致性模型的一个重中之重的挑战是何等处理一个破产的步调。在这种状况下,它或许是必需之撤所有的行事成就的前一步操作。然而,数据不克简单地回滚,因为应用程序的另外并发实例可能曾经变更了它。即使没有赢得数码的产出情况的改,撤消一步可能不仅仅是恢复原先的状态。它恐怕是必要的,适用于各种事情的切切实实规则(见例子)。

If an operation that implements eventual consistency spans several
heterogeneous data stores, undoing the steps in such an operation will
require visiting each data store in turn. The work performed in every
data store must be undone reliably to prevent the system from remaining
inconsistent.

如果实现最终一致性跨越多个异构数据存储操作,撤消步骤等操作需要各个走访每个数据存储。在每个数据存储着实施的行事且要叫撤除,以防范系统的匪均等。

Not all data affected by an operation that implements eventual
consistency might be held in a database. In a Service Oriented
Architecture (SOA) environment an operation may invoke an action in a
service, and cause a change in the state held by that service. To undo
the operation, this state change must also be undone. This may involve
invoking the service again and performing another action that reverses
the effects of the first.

并无是拥有的数额还见面影响及结尾之一致性,可能于一个数据库中开展。在面向服务的体系布局(SOA)环境下之操作可能在劳动调用动作,而招致的服务状态的转变。撤消该操作,该状态更改为必须被取消。这或许涉及还调用服务,并履行外一个五花大绑的影响的行。

Solution 解决方案

Implement a compensating transaction. The steps in a compensating
transaction must undo the effects of the steps in the original
operation. A compensating transaction might not be able to simply
replace the current state with the state the system was in at the start
of the operation because this approach could overwrite changes made by
other concurrent instances of an application. Rather, it must be an
intelligent process that takes into account any work done by concurrent
instances. This process will usually be application-specific, driven by
the nature of the work performed by the original operation.

兑现互补工作。补偿工作中的步骤必须撤回消原操作步骤的熏陶。一个填补工作可能不能够简单地更迭当前运行状态的状态,因为这种方法可以挂应用程序的外并发实例进行改动。相反,它要是一个智能的历程,需要考虑到外工作所做的面世实例。这个历程一般是特定于应用程序的,由原本操作实践之办事之性能驱动的。

A common approach to implementing an eventually consistent operation
that requires compensation is to use a workflow. As the original
operation proceeds, the system records information about each step and
how the work performed by that step can be undone. If the operation
fails at any point, the workflow rewinds back through the steps it has
completed and performs the work that reverses each step. Note that a
compensating transaction might not have to undo the work in the exact
mirror-opposite order of the original operation, and it may be possible
to perform some of the undo steps in parallel.

同种植常见的艺术来落实一个说到底一致的操作,需要加操作是运用一个工作流。作为原有的操作进行,系统记录有关各一样步之消息,以及怎样就该手续所实施之办事。如果在其余时候操作失败,工作流将回到到步骤都就及实施工作的各个一样步。请小心,补偿工作可能无欲在原本的操作的镜像相反的逐条中收回工作,并可能出或实行有相互的取消步骤。

Note:

This approach is similar to the Sagas strategy. A description of this
strategy is available online in Clemens Vasters’
blog.

A compensating transaction is itself an eventually consistent operation
and it could also fail. The system should be able to resume the
compensating transaction at the point of failure and continue. It may be
necessary to repeat a step that has failed, so the steps in a
compensating transaction should be defined as idempotent commands. For
more information about idempotency, see Idempotency
Patterns
on Jonathan Oliver’s blog.

补偿性事务本身就是是一致栽最终一致的操作,也可能破产。该体系应该会东山再起在故障点的补偿性操作,并继续。可能需要还失败步骤,那么当补充工作之步调应定义为幂等的指令。关于幂等性的重多信息,参见Idempotency
Patterns
on Jonathan Oliver’s blog。

In some cases it may not be possible to recover from a step that has
failed except through manual intervention. In these situations the
system should raise an alert and provide as much information as possible
about the reason for the failure.

以一些情况下,它或许无法恢复从一个挫折的手续,除了通过人工干预。在这种情况下,系统应提供预警,并尽量为破产的原由提供多之音。

Issues and Considerations 问题和注意事项

Consider the following points when deciding how to implement this
pattern:

于决定哪些兑现这模式时,考虑以下几点:

  • It might not be easy to determine when a step in an operation that
    implements eventual consistency has failed. A step might not fail
    immediately, but instead it could block. It may be necessary to
    implement some form of time-out mechanism.
  • 当即恐怕无便于决断,在一个末段一致性操作的某个步骤的操作失败。一个手续可能无会见就失败,但它反而吃堵塞。它恐怕要贯彻某种形式之超时机制。
  • Compensation logic is not easily generalized. A compensating
    transaction is application-specific; it relies on the application
    having sufficient information to be able to undo the effects of each
    step in a failed operation.
  • 加论理是免爱普及的。补偿工作是应用程序特定的,它凭借让应用程序有足够的信息,能够撤消一软破产的操作中的各国一样步之震慑。
  • You should define the steps in a compensating transaction as
    idempotent commands. This enables the steps to be repeated if the
    compensating transaction itself fails.
  • 汝当定义一个补偿工作也幂等的一声令下。这使得能够再的步骤,如果上本身失败。
  • The infrastructure that handles the steps in the original operation,
    and the compensating transaction, must be resilient. It must not
    lose the information required to compensate for a failing step, and
    it must be able to reliably monitor the progress of the compensation
    logic.
  • 处理在本来操作中的步骤同加工作之基本功要是发出弹性的。它不可知去所用的信息以弥补一个功亏一篑的步骤,它要能可靠地监测补偿论理的过程。
  • A compensating transaction does not necessarily return the data in
    the system to the state it was in at the start of the original
    operation. Instead, it compensates for the work performed by the
    steps that completed successfully before the operation failed.
  • 补给工作不必返回到原操作起来经常之系中之多少。相反,它弥补了办事好的手续,在操作失败之前完成的工作。
  • The order of the steps in the compensating transaction does not
    necessarily have to be the mirror opposite of the steps in the
    original operation. For example, one data store may be more
    sensitive to inconsistencies than another, and so the steps in the
    compensating transaction that undo the changes to this store should
    occur first.
  • 以补偿工作中步骤的相继不自然是在原的操作步骤的镜像。例如,一个数量存储可能比较其余一个尤其灵活,因此,在补充工作中,撤消对拖欠存储的改变的步骤应该率先发出。
  • Placing a short-term timeout-based lock on each resource that is
    required to complete an operation, and obtaining these resources in
    advance, can help increase the likelihood that the overall activity
    will succeed. The work should be performed only after all the
    resources have been acquired. All actions must be finalized before
    the locks expire.
  • 当各个一个资源达到停放一个短期的超时锁,来形成一个操作,并提早收获这些资源,可以帮增加整体走之可能。所有的资源都叫收购后,才开展当下工作。所有行动要以锁到期前就。
  • Consider using retry logic that is more forgiving than usual to
    minimize failures that trigger a compensating transaction. If a step
    in an operation that implements eventual consistency fails, try
    handling the failure as a transient exception and repeat the step.
    Only abort the operation and initiate a compensating transaction if
    a step fails repeatedly or irrecoverably.
  • 考虑动用重试逻辑,通常是的更爱的,以调减故障,触发一个互补工作。如果在一个操作中贯彻最终之一致性的步调失败,试着拍卖故障作为一个少的良,然后再度步骤。只有放弃操作,如果一个步骤失败或者频繁地启动补偿工作。

Note:

Many of the challenges and issues of implementing a compensating
transaction are the same as those concerned with implementing eventual
consistency. See the section Considerations for Implementing Eventual
Consistency in the Data Consistency
Primer for more
information.

When to Use this Pattern 什么时候用这种模式

Use this pattern only for operations that must be undone if they fail.
If possible, design solutions to avoid the complexity of requiring
compensating transactions (for more information, see the Data
Consistency
Primer).

运这模式仅用于操作必须是要是他们非克撤。如果可能吧,设计缓解方案因为避免复杂的要求加工作(更多信息,见数一致性引物)。

Example 例子

A travel website enables customers to book itineraries. A single
itinerary may comprise a series of flights and hotels. A customer
traveling from Seattle to London and then on to Paris could perform the
following steps when creating an itinerary:

一个出境游网站,使客户预订行程。一个纯的程可概括同多样的航班与小吃摊。一位消费者起西雅图及伦敦,然后至巴黎,在开创行程时,可以履以下步骤:

  1. Book a seat on flight F1 from Seattle to London.
  2. Book a seat on flight F2 from London to Paris.
  3. Book a seat on flight F3 from Paris to Seattle.
  4. Reserve a room at hotel H1 in London.
  5. Reserve a room at hotel H2 in Paris.

订一张由西雅图到伦敦的航班F1。
订一布置自伦敦及巴黎底航班F2。
订一摆自巴黎顶西雅图的航班F3。
褚在伦敦一律中间酒店H1。
在巴黎饭庄预订一个屋子。

These steps constitute an eventually consistent operation, although each
step is essentially a separate atomic action in its own right.
Therefore, as well as performing these steps, the system must also
record the counter operations necessary to undo each step in case the
customer decides to cancel the itinerary. The steps necessary to perform
the counter operations can then run as a compensating transaction if
necessary.

这些手续做最终一致性操作,虽然各个一样步基本上是在大团结之权单独的原子动作。因此,以及以履这些手续时,系统还非得记录必要撤消在场面下,客户决定收回行程的每个步骤中的计数器的操作。然后要实施计数器操作步骤可以以待时运行补偿性事务。

Notice that the steps in the compensating transaction might not be the
exact opposite of the original steps, and the logic in each step in the
compensating transaction must take into account any business-specific
rules. For example, “unbooking” a seat on a flight might not entitle the
customer to a complete refund of any money paid.

要留心,在加工作之手续可能不是本来的步骤完全相反,并且于补充工作之每个步骤都要考虑到其它特定的工作规则逻辑。例如,“订舱”在航班及之座席或无身份客户开发的别样款项全额退款。

亚洲必赢app在哪下载 1

Figure 1 – Generating a compensating transaction to undo a long-running
transaction to book a travel itinerary

图1 – 生成补偿事务撤消长日子运作的事体预订旅游路

Note:

It may be possible for the steps in the compensating transaction to be
performed in parallel, depending on how you have designed the
compensating logic for each step.

起或当加工作之手续并行取决于你如何计划用来每个步骤中之补论理给实施。

In many business solutions, failure of a single step does not always
necessitate rolling the system back by using a compensating transaction.
For example, if—after having booked flights F1, F2, and F3 in the travel
website scenario—the customer is unable to reserve a room at hotel H1,
it is preferable to offer the customer a room at a different hotel in
the same city rather than cancelling the flights. The customer may still
elect to cancel (in which case the compensating transaction runs and
undoes the bookings made on flights F1, F2, and F3), but this decision
should be made by the customer rather than by the system.

于重重工作解决方案,单步的挫折并无总是少不了运用上工作滚动系统恢复。例如,具有预定航班F1,F2和F3在出境游网站情景客户无法预订时酒店H1如果-之后,最好是能够吧客户提供于同一个市之屋子在不同的小吃摊要休是收回航班。客户还可挑选取消(在这种情景下,补偿工作中运作,并撤回关于航班F1,F2和F3作出的预订),但该控制应由用户,而不是出于系统进行。

Related Patterns and Guidance 相关模式与点

The following patterns and guidance may also be relevant when
implementing this pattern:

  • Data Consistency
    Primer. The
    Compensating Transaction pattern is frequently used to undo
    operations that implement the eventual consistency model. This
    primer provides more information on the benefits and tradeoffs of
    eventual consistency.
  • Scheduler-Agent-Supervisor
    Pattern.
    This pattern describes how to implement resilient systems that
    perform business operations that utilize distributed services and
    resources. In some circumstances, it may be necessary to undo the
    work performed by an operation by using a compensating transaction.
  • Retry
    Pattern.
    Compensating transactions can be expensive to perform, and it may be
    possible to minimize their use by implementing an effective policy
    of retrying failing operations by following the Retry pattern.

More Information 更多信息

  • The article
    Sagas on
    Clemens Vasters’ blog.
  • The article Idempotency
    Patterns
    on Jonathan Oliver’s blog.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2018 亚洲必赢app官方下载 版权所有