> 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/configuration/nats-services/global-lock.md).

# Global lock service

You may configure and use the [NATS](https://nats.io/)-based Cennso Data Mesh (CDM) servers to support the [global lock](https://docs.cennso.com/components/tposs/6.19/configuration/session.md#session-handler-locking) for handling PCS sessions in TPOSS.

To do this, you need to specify the name of the pre-configured service in the sessions configuration. For example:

```yaml
sessions:
  global_lock:
    enabled: true
    algorithm: "global-lock-provider"
```

Also, you must configure the `global-lock-provider` service using the following configuration:

```yaml
nats-services:
  global-lock-provider:
    # Enables support for global lock based on NATS.
    enabled: true
    # Endpoint of the NATS server.
    server: "localhost:4222"
    # Timeout in milliseconds to reconnect to a NATS server in case the connection is lost.
    backoff_period: 1000
    # Timeout in milliseconds to wait for the establishment of the connection to a NATS server.
    connection_timeout: 3000
    # Maximum number of connections per stream. Initially, TPOSS creates the number of connections specified in **nats-services.global-lock-provider.connections_per_stream**.
    # If this number is not sufficient for the given load, TPOSS may create additional connections, but not more than this value.
    max_connections: 5
    # The time values that show how many NATS requests were handled for that time or less.
    # The values are translated into Prometheus histogram buckets and are provided in milliseconds.
    request_histogram_buckets:
      - 10
      - 100
      - 1000
      - 5000
      - 10000
    # Timeout in milliseconds that specifies the maximum wait time for a response from the NATS server.
    recv_timeout: 3000
    # Timeout in milliseconds that specifies the maximum wait time for the underlying TCP stack to accept a send operation.
    send_timeout: 3000
    # NATS service stream name to create locks managed by the lock manager.
    # It is also used as a prefix for the topic names TPOSS uses to send lock messages.
    stream: "TPOSS_LOCK_MGR"
    # Number of connections per stream. In general, TPOSS creates seven streams, so the total
    # number of connections TPOSS tries to establish towards the NATS server is this number multiplied by seven.
    connections_per_stream: 5
    # Time in milliseconds that specifies how long a lock message is preserved in the NATS stream.
    ttl: 5000
    # Determines how messages are stored for retention. The possible values are `memory` and `file`.
    storage_type: "memory"
```

To enable the session locking that uses a NATS-based Cennso Data Mesh server, you must set **nats-services.{SERVICE_NAME}.enabled** to `true` and specify **nats-services.{SERVICE_NAME}.server** with the endpoint of the NATS server. If **nats-services.{SERVICE_NAME}.enabled** is set to `true` and the **sessions.global_lock.algorithm** is equal to the `{SERVICE_NAME}`, the TPOSS Helm chart starts the [init container](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) that checks the available NATS server specified in **nats-services.{SERVICE_NAME}.server**. The TPOSS container itself starts only after the init container successfully checks the availability of the specified NATS instance.

The **nats-services.{SERVICE_NAME}.stream** parameter specifies the name of the [stream](https://docs.nats.io/nats-concepts/jetstream). TPOSS instances use the stream to acquire the lock based on the NATS messages sent for the specific topic. TPOSS creates multiple streams at start-up if they do not exist at the NATS server. The value of **nats-services.{NATS_SERVICE}.stream** is used as a prefix to the topics accepted by the stream.

You can configure each stream to be used by multiple connections using the **nats-services.{SERVICE_NAME}.connections_per_stream** configuration option. If TPOSS detects that the number of connections is insufficient during high load, it creates additional connections. You can control the maximum number of connections using the **nats-services.{SERVICE_NAME}.max_connections** configuration option. The total number of connections is the number of streams multiplied by the **nats-services.{SERVICE_NAME}.connections_per_stream** value. By default, TPOSS creates seven streams. After TPOSS sends a message to the given stream, it is not stored there permanently, but is either purged by TPOSS itself or by the TTL timeout. The TTL timeout is configured with the **nats-services.{SERVICE_NAME}.ttl** configuration option.

The **nats-services.{SERVICE_NAME}.storage_type** configuration option controls the JetStream streams used by TPOSS. If the NATS server is configured with the file storage backend, TPOSS also must have this configuration option set to `file`. Otherwise, `memory` is also suitable.

TPOSS provides a metric to observe RTT (round-trip time) for requests towards NATS. You can configure the histogram buckets using the **nats-services.{SERVICE_NAME}.request_histogram_buckets** configuration option. For more information, see the [Metrics](https://docs.cennso.com/components/tposs/6.19/observability/metrics.md#nats-metrics) document.

The **recv_timeout** configuration option specifies how long TPOSS waits for a response from the NATS server. The **send_timeout** configuration option specifies how long TPOSS waits for the underlying TCP stack to accept a send operation. If any timeout is reached, TPOSS closes the NATS connection and tries to create a new one. Any incoming RADIUS, Diameter, SBI, or CDMP requests for this session are rejected until a new NATS connection is ready.
