Now Reading
Journey planning with sagas however with out the luggage

Journey planning with sagas however with out the luggage

2023-05-30 13:43:40

Journey planning with sagas however with out the luggage

So the final time your loved ones went to the native park everybody was speaking concerning the Saga Design Sample, and now you wish to know what it’s, in case you ought to make it a part of your personal distributed system design, and find out how to implement it. As everyone knows, software program design is all about trendy1 traits2.

The case for sagas

In the event you’re questioning if the saga sample is true on your state of affairs, ask your self: does your logic contain a number of steps, a few of which span machines, providers, shards, or databases, for which partial execution is undesirable? Seems, that is precisely the place sagas are helpful. Perhaps you’re checking stock, charging a consumer’s bank card, after which fulfilling the order. Perhaps you’re managing a provide chain. The saga sample is useful as a result of it principally features as a state machine storing program progress, stopping a number of bank card prices, reverting if crucial, and realizing precisely find out how to safely resume in a constant state within the occasion of energy loss.

A typical life-based instance used to clarify the way in which the saga sample compensates for failures is journey planning. Suppose you’re itching to absorb the rain in Duwamish territory, Seattle. You’ll must buy an airplane ticket, reserve a lodge, and get a ticket for a guided backpacking expertise on Mount Rainier. All three of those duties are coupled: in case you’re unable to buy a aircraft ticket there’s no motive to get the remainder. In the event you get a aircraft ticket however have nowhere to remain, you’re going to wish to cancel that aircraft reservation (or retry the lodge reservation or discover elsewhere to remain). Lastly in case you can’t e-book that backpacking journey, there’s really no other reason to return to Seattle so that you may as effectively cancel the entire thing. (Kidding!)

Saga Pattern Diagram Example

Above: a simplistic mannequin of compensating within the face of journey planning failures.

There are a lot of “do all of it, or don’t trouble” software program purposes within the real-world: in case you efficiently cost the consumer for an merchandise however your achievement service stories that the merchandise is out of inventory, you’re going to have upset customers in case you don’t refund the cost. If in case you have the other downside and by accident ship objects “without cost,” you’ll be out of enterprise. If the machine coordinating a machine studying information processing pipeline crashes however the follower machines stick with it processing the info with nowhere to report their information to, you will have a really costly compute assets invoice in your arms3. In all of those instances having some type of “progress monitoring” and compensation code to cope with these “do-it-all-or-don’t-do-any-of-it” duties is precisely what the saga sample gives. In saga parlance, these kinds of “all or nothing” duties are referred to as long-running transactions. This doesn’t essentially imply such actions run for a “lengthy” time, simply that they require extra steps in logical time4 than one thing operating domestically interacting with a single database.

How do you construct a saga?

A saga consists of two components:

  1. Outlined conduct for “going backwards” if you could “undo” one thing (i.e., compensations)
  2. Habits for striving in direction of ahead progress (i.e., saving state to know the place to recuperate from within the face of failure)

The avid reader of this weblog will bear in mind I just lately wrote a post about compensating actions. As you’ll be able to see from above, compensations are however one half of the saga design sample. The opposite half, alluded to above, is basically state administration for the entire system. The compensating actions sample helps you know the way to recuperate if a person step (or in Temporal terminology, an Activity) fails. However what if the complete system goes down? The place do you begin again up? Since not each step may need a compensation connected, you’d be compelled to do your finest guess primarily based on saved compensations. The saga sample retains monitor of the place you’re at the moment with the intention to preserve driving in direction of ahead progress.

So how do I implement sagas in my very own code?

I’m so glad you requested.

leans ahead

whispers in ear

It is a little little bit of a trick query as a result of by operating your code with Temporal, you routinely get your state saved and retries on failure at any degree. Which means the saga sample with Temporal is so simple as coding up the compensation you want to take when a step (Exercise) fails. The top.

The _why _behind this magic is Temporal, by design, routinely retains monitor of the progress of your program and might decide up the place it left off within the face of catastrophic failure. Moreover, Temporal will retry Actions on failure, with out you needing so as to add any code past specifying a Retry Coverage, e.g.,:

RetryOptions retryoptions = RetryOptions.newBuilder()

To study extra about how this automagic works, keep tuned for my upcoming put up on choreography and orchestration, the 2 frequent methods of implementing sagas.

So to precise the high-level logic of my program with each the holiday reserving steps plus compensations I want to tackle failure, it could seem like the next in pseudocode:

   run all compensation actions

In Java, the Saga class retains monitor of compensations for you:

public void bookVacation(BookingInfo data) {
   Saga saga = new Saga(new Saga.Choices.Builder().construct());
   attempt {
       saga.addCompensation(actions::cancelHotel, data.getClientId());

       saga.addCompensation(actions::cancelFlight, data.getClientId());

   } catch (TemporalFailure e) {
       throw e;

In different language SDKs you’ll be able to simply write the addCompensation and compensate features your self. This is a model in Go:

func (s *Compensations) AddCompensation(exercise any, parameters ...any) {
	s.compensations = append(s.compensations, exercise)
	s.arguments = append(s.arguments, parameters)

func (s Compensations) Compensate(ctx workflow.Context, inParallel bool) {
	if !inParallel {
		// Compensate in Final-In-First-Out order, to undo within the reverse order that activies have been utilized.
		for i := len(s.compensations) - 1; i >= 0; i-- {
			errCompensation := workflow.ExecuteActivity(ctx, s.compensations[i], s.arguments[i]...).Get(ctx, nil)
			if errCompensation != nil {
				workflow.GetLogger(ctx).Error("Executing compensation failed", "Error", errCompensation)
	} else {
		selector := workflow.NewSelector(ctx)
		for i := 0; i < len(s.compensations); i++ {
			execution := workflow.ExecuteActivity(ctx, s.compensations[i], s.arguments[i]...)
			selector.AddFuture(execution, func(f workflow.Future) {
				if errCompensation := f.Get(ctx, nil); errCompensation != nil {
					workflow.GetLogger(ctx).Error("Executing compensation failed", "Error", errCompensation)
		for vary s.compensations {

The excessive degree Go code of steps and compensations will look similar to the Java model:

func TripPlanningWorkflow(ctx workflow.Context, data BookingInfo) (err error) {
   choices := workflow.ActivityOptions{
       StartToCloseTimeout: time.Second * 5,
       RetryPolicy:         &temporal.RetryPolicy{MaximumAttempts: 2},

   ctx = workflow.WithActivityOptions(ctx, choices)

   var compensations Compensations

   defer func() {
       if err != nil {
           // exercise failed, and workflow context is canceled
           disconnectedCtx, _ := workflow.NewDisconnectedContext(ctx)
           compensations.Compensate(disconnectedCtx, true)

   err = workflow.ExecuteActivity(ctx, BookHotel, data).Get(ctx, nil)
   if err != nil {
       return err

   err = workflow.ExecuteActivity(ctx, BookFlight, data).Get(ctx, nil)
   if err != nil {
       return err

   err = workflow.ExecuteActivity(ctx, BookExcursion, data).Get(ctx, nil)
   if err != nil {
       return err

   return err

This high-level sequence of code above is known as a Temporal Workflow. And, as talked about earlier than, by operating with Temporal, we don’t have to fret about implementing any of the bookkeeping to trace our progress through occasion sourcing or including retry and restart logic as a result of that each one comes without cost. So when writing code that runs with Temporal, you solely want to fret about writing compensations, and the remainder is supplied without cost.

See Also


Properly, okay, there’s a second factor to “fear about.” As you could recall, sagas include two components, the primary half being these compensations we coded up beforehand. The second half, “striving in direction of ahead progress” includes doubtlessly retrying an exercise within the face of failure. Let’s dig into a type of steps, lets? Temporal does all of the heavy lifting of retrying and conserving monitor of your total progress, nevertheless as a result of code might be retried, you, the programmer, want to verify every Temporal Exercise is idempotent. This implies the noticed results of bookFlight is similar, whether or not it’s referred to as one time or many occasions. To make this a bit extra concrete, a operate that units some subject foo=3 is idempotent as a result of afterwards foo might be 3 regardless of what number of occasions you name it. The operate foo += 3 shouldn’t be idempotent as a result of the worth of foo depends on the variety of occasions your operate is known as. Non-idempotency can typically look extra refined: in case you have a database that enables duplicate data, a operate that calls INSERT INTO foo (bar) VALUES (3) will blithely create as many data in your desk as occasions you name it and is subsequently not idempotent. Naive implementations of features that ship emails or switch cash are additionally not idempotent by default.

In the event you’re backing away slowly proper now as a result of your Actual World Software does much more complicated issues than set foo=3, take coronary heart. There’s a resolution. You should utilize a definite identifier, referred to as an idempotency key, or typically referred to as a referenceId or one thing much like uniquely establish a specific transaction and make sure the lodge reserving transaction happens successfully as soon as. The best way this idempotency key could also be outlined primarily based in your utility wants. Within the journey planning utility, clientId, a subject in BookingInfo is used to uniquely establish transactions.

kind BookingInfo struct {
   Title     string
   ClientId string
   Deal with  string
   CcInfo   CreditCardInfo
   Begin    date.Date
   Finish      date.Date

You additionally in all probability noticed the clientId used to register the compensation within the above Java workflow code:

saga.addCompensation(actions::cancelHotel, data.getClientId());

Nevertheless, utilizing clientId as our key limits a specific individual from reserving a couple of trip without delay. That is in all probability what we wish. Nevertheless, some enterprise purposes could select to construct an idempotency key by combining the clientId and the workflowId to permit a couple of trip without delay booked per-client. In the event you needed a very distinctive idempotency key you possibly can go in a UUID to the workflow. The selection is as much as you primarily based in your utility’s wants.

Many third-party APIs that handle money already settle for idempotency keys for this very objective. If you could implement one thing like this your self, use atomic writes to maintain a document of the idempotency keys you’ve seen up to now, and don’t carry out an operation if its idempotency key’s within the “already seen” set.

Advantages vs Complexity

The saga sample does add complexity to your code, so it’s necessary to not implement it in your code simply as a result of you’ve microservices. Nevertheless, if you could full a process (like reserving a visit with an airfare and lodge) that includes a number of providers and partial execution shouldn’t be really a hit, then a saga might be your good friend. Moreover, in case you discover your saga getting notably unwieldy, it could be time to rethink how your microservices are divided up, and roll up the ol’ sleeves to refactor. General, Temporal makes implementing the saga sample in your code comparatively trivial because you solely must write the compensations wanted for every step. Keep tuned for my subsequent put up, the place I dig into sagas and subscription eventualities, the place Temporal notably shines in decreasing complexity when working with sagas.

The complete repository that makes use of the code talked about on this article might be discovered on GitHub:

If you wish to see different tutorials of sagas utilizing Temporal, please take a look at the next assets:

Moreover considered one of my colleagues, Dominik Tornow, gave an intro to sagas on YouTube.

Be taught extra about Temporal in our courses, tutorials, docs, and videos.


Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top