> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/tposs/6.19/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/tposs/6.19/installation/nats.md).

# NATS configuration

NATS is a crucial component for [global locking](https://docs.cennso.com/components/tposs/6.19/configuration/session.md#session-handler-locking) in the TPOSS cluster. If you use **sessions.global_lock.algorithm** set to the NATS service specified by one of the `nats-services.{SERVICE_NAME}` services, you need a [NATS](https://nats.io/) instance for TPOSS to communicate with. You may use your own NATS installation or the one that TPOSS Helm chart provides as a dependency. To use NATS provided by TPOSS, use this configuration option:

```yaml
nats:
  enabled: true
```

NATS included in the TPOSS Helm chart is already configured in the most optimal way. The following sections provide recommended configuration options and their values for NATS that you may re-use for your own installation. The whole list of the configuration options of the NATS chart and their default values you may find in the [Configuration parameters](#configuration-parameters) section and in the [official documentation](https://github.com/nats-io/k8s).

## Recommended configuration

> [!CAUTION]
> The default values for the configuration options in this section are thoroughly tested and confirmed to work reliably. Changing these options may affect system performance or stability. Proceed with caution, as any changes are made at your own risk.

All the configuration parameters related to NATS are located under the `nats` section in the TPOSS Helm chart.

The next sections provide information about common configuration options of the NATS deployment, which can be deployed from the TPOSS Helm chart.

### NATS image and secrets configuration

You can use the following configuration options to configure the NATS [image](https://kubernetes.io/docs/concepts/containers/images) version, the secrets to pull the image, and the [image pull policy](https://kubernetes.io/docs/concepts/containers/images/#image-pull-policy):

```yaml
nats:
  global:
    image:
      pullSecretNames: []

  reloader:
    enabled: true
    image:
      repository: registry.cennso.com/cennso/natsio/nats-server-config-reloader
      tag: 0.17.1

  container:
    image:
      repository: registry.cennso.com/cennso/nats
      tag: 2.11.6-alpine
      pullPolicy: IfNotPresent
```

### NATS resources configuration

You can use the following configuration options to configure Kubernetes [resources](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) needed by the NATS containers:

```yaml
nats:
   container:
     requests:
      cpu: "1"
      memory: "500Mi"
    limits:
      cpu: "1"
      memory: "500Mi"
```

> [!CAUTION]
> TPOSS needs [NATS JetStream](https://docs.nats.io/nats-concepts/jetstream) enabled to work properly. It is enabled by default in the TPOSS chart with the `nats.config.jetstream.enabled: true` configuration option.

### Persistence configuration

As the global lock based on NATS does not need persistence, you should enable only memory storage for NATS using the following configuration options:

```yaml
nats:
  config:
    jetstream:
      enabled: true
      memoryStore:
        enabled: true
        maxSize: 1Gi
      fileStore:
        enabled: false
```

For the existing use cases, TPOSS only needs around 50 MB of memory for NATS. However, it's recommended to allocate more, as some commands run by the [NATS CLI](https://github.com/nats-io/natscli) tool can cause memory spikes.

### NATS server monitoring configuration

You can use the following configuration options to enable NATS server monitoring and see its metrics:

```yaml
nats:
  promExporter:
    enabled: true
    image:
      repository: registry.cennso.com/cennso/natsio/prometheus-nats-exporter
      tag: 0.17.2
```

If this configuration option is set to `true`, TPOSS installs the [ServiceMonitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/developer/getting-started.md#using-servicemonitors), which defines settings for how metrics should be scraped. You can specify [labels](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/) assigned to this `ServiceMonitor` using these configuration options:

```yaml
nats:
  global:
    labels:
      nats-monitor: tposs
```

The TPOSS Helm chart also provides the Grafana dashboard located in the `dashboards/nats.json`. You can use it to display the NATS server metrics.

### Accessing NATS server

You can use the following configuration option to enable the [nats-box](https://github.com/nats-io/nats-box) sidecar container that contains useful utilities to access the NATS server:

```yaml
nats:
   natsBox:
     enabled: false
```

## Updating NATS

If you need to upgrade the NATS server (for version or configuration updates), there are several ways to do it. The best approach is to update NATS during a maintenance window to avoid any impact on TPOSS user sessions. If it's not possible to stop incoming requests and you must upgrade NATS while TPOSS is handling traffic, you can either update the NATS instance, which will cause some rejected requests, or follow these steps to minimize the impact:

1. Deploy the new NATS server with the updated version or configuration while the old NATS server is running.
2. Update the **nats-services.{SERVICE_NAME}.server** configuration option in TPOSS to point to the new NATS server.
3. Redeploy TPOSS with the updated configuration. Read the [installation guide](https://docs.cennso.com/components/tposs/6.19/installation/installation.md) to learn more.
4. Once TPOSS is running with the new configuration, terminate the old NATS server.

> [!CAUTION]
> During the transition between the old and new NATS servers, TPOSS cannot send locking information. If TPOSS fails to send locking information while processing a session request, the request is rejected with an error.

This method can cause lock inconsistencies during the NATS update. It may lead to TPOSS handling two requests for the same identifier, which results in unpredictable behaviour. However, this is rare and can be avoided by performing the update during low-traffic periods.

## Configuration parameters

The following table lists configurable parameters for the [NATS](https://nats.io/) deployment:

| Key | Type | Description | Default |
|---|---|---|---|
| nats.enabled | bool | Enables NATS deployment. | `true` |
| nats.global.labels | object | Set of [labels](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/) to apply to the [Service](https://kubernetes.io/docs/concepts/services-networking/service/) and [ServiceMonitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api-reference/api.md#monitoring.coreos.com/v1.ServiceMonitor) deployed with the NATS instance. | `{"nats-monitor":"tposs"}` |
| nats.global.image.pullSecretNames | list | Image Pull Secrets for the NATS deployment | `[] `|
| nats.natsBox.enabled | bool | Enables a sidecar container with [NATS utilities](https://github.com/nats-io/nats-box). | `false` |
| nats.container.image.repository | string | NATS image repository. | `"registry.cennso.com/cennso/nats"` |
| nats.container.image.tag | string | NATS image tag. | `"2.11.6-alpine"` |
| nats.container.image.pullPolicy | string | NATS [image pull policy](https://kubernetes.io/docs/concepts/containers/images/#image-pull-policy). | `"IfNotPresent"` |
| nats.container.limits.cpu | string | NATS container CPU limits. | `"1"` |
| nats.container.limits.memory | string | NATS container memory limits. | `"500Mi"` |
| nats.container.requests.cpu | string | NATS container CPU requests. | `"1"` |
| nats.container.requests.memory | string | NATS container memory requests. | `"500Mi"` |
| nats.config.jetstream.enabled | bool | Enables the NATS [JetStream](https://docs.nats.io/nats-concepts/jetstream) functionality. | `true` |
| nats.config.jetstream.fileStore.enabled | bool | Enables file storage of the NATS JetStream. | `false` |
| nats.config.jetstream.memoryStore.enabled | bool | Enables memory storage of the NATS JetStream. | `true` |
| nats.config.jetstream.memoryStore.maxSize | string | The maximum size of the memory storage of the NATS JetStream. | `"1Gi"` |
| nats.promExporter.enabled | bool | Enables [NATS Prometheus exporter](https://github.com/nats-io/prometheus-nats-exporter) to expose metrics from the deployed NATS instance(s). | `true` |
| nats.promExporter.image.repository | string | NATS Prometheus exporter image repository | registry.cennso.com/cennso/natsio/prometheus-nats-exporter |
| nats.promExporter.image.tag | string | NATS Prometheus exporter image tag | 0.17.2 |
| nats.service.merge.spec.ports | list | Ports exposed by the NATS [Service](https://kubernetes.io/docs/concepts/services-networking/service/). | `[{"appProtocol":"tcp","name":"monitor","port":7777,"protocol":"TCP","targetPort":7777},{"appProtocol":"tcp","name":"nats","port":4222,"protocol":"TCP","targetPort":"nats"}]` |
| nats.reloader.enabled | bool | Enables NATS configuration watcher | true |
| nats.reloader.image.repository | string | NATS configuration watcher image repository | registry.cennso.com/cennso/natsio/nats-server-config-reloader |
| nats.reloader.image.tag | string | NATS configuration watcher image tag | `"0.17.1"` |
