NATS JetStream

Scale applications based on NATS JetStream.

Availability: v2.8+ Maintainer: Community

Warning

You are currently viewing v2.22 of the documentation and it is not the latest. For the most recent documentation, kindly click here.

Trigger Specification

This specification describes the nats-jetstream trigger for NATS JetStream.

triggers:
- type: nats-jetstream
  metadata:
    natsServerMonitoringEndpoint: "nats.nats.svc.cluster.local:8222"
    account: "$G"
    stream: "mystream"
    consumer: "pull_consumer"
    lagThreshold: "10"
    activationLagThreshold: "15"
    lagStrategy: "all"
    useHttps: "false"

Parameter list:

  • natsServerMonitoringEndpoint - Location of the NATS server monitoring endpoint.
  • account - Name of the NATS account. “$G” is default when no account is configured.
  • stream - Name of the JS stream within the account.
  • consumer - Name of the consumer for a given stream.
  • lagThreshold - Average target value to trigger scaling actions.
  • activationLagThreshold - Target value for activating the scaler. Learn more about activation here. (Default: 0, Optional)
  • lagStrategy - Which consumer messages count towards the lag. all counts undelivered messages plus messages that were delivered but not acknowledged yet. pending counts only undelivered messages. (Values: all, pending, Default: all, Optional)
  • useHttps - Specifies if the NATS server monitoring endpoint is using HTTPS. (Default: false, Optional)

With the default lagStrategy: all, the lag is num_pending + num_ack_pending, so a message stays counted while a consumer is working on it and until it is acknowledged. This also keeps counting messages that are waiting to be redelivered, which is what you want when a consumer leaves messages unacknowledged and relies on redelivery.

A message also stays in num_ack_pending while a consumer deliberately delays it, for example by negatively acknowledging it with a backoff (NakWithDelay) or by holding it for a long AckWait. No consumer is working on such a message and adding replicas cannot help it, yet it still counts towards the lag and can keep a workload scaled up for as long as the backoff lasts. Set lagStrategy: pending to count only undelivered messages in that case.

💡 WARNING: Messages waiting to be redelivered stay in num_ack_pending, so lagStrategy: pending reports no lag for them. With minReplicaCount: 0, the workload can scale to zero while those messages are still unacknowledged and nothing will activate it to process the redeliveries. Only use lagStrategy: pending when a consumer stays available to handle them, for example by setting minReplicaCount to at least 1.

Authentication Parameters

Some parameters of the JetStream details can be pulled from TriggerAuthentication objects:

  • natsServerMonitoringEndpoint - Location of the NATS Streaming monitoring endpoint.
  • account - Name of the NATS account. “$G” is default when no account is configured.

Example

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: nats-jetstream-scaledobject
  namespace: nats-jetstream
spec:
  pollingInterval: 3   # Optional. Default: 30 seconds
  cooldownPeriod: 10   # Optional. Default: 300 seconds
  minReplicaCount: 0   # Optional. Default: 0
  maxReplicaCount: 2   # Optional. Default: 100
  scaleTargetRef:
    name: sub
  triggers:
  - type: nats-jetstream
    metadata:
      natsServerMonitoringEndpoint: "nats.nats.svc.cluster.local:8222"
      account: "$G"
      stream: "mystream"
      consumer: "pull_consumer"
      lagThreshold: "10"
      activationLagThreshold: "15"
      useHttps: "false"