@nodii/saga
v0.14.0
Published
Saga orchestration library for the Nodii microservice stack — createSaga + step + stepParallel + cross-saga signals + sagaContext interceptor + SagaAdminService factory + @sagaCallable discipline tag + real PostgresSagaStateStore + RedisSignalBus + Patter
Readme
@nodii/saga
Placeholder package at v0.0.1. Implementation lands at v0.1.0 per the locked feature_doc.
Spec: https://planning.dev.nucleus-cloud.in/api/v1/feature-docs?serviceId=nodii-libs&docKey=saga
Retention: delete settled sagas after 90 days
Sagas that ended completed, compensated, compensated_manual or cancelled are hard-deleted 90 days after their last activity, with their saga_outbox rows. Every other status stays until something resolves it, as does a parent with a child row left and any saga with a kept ancestor. Each batch is cancelled after a time limit (default 60 s). Wire the job once at boot, on the owner pool (saga_state is owner-only):
import { makeSagaRetentionJob, runSagaRetentionLoop } from "@nodii/saga";
runSagaRetentionLoop(
makeSagaRetentionJob({ sql: ownerSql, leaseRedis: redis, service: "billing" }),
);It runs daily as a singleton under the lease nodii:lease:<service>:saga-retention, and Postgres checks the lease's fencing token inside every delete batch, so a replica that lost its lease cannot delete beside the one that took it over. A batch whose outcome is unknown keeps the lease until its transaction ends; the job learns that from the client, or by asking Postgres from another connection, and keeps the lease (warning about once a minute) while it cannot reach Postgres. Postgres keeps the highest token per lease key in saga_retention_fence. If the Redis fence counter is reset (FLUSHALL, a failover without persistence, a new Redis), every run then ends fence_lost and the job logs a warning with fence_counter_reset: true; to recover, run DELETE FROM saga_retention_fence WHERE scope = '<lease key>'. batchTimeoutMs (default 20 s) plus its client grace must be below the lease TTL (default 30 s). Defaults: retentionDays: 90, batchSize: 500 (one transaction per batch), maxBatchesPerRun: 1000, intervalMs: 86_400_000. If you have a scheduler, hand it job.name, job.intervalMs and job.handler instead of calling runSagaRetentionLoop. onRun gets the counts of each run, for your metric. The job writes no audit event.
Services that copy the saga SQL into their own migration runner (Flyway, golang-migrate, alembic): the retention DDL ships as a new file, ts/saga/src/migrations/002-saga-retention.sql, to run after 001-saga-state.sql. 001 is unchanged, so a runner that has already applied it is not affected. Without 002, every retention run ends store_error (saga_retention_fence_admit does not exist). applySagaMigrations (Python apply_saga_migrations, Go ApplySagaMigrations) runs both.
Only the saga_outbox table is purged. Known gap (RETENTION-FIX-81): the purge deletes only outbox rows that belong to a saga it deletes. Two kinds of saga_outbox row are never deleted: rows the participant worker writes under a remote orchestrator's saga id (no local saga row), and a deleted saga's rows written after the cutoff. Until RETENTION-FIX-81 lands, prune those yourself if they matter. There is no automatic orphan pass yet, because saga tables can share saga_outbox, and a check against one table would delete another table's live saga rows. A participant wired with a custom outboxTable keeps its rows there; prune that table yourself.
The saga migration gives saga_state its retention index and backfills its completed_at. A store on another table (tableName) needs the same: after applySagaMigrations, run sagaRetentionTableMigrationSql("<your table>") once (idempotent); it creates <table>_retention_idx and backfills that table. Saga ids must be unique across every saga table that shares saga_outbox: the purge deletes the outbox rows of each deleted saga by saga_id, whatever table wrote them.
The saga migration adds the partial index saga_state_retention_idx for this scan. A plain CREATE INDEX blocks writes to saga_state while it builds, so on a large table create it first with CREATE INDEX CONCURRENTLY IF NOT EXISTS saga_state_retention_idx ON saga_state (completed_at, id) WHERE status IN ('completed', 'compensated', 'compensated_manual', 'cancelled'); the migration then skips it. If that build fails, drop the invalid index it leaves and build it again.
