npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

saga-transaction-manager

v1.0.1

Published

Transaction management in Saga model

Downloads

5

Readme

English 中文

summary

saga-transaction-manager is a Saga model distributed transaction framework that can easily handle distributed transactions

Introduction to the Saga Transaction Pattern

Saga = Long Live Transaction (LLT)

  • Paper https://www.cs.cornell.edu/andru/cs711/2002fa/reading/sagas.pdf Saga pattern

  • Core Idea

A long transaction is divided into multiple local transactions, each with a corresponding compensation transaction. When a long transaction fails, the compensation transactions are executed in reverse order to roll back the entire long transaction.

  • Applicable Scenarios

Long business processes, complex business processes Participants include other companies or legacy system services Need to ensure eventual consistency of the transaction

  • Implementation Methods

There are two common Saga implementation methods: Choreography and Orchestration. This project uses the Saga Orchestration pattern.

  1. Choreography

Each local transaction publishes domain events in other services to trigger local transactions in those services. The responsibility is distributed among the Saga participants. Each service is independent and has its own business logic, and needs to ensure whether to continue the next process.

  1. Orchestration

The Saga orchestrator handles all transactions and instructs the participants to perform the appropriate operations based on the events. Simpler implementation

Quick Start

Run the example

From the examples/serviceProxy directory:

  1. Implement the ISagaTransactionService interface for local transactions and compensation transactions.
import { ISagaTransactionService } from 'saga-transaction-manager';

// Implement a short transaction
class CreateUserTransaction implements ISagaTransactionService {
  execute() {
    // Business logic for creating a user
    return Promise.resolve();
  }
  compensate() {
    // Business logic for rolling back the user creation operation
    return Promise.resolve();
  }
}
class UpdateProfileTransaction implements ISagaTransactionService {
  ...
}
class SendWelcomeEmailTransaction implements ISagaTransactionService {
  ...
}
  1. Use TmSequentialExecution to orchestrate the transactions.
import { TmSequentialExecution } from 'saga-transaction-manager';

// Orchestrate the long transaction
const transactionManager = new TmSequentialExecution();
transactionManager.addTransaction(new CreateUserTransaction());
transactionManager.addTransaction(new UpdateProfileTransaction());
transactionManager.addTransaction(new SendWelcomeEmailTransaction());

// Execute the long transaction, compensation transactions will be automatically executed on failure
transactionManager.execute();

  • Orchestrator with Context

For long transactions that require storing shared variables, you can use the SagaServiceWithContext abstract class and TmSequentialExecutionWithContext to orchestrate the transactions.

import { SagaServiceWithContext } from 'saga-transaction-manager';

class CreateUserTransaction extends SagaServiceWithContext {
  constructor() {
    super();
  }
  execute() {
    // set shared variables
    this.context?.setVariable('A', 'valueA');
  }
  compensate() {
    // get shared variables
    this.context?.getVariable('A')
  }
}
import { TmSequentialExecutionWithContext } from 'saga-transaction-manager';

// Orchestrate the long transaction
const transactionManagerWithCtx = new TmSequentialExecutionWithContext();
transactionManagerWithCtx.addTransaction(new CreateUserTransaction());
transactionManagerWithCtx.addTransaction(new UpdateProfileTransaction());
transactionManagerWithCtx.addTransaction(new SendWelcomeEmailTransaction());

// Execute the long transaction, compensation transactions will be automatically executed on failure
transactionManagerWithCtx.execute();

Limitations

  1. Cannot guarantee isolation
  2. No control over hanging: If the original service times out and the compensation service executes, the original service may still complete.
  • Implementation Suggestions

Ensure that the service and compensation service are idempotent.

todo

  1. Actor model retry mechanism
  2. Annotation-based implementation of Saga
service1(){}

@SagaCompensiable(method='service1')
compensiteFunction(){}

@SagaTransactional
process(){
  service1()
  service2()
}
  1. Support the use of message queues to store orchestration information
  2. Implement Saga using a state machine engine, Orchestrator is extensible