Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(router): separate DB pool configs for master, accounts, and global schemas (#13335)

Sai Harsha Vardhane492baa34c5f
diff --git a/config/config.example.toml b/config/config.example.toml
index 530d86d3a3..5fb4e31408 100644
--- a/config/config.example.toml
+++ b/config/config.example.toml
@@ -73,6 +73,34 @@ queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 clie
idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+# Accounts schema SQL data store credentials (merchant accounts, profiles, MCAs, key stores)
+[accounts_database]
+username = "db_user" # DB Username
+password = "db_pass" # DB Password. Use base-64 encoded kms encrypted value here when kms is enabled
+host = "localhost" # DB Host
+port = 5432 # DB Port
+dbname = "hyperswitch_db" # Name of Database
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
+connection_timeout = 10 # Timeout for database connection in seconds
+queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+
+# Global tenant (users, roles) SQL data store credentials
+[global_database]
+username = "db_user" # DB Username
+password = "db_pass" # DB Password. Use base-64 encoded kms encrypted value here when kms is enabled
+host = "localhost" # DB Host
+port = 5432 # DB Port
+dbname = "hyperswitch_db" # Name of Database
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
+connection_timeout = 10 # Timeout for database connection in seconds
+queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+
# Redis credentials
[redis]
host = "127.0.0.1"
diff --git a/config/deployments/env_specific.toml b/config/deployments/env_specific.toml
index 07003f4924..14415ce873 100644
--- a/config/deployments/env_specific.toml
+++ b/config/deployments/env_specific.toml
@@ -303,6 +303,34 @@ queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 clie
idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+# Accounts schema SQL data store credentials (merchant accounts, profiles, MCAs, key stores)
+[accounts_database]
+username = "db_user" # DB Username
+password = "db_pass" # DB Password. Use base-64 encoded kms encrypted value here when kms is enabled
+host = "localhost" # DB Host
+port = 5432 # DB Port
+dbname = "hyperswitch_db" # Name of Database
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
+connection_timeout = 10 # Timeout for database connection in seconds
+queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+
+# Global tenant (users, roles) SQL data store credentials
+[global_database]
+username = "db_user" # DB Username
+password = "db_pass" # DB Password. Use base-64 encoded kms encrypted value here when kms is enabled
+host = "localhost" # DB Host
+port = 5432 # DB Port
+dbname = "hyperswitch_db" # Name of Database
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
+connection_timeout = 10 # Timeout for database connection in seconds
+queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
+
[report_download_config]
dispute_function = "report_download_config_dispute_function" # Config to download dispute report
payment_function = "report_download_config_payment_function" # Config to download payment report
diff --git a/config/development.toml b/config/development.toml
index f2aa2455b8..41f4e392cc 100644
--- a/config/development.toml
+++ b/config/development.toml
@@ -85,6 +85,30 @@ connection_timeout = 10
idle_timeout = 300
max_lifetime = 1800
+[accounts_database]
+username = "db_user"
+password = "db_pass"
+host = "localhost"
+port = 5432
+dbname = "hyperswitch_db"
+max_pool_size = 5
+min_idle_pool_size = 2
+connection_timeout = 10
+idle_timeout = 300
+max_lifetime = 1800
+
+[global_database]
+username = "db_user"
+password = "db_pass"
+host = "localhost"
+port = 5432
+dbname = "hyperswitch_db"
+max_pool_size = 5
+min_idle_pool_size = 2
+connection_timeout = 10
+idle_timeout = 300
+max_lifetime = 1800
+
[redis]
host = "127.0.0.1"
port = 6379
diff --git a/config/docker_compose.toml b/config/docker_compose.toml
index 879eab019f..9b801c0b10 100644
--- a/config/docker_compose.toml
+++ b/config/docker_compose.toml
@@ -97,6 +97,28 @@ min_idle_pool_size = 2
idle_timeout = 300
max_lifetime = 1800
+[accounts_database]
+username = "db_user"
+password = "db_pass"
+host = "pg"
+port = 5432
+dbname = "hyperswitch_db"
+max_pool_size = 5
+min_idle_pool_size = 2
+idle_timeout = 300
+max_lifetime = 1800
+
+[global_database]
+username = "db_user"
+password = "db_pass"
+host = "pg"
+port = 5432
+dbname = "hyperswitch_db"
+max_pool_size = 5
+min_idle_pool_size = 2
+idle_timeout = 300
+max_lifetime = 1800
+
[secrets]
admin_api_key = "test_admin"
jwt_secret = "secret"
diff --git a/crates/router/src/configs/secrets_transformers.rs b/crates/router/src/configs/secrets_transformers.rs
index 883db6efac..860f65ac04 100644
--- a/crates/router/src/configs/secrets_transformers.rs
+++ b/crates/router/src/configs/secrets_transformers.rs
@@ -425,6 +425,28 @@ pub(crate) async fn fetch_raw_secrets(
.await
.expect("Failed to decrypt master database configuration");
+ #[allow(clippy::expect_used)]
+ let accounts_database = if let Some(accounts_database) = conf.accounts_database {
+ Some(
+ settings::Database::convert_to_raw_secret(accounts_database, secret_management_client)
+ .await
+ .expect("Failed to decrypt accounts database configuration"),
+ )
+ } else {
+ None
+ };
+
+ #[allow(clippy::expect_used)]
+ let global_database = if let Some(global_database) = conf.global_database {
+ Some(
+ settings::Database::convert_to_raw_secret(global_database, secret_management_client)
+ .await
+ .expect("Failed to decrypt global database configuration"),
+ )
+ } else {
+ None
+ };
+
#[cfg(feature = "olap")]
#[allow(clippy::expect_used)]
let analytics =
@@ -564,6 +586,8 @@ pub(crate) async fn fetch_raw_secrets(
chat,
sage,
master_database,
+ accounts_database,
+ global_database,
redis: conf.redis,
log: conf.log,
#[cfg(feature = "deja")]
diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rs
index e9988ca08f..022af98461 100644
--- a/crates/router/src/configs/settings.rs
+++ b/crates/router/src/configs/settings.rs
@@ -85,6 +85,10 @@ pub struct Settings<S: SecretState> {
pub chat: SecretStateContainer<ChatSettings, S>,
pub sage: SecretStateContainer<SageSettings, S>,
pub master_database: SecretStateContainer<Database, S>,
+ /// Falls back to `master_database` when not configured.
+ pub accounts_database: Option<SecretStateContainer<Database, S>>,
+ /// Falls back to `master_database` when not configured.
+ pub global_database: Option<SecretStateContainer<Database, S>>,
#[cfg(feature = "olap")]
pub replica_database: SecretStateContainer<Database, S>,
pub redis: RedisSettings,
@@ -491,6 +495,8 @@ impl TenantConfig {
&event_handler,
conf,
tenant,
+ conf.master_database.clone().into_inner(),
+ conf.accounts_database_config(),
cache_store.clone(),
testable,
))
@@ -524,6 +530,8 @@ impl TenantConfig {
&event_handler,
conf,
tenant,
+ conf.master_database.clone().into_inner(),
+ conf.accounts_database_config(),
cache_store.clone(),
testable,
))
@@ -1387,6 +1395,12 @@ impl Settings<SecuredSecret> {
pub fn validate(&self) -> ApplicationResult<()> {
self.server.validate()?;
self.master_database.get_inner().validate()?;
+ if let Some(accounts_database) = &self.accounts_database {
+ accounts_database.get_inner().validate()?;
+ }
+ if let Some(global_database) = &self.global_database {
+ global_database.get_inner().validate()?;
+ }
#[cfg(feature = "olap")]
self.replica_database.get_inner().validate()?;
@@ -1513,6 +1527,24 @@ impl Settings<RawSecret> {
pub fn is_kv_soft_kill_mode(&self) -> bool {
false
}
+
+ /// Returns the accounts database config, falling back to `master_database` if
+ /// `accounts_database` is not configured.
+ pub fn accounts_database_config(&self) -> Database {
+ self.accounts_database
+ .clone()
+ .map(SecretStateContainer::into_inner)
+ .unwrap_or_else(|| self.master_database.clone().into_inner())
+ }
+
+ /// Returns the global database config, falling back to `master_database` if
+ /// `global_database` is not configured.
+ pub fn global_database_config(&self) -> Database {
+ self.global_database
+ .clone()
+ .map(SecretStateContainer::into_inner)
+ .unwrap_or_else(|| self.master_database.clone().into_inner())
+ }
}
#[cfg(feature = "payouts")]
diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rs
index 606d41c931..32504a3c97 100644
--- a/crates/router/src/routes/app.rs
+++ b/crates/router/src/routes/app.rs
@@ -478,6 +478,8 @@ impl AppState {
&event_handler,
&conf,
&conf.multitenancy.global_tenant,
+ conf.global_database_config(),
+ conf.global_database_config(),
Arc::clone(&cache_store),
testable,
))
@@ -553,11 +555,14 @@ impl AppState {
/// # Panics
///
/// Panics if Failed to create store
+ #[allow(clippy::too_many_arguments)]
pub async fn get_store_interface(
storage_impl: &StorageImpl,
event_handler: &EventsHandler,
conf: &Settings,
tenant: &dyn TenantConfig,
+ master_config: settings::Database,
+ accounts_config: settings::Database,
cache_store: Arc<RedisStore>,
testable: bool,
) -> Box<dyn CommonStorageInterface> {
@@ -589,6 +594,8 @@ impl AppState {
get_store(
&conf.clone(),
tenant,
+ master_config.clone(),
+ accounts_config.clone(),
Arc::clone(&cache_store),
testable,
key_manager_state,
@@ -606,6 +613,8 @@ impl AppState {
get_store(
conf,
tenant,
+ master_config,
+ accounts_config,
Arc::clone(&cache_store),
testable,
key_manager_state,
diff --git a/crates/router/src/services.rs b/crates/router/src/services.rs
index 3862c4a8ca..1a07be00e8 100644
--- a/crates/router/src/services.rs
+++ b/crates/router/src/services.rs
@@ -31,7 +31,10 @@ use storage_impl::{errors::StorageResult, redis::RedisStore, RouterStore};
use tokio::sync::oneshot;
pub use self::{api::*, encryption::*};
-use crate::{configs::Settings, core::errors};
+use crate::{
+ configs::{settings::Database, Settings},
+ core::errors,
+};
#[cfg(not(feature = "olap"))]
pub type StoreType = storage_impl::database::store::Store;
@@ -50,14 +53,17 @@ pub type Store = KVRouterStore<StoreType>;
pub async fn get_store(
config: &Settings,
tenant: &dyn TenantConfig,
+ master_config: Database,
+ accounts_config: Database,
cache_store: Arc<RedisStore>,
test_transaction: bool,
key_manager_state: keymanager::KeyManagerState,
) -> StorageResult<Store> {
- let master_config = config.master_database.clone().into_inner();
-
+ // Reads are served off a single replica pool regardless of tenant/accounts/global role.
#[cfg(feature = "olap")]
let replica_config = config.replica_database.clone().into_inner();
+ #[cfg(feature = "olap")]
+ let accounts_replica_config = replica_config.clone();
#[allow(clippy::expect_used)]
let master_enc_key = hex::decode(config.secrets.get_inner().master_enc_key.clone().expose())
@@ -65,11 +71,16 @@ pub async fn get_store(
.expect("Failed to decode master key from hex");
#[cfg(not(feature = "olap"))]
- let conf = master_config.into();
+ let conf = (master_config.into(), accounts_config.into());
#[cfg(feature = "olap")]
// this would get abstracted, for all cases
#[allow(clippy::useless_conversion)]
- let conf = (master_config.into(), replica_config.into());
+ let conf = (
+ master_config.into(),
+ replica_config.into(),
+ accounts_config.into(),
+ accounts_replica_config.into(),
+ );
let store: RouterStore<StoreType> = if test_transaction {
RouterStore::test_store(
diff --git a/crates/storage_impl/src/database/store.rs b/crates/storage_impl/src/database/store.rs
index 14a3d7318b..e84de69a5c 100644
--- a/crates/storage_impl/src/database/store.rs
+++ b/crates/storage_impl/src/database/store.rs
@@ -38,18 +38,24 @@ pub struct Store {
#[async_trait::async_trait]
impl DatabaseStore for Store {
- type Config = Database;
+ /// (master config, accounts config)
+ type Config = (Database, Database);
async fn new(
- config: Database,
+ config: (Database, Database),
tenant_config: &dyn TenantConfig,
test_transaction: bool,
_key_manager_state: Option<keymanager::KeyManagerState>,
) -> StorageResult<Self> {
+ let (master_config, accounts_config) = config;
Ok(Self {
- master_pool: diesel_make_pg_pool(&config, tenant_config.get_schema(), test_transaction)
- .await?,
+ master_pool: diesel_make_pg_pool(
+ &master_config,
+ tenant_config.get_schema(),
+ test_transaction,
+ )
+ .await?,
accounts_pool: diesel_make_pg_pool(
- &config,
+ &accounts_config,
tenant_config.get_accounts_schema(),
test_transaction,
)
@@ -84,20 +90,22 @@ pub struct ReplicaStore {
#[async_trait::async_trait]
impl DatabaseStore for ReplicaStore {
- type Config = (Database, Database);
+ /// (master config, replica config, accounts master config, accounts replica config)
+ type Config = (Database, Database, Database, Database);
async fn new(
- config: (Database, Database),
+ config: (Database, Database, Database, Database),
tenant_config: &dyn TenantConfig,
test_transaction: bool,
_key_manager_state: Option<keymanager::KeyManagerState>,
) -> StorageResult<Self> {
- let (master_config, replica_config) = config;
+ let (master_config, replica_config, accounts_master_config, accounts_replica_config) =
+ config;
let master_pool =
diesel_make_pg_pool(&master_config, tenant_config.get_schema(), test_transaction)
.await
.attach_printable("failed to create master pool")?;
let accounts_master_pool = diesel_make_pg_pool(
- &master_config,
+ &accounts_master_config,
tenant_config.get_accounts_schema(),
test_transaction,
)
@@ -112,7 +120,7 @@ impl DatabaseStore for ReplicaStore {
.attach_printable("failed to create replica pool")?;
let accounts_replica_pool = diesel_make_pg_pool(
- &replica_config,
+ &accounts_replica_config,
tenant_config.get_accounts_schema(),
test_transaction,
)
diff --git a/loadtest/config/development.toml b/loadtest/config/development.toml
index ffe197d68b..778718ff3b 100644
--- a/loadtest/config/development.toml
+++ b/loadtest/config/development.toml
@@ -25,6 +25,30 @@ min_idle_pool_size = 2
idle_timeout = 300
max_lifetime = 1800
+[accounts_database]
+username = "postgres"
+password = "postgres"
+host = "db"
+port = 5432
+dbname = "loadtest_router"
+max_pool_size = 20
+connection_timeout = 10
+min_idle_pool_size = 2
+idle_timeout = 300
+max_lifetime = 1800
+
+[global_database]
+username = "postgres"
+password = "postgres"
+host = "db"
+port = 5432
+dbname = "loadtest_router"
+max_pool_size = 20
+connection_timeout = 10
+min_idle_pool_size = 2
+idle_timeout = 300
+max_lifetime = 1800
+
[server]
host = "0.0.0.0"