agentleFS
TeddyAlbinaTeddyAlbina/skills/nats/SKILL.md

nats

Skill2 starsChanged 29 days agoMITView on GitHub

NATS is a simple, secure, and high-performance open source messaging system for cloud native applications, IoT, and microservices. This skill covers deploying and operating NATS Server, JetStream persistence, clustering, leaf nodes, and gateways on Kubernetes and bare metal. It provides configuration patterns, security hardening, troubleshooting, and operational guidance for the NATS ecosystem. Activate for NATS server configuration, JetStream streams/consumers, Helm chart deployment, clustering, security setup, or performance tuning.

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Horizontal: Add servers to the cluster.

    line 261

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

NATS

When to Use This Skill

Invoke this skill when the user mentions or asks about:

  • Deploying NATS on Kubernetes, Docker, VMs, or bare metal.
  • Configuring NATS Server (nats-server, nats.conf, CLI flags).
  • JetStream (streams, consumers, Key-Value store, Object Store, persistence, replication).
  • Core NATS (pub/sub, request/reply, queue groups, subject hierarchies).
  • Clustering / High Availability (NATS clusters, super-clusters, leaf nodes, gateways).
  • Security (TLS, JWT, NKEY, username/password, auth callout, authorization).
  • Helm charts (nats/nats, nats/nack, nats/surveyor).
  • Monitoring / Observability (Prometheus metrics, NATS Surveyor, server statistics).
  • Troubleshooting (slow consumers, connection issues, stream lag, RAFT quorum).
  • Client SDKs (Go, Rust, Python, Node.js, C#, Java, etc.) — primarily from an infrastructure/operations perspective.

Core Concepts

NATS Server (nats-server)

A single, lightweight binary (< 20 MB) with no external dependencies. It supports:

  • Core NATS: At-most-once pub/sub messaging with millions of messages per second.
  • Request/Reply: Synchronous or asynchronous RPC over subjects.
  • Queue Groups: Load-balance message delivery across a group of subscribers.
  • Subject Hierarchies: Dot-separated subjects (e.g., orders.us.east) with wildcard support (*, >).

JetStream (Persistence Layer)

Built into nats-server. Provides at-least-once and exactly-once delivery, temporal decoupling, and distributed storage.

FeatureDescription
StreamsNamed persistent message logs with retention policies and limits.
ConsumersViews on a stream. Push (fast, ordered) or Pull (scalable, acked, batched).
Retention Policieslimits (default replay), work queue (exactly-once consumption), interest (retain while consumers exist).
Discard Policiesdiscard old (delete oldest) or discard new (reject new messages) when limits are reached.
Replication (R)R=1 (fast, no HA) to R=5 (tolerate 2 server loss). R=3 is the recommended balance.
Key-Value StoreAtomic operations, watchers, versioning, TTL. Built on top of JetStream.
Object StoreChunked file storage with versioning. Built on top of JetStream.
Mirroring / SourcingReplicate streams across JetStream domains for DR or aggregation.

Consistency Model: JetStream uses a NATS-optimized RAFT algorithm. Writes are linearizable; reads are effectively serializable. For the strongest read consistency, send get requests to the stream leader.

Topologies

TopologyUse Case
Single ServerDev/test, edge devices.
ClusterHA production. 3+ nodes with RAFT for JetStream metadata and streams.
Leaf NodeExtend a cluster into a remote VPC, edge location, or K8s namespace. Transparent proxy.
GatewayConnect clusters across regions/clouds. Filters traffic, enables geo-replication and DR.
Super-ClusterMultiple clusters connected via gateways forming a global mesh.

Deploying on Kubernetes

Quick Start with Helm

The official Helm charts are maintained at nats-io/k8s. See the local reference: references/kubernetes/README.md.

helm repo add nats https://nats-io.github.io/k8s/helm/charts/
helm repo update
helm install my-nats nats/nats

Primary Charts

ChartPurposeReference
natsNATS server (core + JetStream)references/kubernetes/CLAUDE.md
nackJetStream Controller (CRDs for K8s)references/kubernetes/CLAUDE.md
surveyorPrometheus monitoring for NATSreferences/kubernetes/README.md

Chart Architecture (nats)

The nats chart uses a unique loadMergePatch pattern:

  1. templates/ are thin wrappers calling nats.loadMergePatch.
  2. files/ contains the actual YAML definitions, loaded via .Files.Get.
  3. Users can customize almost any resource via merge: {} (deep merge) and patch: [] (RFC 6902 JSON Patch) in values.yaml.

Key values structure:

  • config.*: NATS server config (maps to nats.conf sections).
    • Sub-keys: nats, cluster, jetstream, leafnodes, websocket, mqtt, gateway, monitor, profiling, resolver.
    • Each protocol has enabled, port, tls.
    • Each section supports merge: {} and patch: [].
  • container.*: Server container spec (image, ports, env, resources).
  • reloader.*: Config reloader sidecar.
  • promExporter.*: Prometheus exporter sidecar.
  • statefulSet.*, podTemplate.*, headlessService.*, configMap.*: K8s resource customization.
  • natsBox.*: NATS Box deployment (CLI tools with pre-configured contexts).
  • extraResources: Arbitrary additional K8s resources.

Config merge special syntax:

  • Strings wrapped in << >> are unquoted (for NATS size units like 1GB or env vars like $TOKEN).
  • Keys ending in $include become NATS include directives.

NACK (JetStream Controller)

NACK allows managing JetStream resources via Kubernetes CRDs (Stream, Consumer, Account, etc.).

  • CRDs are in crds/crds.yml and auto-updated.
  • Helper prefix in templates: jsc.*.
  • Has useLegacyNames toggle for K8s label compatibility.

Local Development / Validation

Render and lint locally without installing:

# Render templates
helm template my-nats helm/charts/nats
helm template my-nats helm/charts/nats -f custom.yaml

# Lint with chart-testing
ct lint --all --chart-dirs helm/charts \
  --excluded-charts nats-account-server,nats-kafka,nats-operator,surveyor \
  --validate-maintainers=false

# Run Go template tests
cd helm/charts/nats/test && go test

See full commands in references/kubernetes/CLAUDE.md.

Server Configuration

Minimal nats.conf

# Single node with JetStream
jetstream {
  store_dir: "/data/jetstream"
}

Cluster with JetStream

server_name: nats-1
jetstream {
  store_dir: "/data/jetstream"
}
cluster {
  name: my-cluster
  listen: 0.0.0.0:6222
  routes: [
    nats-route://nats-2:6222
    nats-route://nats-3:6222
  ]
}

Protocols and Ports

ProtocolDefault PortConfig SectionNotes
NATS Client4222natsCore TCP port.
Cluster Routes6222clusterInter-server routing.
Gateway7222gatewayInter-cluster / super-cluster.
Leaf Node7422leafnodesAccept leaf node connections.
WebSocket8080websocketWebSocket support.
MQTT1883mqttMQTT 3.1.1 bridge.
Monitor / HTTP8222monitorServer stats, health, debug vars.
Profiling-profilingGo pprof endpoint.

Security

NATS provides multi-level security with account isolation.

Authentication Methods

MethodDescriptionReference
TokenGlobal shared token.docs
Username/PasswordPlain text or bcrypt-hashed.docs
TLS (mTLS)Client certificate authentication.docs
NKEYEd25519 public key with server challenge.docs
Decentralized JWTZero-trust with operators, accounts, and users.docs

Authorization

  • The authorization block configures both auth and per-user permissions.
  • Each user can have permissions restricting publish/subscribe subjects.
  • Accounts provide subject namespace isolation; cross-account traffic uses exports and imports.

TLS

Almost every protocol section (cluster, leafnodes, websocket, mqtt, gateway, monitor) supports tls:

cluster {
  port: 6222
  tls {
    cert_file: "/certs/server.pem"
    key_file: "/certs/server.key"
    ca_file: "/certs/ca.pem"
  }
}

In the Helm chart, TLS is configured via config.*.tls.enabled and secretName.

Operations

Monitoring

  • HTTP Monitor (monitor port 8222): /varz, /connz, /routez, /gatewayz, /leafz, /jsz, /healthz, /subsz.
  • Prometheus Exporter: Sidecar in Helm chart (promExporter.enabled: true).
  • NATS Surveyor: Standalone tool for cluster-wide metrics. See surveyor Helm chart.
  • Server Statistics: Via nats server info, nats server report, nats server watch (NATS CLI).

JetStream Operations

# Check stream info
nats stream info ORDERS

# Check consumer info
nats consumer info ORDERS NEW

# Server report for JetStream
nats server report jetstream

# Evacuate a server (drain streams/consumers before maintenance)
nats server evacuate <server-name>

Durability and fsync

JetStream file stores flush to the OS synchronously but do not fsync on every write by default. The sync_interval (default 2 minutes) controls how often fsync runs.

  • Default: Balanced performance and risk. Suitable for multi-AZ deployments with R=3.
  • sync_interval: always: Strongest durability, slowest performance. Use for critical financial or compliance data.
  • Mitigation: Lower sync_interval or use placement tags to route critical streams to a dedicated high-durability cluster.

See JetStream sync documentation for failure scenarios.

Troubleshooting

SymptomLikely CauseAction
Slow ConsumersClient not reading fast enough; server is buffering.Scale consumers or increase buffer. Check slow_consumer_threshold.
JetStream publish timeoutStream leader unreachable or quorum lost.Check cluster health, RAFT status (nats server report raft), network partitions.
Message duplicationNetwork blip, consumer ack lost, or publisher retry.Use exactly-once semantics (dedup headers, double-ack consumers).
Stream lag / high redeliverySlow processing, consumer offline, or nack loop.Inspect consumer stats, check app logs, scale pull consumers.
Connection refusedWrong port, TLS required but not used, auth failure.Verify client URL, tls settings, and authorization block.
Cluster not formingRoute URLs misconfigured, firewall blocking gossip, or server_name duplicates.Verify cluster.routes, server_name uniqueness, and port reachability.
Disk full / stream limitsRetention policy or max bytes/message count reached.Increase limits, change discard policy, or age out old messages.

Scaling

  • Horizontal: Add servers to the cluster. JetStream will auto-rebalance RAFT groups over time. For immediate effect, evacuate and restart.
  • Vertical: Increase CPU/disk for JetStream nodes. SSD/NVMe is strongly recommended for file stores.
  • Placement Tags: Pin streams to specific servers using server_tags and stream placement for topology-aware durability.

Developer Quick Reference

NATS CLI (nats)

The nats CLI is the primary operational tool.

# Pub/Sub
nats pub orders.new "hello"
nats sub orders.new

# Request/Reply
nats req orders.service "request" --wait 5s

# JetStream
nats stream add ORDERS --subjects "orders.*" --storage file --replicas 3
nats consumer add ORDERS PROCESSOR --pull --ack explicit
nats consumer next ORDERS PROCESSOR

# KV
nats kv add CONFIG
nats kv put CONFIG key value
nats kv get CONFIG key

SDKs

Official clients exist for Go, Rust, Python, Node.js, C#, Java, C, Elixir, and more. See NATS Clients.

References

Official Documentation

Local References

  • K8s Helm Charts README: references/kubernetes/README.md
  • K8s Chart Development (CLAUDE.md): references/kubernetes/CLAUDE.md — Build commands, architecture, values structure, CI workflows, and conventions.
  • Dependencies: references/kubernetes/dependencies.md
  • Helm Chart Dir: references/kubernetes/helm/
  • Kine + NATS Example: references/kubernetes/kine-nats/

External Projects

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.