Introduction
In this article, we will discuss the significance of optimizing your database connections in accordance with your available resources. We highly recommend utilizing an external PostgreSQL database server for production instances of Xray.
By default, PostgreSQL's max_connections setting is configured to 100, which denotes the maximum number of concurrent connections it can support. However, Xray microservices may require more than 170 simultaneous connections under heavy load. The default configuration settings are outlined below. You can update the values in your system.yaml file and restart the server to apply the new connection configuration.
server:
database:
maxOpenConnections: 60
analysis:
database:
maxOpenConnections: 30
indexer:
database:
maxOpenConnections: 30
persist:
database:
maxOpenConnections: 30
Note:
It is recommended to maintain a server-to-services ratio of 2:1
You have the option to increase the default values, as most of the production level database instances can support more than 170 concurrent connections. To determine the appropriate max_connections setting for your database, you can perform calculations based on your system's specifications.
For example, the current max_connections setting for an RDS MySQL instance defaults to {DBInstanceClassMemory / 12582880}. If you are using a t2.micro instance with 512 MB of RAM, the maximum number of connections would be calculated as follows: (512×1024×1024)/12582880≈40, and so forth.
Calculation for HA Xray nodes:
If you are on a HA cluster for Xray, then you have to use the formula below:
Total # of connections = (number of nodes) * (maxOpenConnServer + maxOpenConnPersist + maxOpenConnAnalysis + maxOpenConnIndexer) + 50;
If you are operating two nodes with the default maxOpenConnections settings from Xray, the max_connections value in your database should be set to 400, based on the specified formula. If you choose to increase the default maxOpenConnections for Xray's services, you can recalculate and adjust the max_connections value accordingly.
This approach aims to optimize resource utilization and prevent bottlenecks when running the application with the default configurations.