saga

package
v1.149.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package saga provides an implementation for the [SAGA pattern](https://microservices.io/patterns/data/saga.html) for [transaction like distribution processes](https://learn.microsoft.com/en-us/azure/architecture/patterns/saga) across multiple services without relying on a global ACID transaction. The SAGA orchestration pattern breaks a distributed business operation into a sequence of local transactions coordinated by a central orchestrator. Each step executes independently, and if any step fails, the orchestrator triggers compensating transactions in reverse order to undo completed work, following the [Compensating Transaction pattern](https://en.wikipedia.org/wiki/Compensating_transaction?utm_source=chatgpt.com) (https://learn.microsoft.com/en-us/azure/architecture/patterns/compensating-transaction). To make sagas safe across retries, failures, or orchestrator crashes, compensating transactions must themselves be designed to be idempotent and retryable. That way if a compensation fails (or the orchestrator crashes mid-rollback), they can be resumed/ retried without risk of double-undo or inconsistent state. For that purpose, idempotency keys are used, ensuring repeated “execute” or “compensate” calls do not duplicate effects, as described in [idempotency practices](https://brandur.org/idempotency-keys)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IActionArguments

type IActionArguments interface {
	// GetIdemKey return idempotent key to ensure safe
	// retries and crash recovery, preventing duplicate side effects.
	GetIdemKey() string
	GetArguments() map[string]any
}

func NewStepArguments

func NewStepArguments(args map[string]any) IActionArguments

func NewStepArgumentsWithIdempotentKey

func NewStepArgumentsWithIdempotentKey(idemKey string, args map[string]any) IActionArguments

func NoStepArguments

func NoStepArguments() IActionArguments

type IActionIdentifier

type IActionIdentifier interface {
	fmt.Stringer
	GetName() string
	GetNamespace() string
}

func NewStepIdentifier

func NewStepIdentifier(name, namespace string) IActionIdentifier

type ISagaOrchestrator

type ISagaOrchestrator interface {
	parallelisation.IExecutionGroup[ITransactionStep]
}

ISagaOrchestrator coordinates a sequence of local transactions (ITransactionStep) to achieve an eventually consistent distributed workflow without relying on a global ACID transaction. Each step has a forward action (Execute) and a compensating action (Compensate). The orchestrator executes steps in order; if any step fails, it triggers compensating actions in reverse order to undo previously completed steps.

This pattern is useful for long-running or cross-service operations where traditional two-phase commit is impractical.

References:

type ITransactionStep

type ITransactionStep interface {
	// GetID returns an identifier of the action
	GetID() IActionIdentifier
	// Execute performs the forward action.
	Execute(ctx context.Context, args IActionArguments) error
	// Compensate performs the compensating/rollback action.
	Compensate(ctx context.Context, args IActionArguments) error
}

ITransactionStep describes a step in the transaction across a distributed system.

type MinimalSaga

type MinimalSaga struct {
	// contains filtered or unexported fields
}

func NewMinimalSaga

func NewMinimalSaga(args IActionArguments) *MinimalSaga

NewMinimalSaga

func (*MinimalSaga) Clone

Clone returns a clone of the orchestrator and its execution state.

func (*MinimalSaga) CopyFunctions

func (*MinimalSaga) Execute

func (s *MinimalSaga) Execute(ctx context.Context) error

func (*MinimalSaga) GetCompensation

func (*MinimalSaga) GetTransaction

func (*MinimalSaga) Len

func (s *MinimalSaga) Len() int

func (*MinimalSaga) NewSaga

func (s *MinimalSaga) NewSaga(args IActionArguments) *MinimalSaga

func (*MinimalSaga) RegisterFunction

func (s *MinimalSaga) RegisterFunction(function ...ITransactionStep)

func (*MinimalSaga) RegisterFunctions

func (s *MinimalSaga) RegisterFunctions(function iter.Seq[ITransactionStep])

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL