stack-nats
Skill0 starsChanged 5 months agoApache-2.0View on GitHub
NATS.io messaging patterns for agents working in projects whose docs/TECH_STACK.md lists nats. Load when implementing, testing, or reviewing event-driven code.
In this repository17 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- flow-adoptionSkill
- flow-legacy-migrationSkill
- stack-awsSkill
- stack-azureSkill
- stack-design-systemSkill
- stack-gcpSkill
- stack-javaSkill
- stack-kafkaSkill
- stack-mongodbSkill
- stack-nestjsSkill
- stack-nextjs-reactSkill
- stack-nxSkill
- stack-playwrightSkill
- stack-postgresqlSkill
- stack-quarkusSkill
- stack-react-nativeSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 43
Every durable consumer declares max-deliver plus a dead-letter strategy (handle the MAX_DELIVERIES advisory or route to an explicit DLQ …
- Very specific: line 99
All rows must pass before commit per .claude/rules/LIVE_VALIDATION.md.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Stack: NATS
NATS (JetStream) is the default broker of .claude/docs/DEFAULT_TARGET_ARCHITECTURE.md §4 — always ONE adapter behind a messaging port, never a direct module dependency. Where the host's docs/ARCHITECTURE.md declares different conventions, the host docs win.
When this applies
Detection markers (any of):
docs/TECH_STACK.md## Detected stackslistsnats- Dependencies:
nats(nats.js client),@nestjs/microservicesconfigured with the NATS transport - Config:
nats://URLs in env/config files - Files: a
natsservice indocker-compose.yml(typically with-js/--jetstream), JetStream stream/consumer definitions committed to the repo
Implementation patterns
THE PORT RULE (non-negotiable)
Modules depend on a messaging port — an abstract contract owned by the module's PURE core (domain/ on NestJS, application/ on Quarkus — per the backend stack skill). The NATS adapter is one implementation:
// PURE core (domain/ on NestJS, application/ on Quarkus)
export abstract class EventBus {
abstract publish(event: DomainEvent): Promise<void>
}
// adapter layer (adapters/ on NestJS, infrastructure/ on Quarkus); the JetStream client lives ONLY here
export class NatsEventBus extends EventBus { /* ... */ }- Domain/application code NEVER imports a broker client. A
natsimport outside the adapter directory is a review BLOCKER. - Swapping brokers (premise "SNS/SQS instead of NATS") binds the
stack-awsadapters at the SAME port — adapters change, modules don't.
Subject taxonomy
<domain>.<entity>.<event>, past-tense facts (R-ARCH-09):billing.invoice.created— never imperative (billing.invoice.createis RPC in disguise).- Versioned: suffix a version token when the payload contract changes incompatibly —
billing.invoice.created.v2. Consumers pin the versions they understand; producers run old and new side by side during migration. - Multi-tenant: the tenant id travels in a subject token (
billing.<tenantId>.invoice.created) or in headers — NEVER payload-only. Subject/header placement lets account permissions, subject filtering, and per-tenant consumers work without deserializing every message.
JetStream streams & durable consumers
- One stream per domain (
BILLINGcapturingbilling.>); stream retention/limits are config committed to the repo and applied idempotently at boot/deploy — never hand-created in a running broker. - Each consuming module gets a durable consumer named for it (
billing-notifications); ephemeral consumers are for ad-hoc inspection only. - Every durable consumer declares
max-deliverplus a dead-letter strategy (handle theMAX_DELIVERIESadvisory or route to an explicit DLQ subject). A consumer without one recycles poison messages forever.
Ack & redelivery
AckExplicit; ack ONLY after side effects are durably committed — ack-then-process loses messages on crash.- Transient failure →
nakwith backoff delay. Permanently unprocessable message →termand route to dead-letter handling. Never swallow-and-ack a failure.
Idempotent handlers (mandatory)
- At-least-once delivery is the contract — every consumer dedupes, by the
Nats-Msg-Idheader (JetStream's publish dedupe window) and/or a persisted business id (R-ARCH-11). The dedupe window is finite: spec-critical consumers persist processed ids. - A duplicate maps to a typed replay outcome (no-op), never an error.
Outbox for DB-write + publish atomicity
Never publish inside an uncommitted transaction without an outbox. In-transaction publish announces state that may roll back; naive post-commit publish is lost if the process dies between commit and publish. When the spec needs the event reliably:
TX: [ write domain rows + insert outbox row ] → COMMIT
relay: poll/tail outbox → publish (Nats-Msg-Id = outbox row id) → mark sentThe outbox row id doubles as the dedupe id, making the relay safely retryable. Plain post-commit fire-and-forget is acceptable ONLY for events the spec marks lossy-tolerant (telemetry, advisory).
Request-reply vs events
- Request-reply (core NATS): the caller needs the answer NOW to proceed; 1:1; no durability — a synchronous query over the broker.
- Events (JetStream): durable facts, fan-out, producers don't know consumers.
- A "consumer" that exists solely to execute a command for one producer is RPC in disguise — use request-reply or a direct module-public call instead of a fake event.
Testing on this stack
- Integration: real NATS via Testcontainers (
nats:2-alpinewith-js) or the composenatsservice — never a mocked broker client (R-TESTI-01 spirit: mocks stay green while stream/consumer config is wrong). - Contract tests on subjects: the producer test asserts the published subject, headers, and payload schema (Zod-parse what actually landed on the stream); the consumer test feeds the documented shape and asserts the side effect. Both sides test against the declared subject contract, not against each other.
- Double-delivery idempotency assertions: deliver the same message twice (same
Nats-Msg-Id/business id), assert EXACTLY one side effect (R-TESTI-02 adapted). Mandatory for every consumer. - Tests own their streams/consumers: unique stream/durable names per suite (or purge in teardown) so parallel suites never share cursors.
Spec validation checks
Stack-specific checks the spec validators (spec-validate-lead-engineer, on SPEC_<FEATURE>.md §4–6) delegate here:
| Check | Severity |
Event consumer without a declared idempotency strategy (Nats-Msg-Id/business-id dedupe) | BLOCKER |
| Publish inside an uncommitted DB transaction without an outbox | BLOCKER |
| Module publishing/subscribing around the messaging port (broker client import outside the adapter) | BLOCKER |
Durable consumer without a dead-letter / max-deliver strategy | WARNING |
| Unversioned subjects on contracts expected to evolve | WARNING |
| Tenant id carried payload-only (not in subject token or headers) | WARNING |
Live validation commands
R-VAL-01 obligations for messaging changes — exact invocations live in docs/DEV_SETUP.md; the rows below are the obligations:
| Check | Typical shape |
| Broker up in compose | docker compose up -d nats (or the project's make target) per DEV_SETUP |
| Streams/consumers registered as declared | nats stream ls / nats consumer ls <STREAM> against the local broker |
| Run affected integration tests (real NATS) | test target per DEV_SETUP |
| Double-deliver smoke for touched consumers | publish the same message twice (nats pub with the same Nats-Msg-Id); observe exactly one side effect |
All rows must pass before commit per .claude/rules/LIVE_VALIDATION.md.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

