@multisystemsuite/deadlock-guard
v2.1.0
Published
Deadlock detection, retry, and lock order analysis
Readme
@multisystemsuite/deadlock-guard
Deadlock detection and retry with exponential backoff, lock-order analysis, metrics, and Prometheus export.
Version: 1.0.1 · License: MIT
npm readme: @multisystemsuite/deadlock-guard on npm
Installation
pnpm add @multisystemsuite/deadlock-guardQuick start
import { DeadlockGuard } from '@multisystemsuite/deadlock-guard';
const deadlockGuard = new DeadlockGuard({
maxRetries: 5,
retryDelayMs: 50,
});
await deadlockGuard.execute(async () => {
await inventory.update();
}, ['inventory', 'orders']);Configuration
| Option | Default | Description |
|--------|---------|-------------|
| maxRetries | 5 | Max retry attempts after deadlock |
| retryDelayMs | 50 | Base delay; doubles each attempt |
| onDeadlock | — | (error, attempt) => void callback |
API reference
execute<T>(fn, resources?): Promise<T>
Runs fn with deadlock retry. Optional resources array enforces consistent lock order.
Throws if lock order violates prior acquisitions (prevents deadlock cycles).
analyzeLockOrder(resources)
const { valid, suggestion } = guard.analyzeLockOrder(['orders', 'inventory']);
// suggestion: "Acquire locks in order: inventory -> orders"Metrics
| Method | Description |
|--------|-------------|
| getMetrics() | { attempts, deadlocks, successes, alerts } |
| toPrometheusMetrics() | MetricPoint[] for exporters |
| resetMetrics() | Clear counters |
Prometheus metric names:
db_deadlock_attempts_totaldb_deadlock_totaldb_deadlock_success_totaldb_deadlock_alerts_total
Best practices
- Always pass
resourcesin alphabetical or schema-defined order. - Keep transactions short inside
execute. - Log
onDeadlockfor alerting when retries are frequent.
Related packages
See the @multisystemsuite org on npm.
npm
- Package: @multisystemsuite/deadlock-guard
- Install:
npm install @multisystemsuite/deadlock-guard
