Loading…
Loading…
Configure the worker service's database connection pool, including IAM and password authentication modes, pool sizing, and socket resilience settings.
This document describes the database connection configuration for the worker service. The system uses a HikariDataSource connection pool to manage connections to the primary PostgreSQL database. This configuration is critical for the worker's performance, resilience, and ability to authenticate with the database.
The configuration supports two primary authentication methods:
A key feature of this component is its resilience to network partitions. It applies socket timeouts to prevent the connection pool from becoming exhausted by stale connections, allowing for self-healing once network connectivity is restored.
Finally, every connection obtained from the pool is wrapped by a TenantBindingDataSource. This wrapper associates the connection with the current tenant context, a foundational step for implementing multi-tenancy features like row-level security.
The DataSource bean is initialized by the application framework at startup. The configuration logic proceeds through the following steps:
S2R_DB_USER) is configured. If not, the application will fail to start.S2R_DB_IAM_AUTH_ENABLED environment variable is set to false. Otherwise, the choice depends on the authMode configuration property.HikariDataSource instance is created with the pool name s2r-worker-hikari. The pool is sized with a maximum of 16 connections to handle burst workloads, such as processing a backlog of events after a service recovery.jdbc:postgresql:// URL using the host, port, database name, and SSL mode. It requires the S2R_DB_HOST and S2R_DB_PASSWORD to be configured.TenantBindingDataSource. This wrapper ensures that for every borrowed connection, the session-level parameter app.tenant_id is set to the ID of the current tenant. This is active in both authentication modes.The following diagram illustrates the logic for selecting the authentication mode and constructing the final DataSource.
flowchart TD
subgraph "Authentication Mode Selection"
A[Start: dataSource bean creation] --> B{S2R_DB_IAM_AUTH_ENABLED is 'false'?};
B -- Yes --> C[Password Auth];
B -- No / Unset --> D{authMode property is 'iam'?};
D -- Yes --> E[IAM Auth];
D -- No --> C;
end
subgraph "DataSource Construction"
E -- "Requires S2R_DB_INSTANCE_CONNECTION_NAME" --> F[Configure for Cloud SQL Connector];
C -- "Requires S2R_DB_HOST" --> G[Configure for standard JDBC];
F --> H[Create HikariDataSource];
G --> H;
H --> I[Wrap with TenantBindingDataSource];
I --> J[End: DataSource is ready];
endYou can configure the worker to use either IAM or password-based database authentication.
This is the default and recommended method for cloud deployments. It relies on the Google Cloud SQL Java connector (postgres-socket-factory), which handles mTLS and automatically refreshes short-lived IAM database credentials.
To enable IAM authentication:
S2R_DB_IAM_AUTH_ENABLED environment variable is either unset or not set to false.authMode configuration property to iam.S2R_DB_INSTANCE_CONNECTION_NAME.When this mode is active, the connector manages the secure tunnel to the database, so S2R_DB_HOST and S2R_DB_PASSWORD are not used.
This mode uses a standard username and password. It is intended for local development (e.g., with Docker Compose) or on-premises deployments where IAM is not available.
To enable password authentication, set the environment variable S2R_DB_IAM_AUTH_ENABLED=false.
IMPORTANT
When using password authentication, you must provide values for S2R_DB_HOST and S2R_DB_PASSWORD.
The database connection is configured through a combination of an environment variable and properties from the central DbProperties configuration. The property names listed below correspond to environment variables (e.g., user corresponds to S2R_DB_USER).
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
S2R_DB_IAM_AUTH_ENABLED | String | true | No | If set to false (case-insensitive), forces password authentication. Otherwise, defers to authMode. |
user | String | — | Yes | The database user to connect as. Must be configured. |
authMode | String | — | No | Specifies the authentication method. Set to iam for IAM-based authentication. If not iam, password auth is used (unless overridden by S2R_DB_IAM_AUTH_ENABLED). |
instanceConnectionName | String | — | If authMode is iam | The Cloud SQL instance connection name (e.g., project:region:instance). |
ipType | String | PRIVATE | No | The IP type for the Cloud SQL connector to use (PUBLIC or PRIVATE). Used only with IAM auth. |
host | String | — | If using password auth | The database host name or IP address. |
port | Integer | — | No | The database port. The source does not define a default. |
name | String | — | No | The name of the database to connect to. |
password | String | — | If using password auth | The password for the database user. |
sslMode | String | require | No | The SSL mode for the JDBC connection. Used only with password auth. |
socketTimeoutSeconds | Integer | — | No | The socket read timeout in seconds. If greater than 0, applied to all connections to prevent hangs during network partitions. |
connectTimeoutSeconds | Integer | — | No | The connection timeout for the underlying database driver in seconds. |
The worker service uses a HikariCP connection pool with specific tuning parameters. These are set internally and are not directly configurable but are important for understanding the service's behavior.
| Parameter | Value | Description |
|---|---|---|
poolName | s2r-worker-hikari | The name of the connection pool. |
maximumPoolSize | 16 | The maximum number of active connections. Sized to handle high-throughput event replay scenarios. |
minimumIdle | 4 | The minimum number of idle connections maintained in the pool. |
registerMbeans | true | Exposes pool metrics via JMX (HikariPoolMXBean) for monitoring. |
connectionTimeout | 3000 ms | The maximum time to wait for a connection from the pool. |
validationTimeout | 2000 ms | The maximum time to wait for a connection to be validated. |
initializationFailTimeout | -1 | The pool will fail to start immediately if it cannot successfully connect to the database. |
maxLifetime | 30 minutes | The maximum lifetime of a connection in the pool. |
keepaliveTime | 10 minutes | The frequency at which keepalive probes are sent to idle connections to prevent them from being terminated by the network. |
leakDetectionThreshold | socketTimeoutSeconds * 2 | The time a connection can be borrowed before a leak is suspected. Dynamically set based on socketTimeoutSeconds to avoid false positives on slow queries. Disabled if socketTimeoutSeconds is 0. |
The application will fail to start with an IllegalStateException under the following conditions:
S2R_DB_USER is not configured.authMode is iam), but S2R_DB_INSTANCE_CONNECTION_NAME is not configured.S2R_DB_HOST is not configured.In the event of a network partition between the worker and the database, active connections could previously become stuck waiting for a response, eventually exhausting the pool.
This configuration mitigates the issue by setting a socketTimeout on each database connection. If a connection is unresponsive for longer than this timeout, it will be terminated with an exception. This allows the connection to be returned to the pool and for the pool to self-heal by creating new connections once network connectivity is restored.
TenantBindingDataSource is a key component of the platform's multi-tenancy strategy. It ensures that all database operations are executed within the correct tenant's context.