Skip to main content

Welcome to Saga Bus

Saga Bus is a MassTransit-style saga orchestration library for TypeScript and Node.js. Build resilient, distributed workflows with ease.

Why Saga Bus?​

Modern applications often need to coordinate complex, multi-step processes across multiple services. Saga Bus provides:

  • Type-Safe DSL - Fluent builder API with full TypeScript inference
  • Multiple Transports - RabbitMQ, Kafka, SQS, Azure Service Bus, GCP Pub/Sub, Redis, NATS
  • Multiple Stores - PostgreSQL, MySQL, SQL Server, MongoDB, DynamoDB, Redis, SQLite, Prisma
  • Production-Ready Middleware - Logging, tracing, metrics, validation, idempotency, multi-tenancy
  • Framework Integrations - NestJS, Next.js, Express, Fastify, Hono

Quick Example​

import { createSagaMachine, createBus } from '@saga-bus/core';
import { RabbitMqTransport } from '@saga-bus/transport-rabbitmq';
import { PostgresSagaStore } from '@saga-bus/store-postgres';

// Define your saga with a fluent DSL
const orderSaga = createSagaMachine<OrderState, OrderMessages>()
.name('OrderSaga')
.correlate('OrderSubmitted', msg => msg.orderId, { canStart: true })
.correlate('*', msg => msg.orderId)
.initial<OrderSubmitted>(msg => ({
orderId: msg.orderId,
status: 'pending',
items: msg.items,
}))
.on('PaymentCaptured')
.handle(async (msg, state, ctx) => {
await ctx.publish({ type: 'ReserveInventory', orderId: state.orderId });
return { ...state, status: 'payment_captured' };
})
.on('InventoryReserved')
.handle(async (msg, state, ctx) => {
await ctx.publish({ type: 'CreateShipment', orderId: state.orderId });
return { ...state, status: 'inventory_reserved' };
})
.on('ShipmentCreated')
.handle(async (msg, state, ctx) => {
ctx.complete();
return { ...state, status: 'shipped' };
})
.build();

// Create and start the bus
const bus = createBus({
transport: new RabbitMqTransport({ url: 'amqp://localhost' }),
store: new PostgresSagaStore({ connectionString: process.env.DATABASE_URL }),
sagas: [{ definition: orderSaga }],
});

await bus.start();

Features at a Glance​

FeatureDescription
CorrelationLink messages to saga instances via correlation IDs
State GuardsConditional message handling with .when()
TimeoutsBuilt-in timeout tracking with automatic expiry messages
Optimistic ConcurrencyVersion-based conflict detection prevents lost updates
Distributed TracingW3C trace context propagation out of the box
CompensationHandle failures gracefully with compensating actions

Getting Started​

Ready to build your first saga? Follow our step-by-step guide:

  1. Installation - Add Saga Bus to your project
  2. Quick Start - Get running in 5 minutes
  3. Your First Saga - Build a complete workflow

Packages​

Saga Bus is organized as a monorepo with focused packages:

Core​

Transports​

Choose the message broker that fits your infrastructure:

PackageBroker
@saga-bus/transport-rabbitmqRabbitMQ
@saga-bus/transport-kafkaApache Kafka
@saga-bus/transport-sqsAWS SQS
@saga-bus/transport-azure-servicebusAzure Service Bus
@saga-bus/transport-gcp-pubsubGoogle Cloud Pub/Sub
@saga-bus/transport-redisRedis Streams
@saga-bus/transport-natsNATS JetStream
@saga-bus/transport-inmemoryIn-Memory (testing)

Stores​

Persist saga state in your preferred database:

PackageDatabase
@saga-bus/store-postgresPostgreSQL
@saga-bus/store-mysqlMySQL / MariaDB
@saga-bus/store-sqlserverSQL Server
@saga-bus/store-mongodbMongoDB
@saga-bus/store-dynamodbAWS DynamoDB
@saga-bus/store-redisRedis
@saga-bus/store-sqliteSQLite
@saga-bus/store-prismaPrisma ORM
@saga-bus/store-inmemoryIn-Memory (testing)

Middleware​

Add cross-cutting concerns:

PackagePurpose
@saga-bus/middleware-loggingStructured logging
@saga-bus/middleware-tracingOpenTelemetry tracing
@saga-bus/middleware-metricsPrometheus metrics
@saga-bus/middleware-validationZod schema validation
@saga-bus/middleware-idempotencyMessage deduplication
@saga-bus/middleware-tenantMulti-tenant isolation

Framework Integrations​

First-class support for popular frameworks:

PackageFramework
@saga-bus/nestjsNestJS
@saga-bus/nextjsNext.js
@saga-bus/expressExpress
@saga-bus/fastifyFastify
@saga-bus/honoHono

Need Help?​