Skip to main content

NATS JetStream

NATS JetStream is supported as an Agent transport only: the Master database must still use a SQL provider. Use it for ultra-low latency, high fan-out, or ephemeral workloads.

Install​

dotnet add package JobMaster.NatsJetStream

Configuration​

builder.Services.AddJobMasterCluster(config =>
{
config.ClusterId("My-Cluster")
.UsePostgresForMaster("Host=...;Database=...;Username=...;Password=...");

config.AddAgentConnectionConfig("Nats-1")
.UseNatsJetStream("nats://localhost:4222");
});
warning

When using NATS as the Agent transport, TransientThreshold is capped at 5 minutes. JobMaster validates this at startup and refuses to start a NATS-backed cluster configured above the cap.

TransientThreshold and NATS capacity​

Jobs onboarded ahead of their execution time sit in the NATS stream as unacknowledged messages until they're due. As you increase TransientThreshold, JobMaster increases MaxAckPending on the consumer to match: more jobs can be held ahead of time, but each one occupies broker capacity for the whole wait, not just while it's running. That trade-off is why the setting is capped for this transport.

**Not sure what to set?** `TransientThreshold(TimeSpan.FromMinutes(2))` is a good starting point for NATS: enough look-ahead to keep Master DB scan frequency reasonable, without parking too many jobs in the stream at once. If your workload needs a longer look-ahead than 5 minutes, use a SQL-backed Agent transport for it instead; see Database vs. Message Broker Isolation.

Message retention and replicas​

Each NATS agent connection uses one JetStream stream. JobMaster creates it, and at every startup brings an existing one to the settings below (logged when it changes):

config.AddAgentConnectionConfig("Nats-1")
.UseNatsJetStream("nats://localhost:4222", streamMaxAge: TimeSpan.FromDays(14), streamNumReplicas: 3);
  • streamMaxAge (default 30 days): how long NATS keeps a message, handled or not. A message still waiting after that is deleted, for example when every worker of the connection has been down that long. Keep it longer than any outage you want to survive.
  • streamNumReplicas (default 1): how many servers of your NATS cluster keep a copy of the messages. With 1, losing that server's disk loses its messages; use 3 on a 3-node NATS cluster. NATS rejects a value it doesn't support (more than 5, or more than its number of servers) when it creates the stream. Updating an existing stream on a single, non-clustered server is accepted, but there's still only one copy.

If processes using the same agent connection set different values, the last one to start applies its own, so set them the same everywhere.

Authentication & TLS​

Pass auth and TLS settings directly on UseNatsJetStream, or via ConfigFromJson's per-connection connectionOptions dictionary:

config.AddAgentConnectionConfig("Nats-1")
.UseNatsJetStream("nats-1:4222", userName: "svc", password: "secret");

// Multi-server cluster connection, one set of credentials per server
config.AddAgentConnectionConfig("Nats-1")
.UseNatsJetStream(new[]
{
("nats-1:4222", "svc", "secret"),
("nats-2:4222", "svc", "secret"),
});

Supported connectionOptions keys when configuring via JSON:

  • Auth: username, password, token, credentialsFile, nkey, jwt
  • TLS: tlsCertBundleFile, tlsCertBundleFilePassword, tlsCaFile, tlsInsecureSkipVerify, tlsMode
  • Stream: streamMaxAge (a TimeSpan, e.g. "14.00:00:00"), streamNumReplicas (e.g. "3")

An unrecognized key, or an invalid tlsMode, streamMaxAge or streamNumReplicas value, throws at startup.