> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/tposs/latest/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/latest/configuration/db.md).

# Database settings

TPOSS must be able to connect to the MySQL database during startup. The following configuration defines the database connection settings for TPOSS:

```yaml
database:
  # Database name used by the TPOSS database client.
  name: tposs
  # Database client user name.
  user: tposs
  # Database client user password.
  password: tposs
  # Host address of the database server. The port is mandatory.
  host: db-host:3306
  # Number of connections within the database connection pool.
  connections: 50
  # Timeout in milliseconds to wait for a free connection from the database connection pool.
  queue_target: 50
  # Timeout in milliseconds for the queries executed by the TPOSS database client.
  timeout: 30000
  # Size of the TCP buffer in bytes for database connections.
  recbuf: 87380
  # Timeout in milliseconds that specifies how long the data transmitted to the database
  # can remain unacknowledged before the TCP connection is closed.
  tcp_user_timeout: 1000
  # Specifies whether the MySQL clients enable TCP keepalive.
  keepalive: true
  # The time values that show how many queries were executed for that time or less.
  # The values are translated into Prometheus histogram buckets and are provided in milliseconds.
  query_histogram_buckets: "5, 20, 50, 100, 250, 500, 1000"
  # The time values that show how many database clients waited in the queue for that time or less.
  # The values are translated into Prometheus histogram buckets and are provided in milliseconds.
  queue_histogram_buckets: "1, 5, 20, 50, 100, 250, 500"
  # Interval in milliseconds after which TPOSS MySQL clients send the COM_PING command to the database.
  ping_interval: 1000
  # Timeout in milliseconds that specifies how long the TPOSS database client will wait
  # for a response to a COM_PING command request from the database.
  ping_timeout: 1000
```

TPOSS uses the first four configuration options to connect to the MySQL database. Before TPOSS connects to the MySQL server for the first time, you must create:

- Database name specified in **database.name**
- User specified in **database.user**
- User password specified in **database.password**

Be careful when assigning [permissions](https://dev.mysql.com/doc/refman/8.0/en/grant.html) for the TPOSS database user. At minimum, the user needs permissions for all basic operations like `CREATE`, `ALTER`, `SELECT`, `INSERT`, `UPDATE`, `DELETE`, and `EVENT`. You can simplify this by granting [ALL permissions](https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html#priv_all) to the TPOSS database using the following query:

```sql
CREATE USER IF NOT EXISTS 'tposs'@'%' IDENTIFIED WITH mysql_native_password BY 'tposs';
GRANT ALL PRIVILEGES ON tposs.* TO 'tposs'@'%';
```

If the TPOSS cluster is launched using a fresh MySQL instance, it needs to run the database migrations on the new database. For this, set the following option to `true`:

```yaml
# Run the database migration during TPOSS startup.
tposs_migrate_db_on_start: true
```

The size of the database connection pool is specified with the **database.connections** configuration option. It is recommended not to set a big number because increasing the number of connections towards the database may decrease the database performance. Also, the sum of all connections used by all TPOSS instances within the TPOSS cluster should not be bigger than the value of the [**max_connections**](https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_max_connections) database configuration option. 

If there is no free connection in the database connection pool, TPOSS waits for a period equal to twice the value of the **database.queue_target** configuration option. If a connection still isn’t available after this time, the query is aborted, and TPOSS returns an error for the incoming request. Additionally, the maximum time that TPOSS waits for a database response is controlled by the **database.timeout** configuration option. If the database does not respond within this time, TPOSS aborts the query and returns an error.

TPOSS exposes different [database metrics](https://docs.cennso.com/components/tposs/latest/observability/db.md). You can control the behavior of some of these metrics using the **database.queue_histogram_buckets** and **database.query_histogram_buckets** configuration options. For more information about the values for these configuration options, see [Prometheus histograms](https://prometheus.io/docs/practices/histograms/).

You can control the behavior of MySQL client sockets in TPOSS using these configuration options:

- Use **database.recbuf** to set the size of the socket receive buffer.
- Use **database.keepalive** and **database.tcp_user_timeout** options to detect disconnects as early as possible.

If **database.keepalive** is set to `true`, TPOSS MySQL clients send TCP keepalive packets. While you can’t configure the keepalive messages interval directly through the database configuration options, you can set the [security context](https://docs.cennso.com/components/tposs/latest/configuration/security_context.md).

> [!NOTE]
> It is highly recommended to set the value of the **database.keepalive** to `true`. The value of the **database.tcp_user_timeout** is worth setting low - for example, around 1 second - to catch disconnects as fast as possible, as it may impact TPOSS performance and resource consumption.

The **database.tcp_user_timeout** option specifies how long (in milliseconds) transmitted data can remain unacknowledged. If this timeout is reached, the MySQL client sockets are closed.

## Database health check

> [!IMPORTANT]
> You should configure values of the following metrics based on the regular network round-trip time (RTT) to the database server:
>
> - **database.health_check.ping_timeout** must be greater than the network RTT to avoid false-positive failures. For example, if the RTT to the database is `50ms`, set **database.health_check.ping_timeout** to at least `100-200ms` to account for network jitter and database processing time.
> - **database.health_check.ping_interval** determines how frequently health checks are performed. Lower values detect failures faster but may lead to false positives if the network is not stable.
> - **database.health_check.consecutive_failures_threshold** combined with **database.health_check.ping_interval** determines the total failure detection time. For example, with **database.health_check.ping_interval** set to `500` and **database.health_check.consecutive_failures_threshold** set to `3`, the database will be marked unhealthy after `1.5` seconds of consecutive failures.
>
> Measure the actual network RTT to your database server and adjust these values accordingly for your deployment environment.

TPOSS provides a database health checker process which looks for the status of connectivity to the database and the database pool. If this process sees that something is wrong with the database, it notifies TPOSS sub-systems to start rejecting any incoming requests (RADIUS, Diameter, HTTP, and CDMP) until connectivity is restored. You can control the health check process using the following configuration options:

```yaml
database:
  health_check:
    # Interval in milliseconds between COM_PING commands.
    ping_interval: 500
    # Timeout in milliseconds to wait a response for a single COM_PING command.
    ping_timeout: 200
    # Timeout in milliseconds for establishing the health check connection.
    connect_timeout: 2000
    # Number of consecutive COM_PING request failures before the database is marked unhealthy.
    consecutive_failures_threshold: 3
    # Minimum ratio of active connections to pool size required to consider the database healthy.
    # For example, 0.75 means at least 75% of pool connections must be active.
    healthy_pool_ratio: 0.75
    # Maximum number of queries allowed to queue per database connection before the database is considered unhealthy.
    # For example, if pool_size is configured to `10` connections and this option is set to `5`, the database
    # is marked unhealthy when more than 50 requests are waiting for a free connection in the pool queue.
    max_queued_queries_per_connection: 10
```

The **database.health_check.ping_interval** is a time interval for which the health checker process sends a `COM_PING` MySQL protocol-level message to the MySQL server. Then, the health checker process waits for a response for a maximum time specified by the **database.health_check.ping_timeout**. If the number of `COM_PING` requests equals the **database.health_check.consecutive_failures_threshold** failed, TPOSS stops handling any incoming (RADIUS, Diameter, HTTP or CDMP) requests and marks the database as unavailable until the next MySQL `COM_PING` request succeeds.

In addition to the `COM_PING` requests, TPOSS checks the size of the queue in the database connection pool. If there are more queries in the queue than the size of the database connection pool (specified by the **database.connections**) multiplied by the **max_queued_queries_per_connection**, TPOSS stops to handle the incoming requests until the queue size is back within regular conditions.

When the database health checker sees that the connections to the database can be restored, it constantly checks the number of active connections to the database. As soon as the percentage of active connections is higher than the **database.health_check.healthy_pool_ratio**, the database is marked as available, and TPOSS can proceed to handle incoming requests. The value of this configuration option must be any floating point value from `0.0` to `1.0`.
