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

fix: Implement persistent caching for config table retrieval (#2044)

Kartikeya Hegde25e82a1f7f2c
diff --git a/crates/router/src/core/configs.rs b/crates/router/src/core/configs.rs
index b17d8a1499..3c29a81c47 100644
--- a/crates/router/src/core/configs.rs
+++ b/crates/router/src/core/configs.rs
@@ -25,7 +25,7 @@ pub async fn set_config(
pub async fn read_config(store: &dyn StorageInterface, key: &str) -> RouterResponse<api::Config> {
let config = store
- .find_config_by_key_cached(key)
+ .find_config_by_key(key)
.await
.to_not_found_response(errors::ApiErrorResponse::ConfigNotFound)?;
Ok(ApplicationResponse::Json(config.foreign_into()))
@@ -36,7 +36,7 @@ pub async fn update_config(
config_update: &api::ConfigUpdate,
) -> RouterResponse<api::Config> {
let config = store
- .update_config_cached(&config_update.key, config_update.foreign_into())
+ .update_config_by_key(&config_update.key, config_update.foreign_into())
.await
.to_not_found_response(errors::ApiErrorResponse::ConfigNotFound)?;
Ok(ApplicationResponse::Json(config.foreign_into()))
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index 596c2ff725..680698e9bd 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -106,7 +106,7 @@ where
let connector_api_version = if supported_connector.contains(&connector_enum) {
state
.store
- .find_config_by_key_cached(&format!("connector_api_version_{connector_id}"))
+ .find_config_by_key(&format!("connector_api_version_{connector_id}"))
.await
.map(|value| value.config)
.ok()
diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rs
index eab55f458e..13878b4741 100644
--- a/crates/router/src/core/utils.rs
+++ b/crates/router/src/core/utils.rs
@@ -262,7 +262,7 @@ pub async fn construct_refund_router_data<'a, F>(
let connector_api_version = if supported_connector.contains(&connector_enum) {
state
.store
- .find_config_by_key_cached(&format!("connector_api_version_{connector_id}"))
+ .find_config_by_key(&format!("connector_api_version_{connector_id}"))
.await
.map(|value| value.config)
.ok()
diff --git a/crates/router/src/core/webhooks/utils.rs b/crates/router/src/core/webhooks/utils.rs
index 5826216b16..ad37775e7a 100644
--- a/crates/router/src/core/webhooks/utils.rs
+++ b/crates/router/src/core/webhooks/utils.rs
@@ -29,7 +29,7 @@ pub async fn lookup_webhook_event(
Ok(merchant_webhook_config) => merchant_webhook_config.contains(event),
Err(..) => {
//if failed to fetch from redis. fetch from db and populate redis
- db.find_config_by_key_cached(&redis_key)
+ db.find_config_by_key(&redis_key)
.await
.map(|config| {
if let Ok(set) =
diff --git a/crates/router/src/db/configs.rs b/crates/router/src/db/configs.rs
index f7bc8e699d..42457ff684 100644
--- a/crates/router/src/db/configs.rs
+++ b/crates/router/src/db/configs.rs
@@ -25,7 +25,7 @@ pub trait ConfigInterface {
key: &str,
) -> CustomResult<storage::Config, errors::StorageError>;
- async fn find_config_by_key_cached(
+ async fn find_config_by_key_from_db(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError>;
@@ -36,12 +36,6 @@ pub trait ConfigInterface {
config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError>;
- async fn update_config_cached(
- &self,
- key: &str,
- config_update: storage::ConfigUpdate,
- ) -> CustomResult<storage::Config, errors::StorageError>;
-
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError>;
}
@@ -55,49 +49,41 @@ impl ConfigInterface for Store {
config.insert(&conn).await.map_err(Into::into).into_report()
}
- //fetch directly from DB
- async fn find_config_by_key(
+ //update in DB and remove in redis and cache
+ async fn update_config_by_key(
&self,
key: &str,
+ config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError> {
- let conn = connection::pg_connection_write(self).await?;
- storage::Config::find_by_key(&conn, key)
- .await
- .map_err(Into::into)
- .into_report()
+ cache::publish_and_redact(self, CacheKind::Config(key.into()), || {
+ self.update_config_by_key(key, config_update)
+ })
+ .await
}
- async fn update_config_by_key(
+ async fn find_config_by_key_from_db(
&self,
key: &str,
- config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
- storage::Config::update_by_key(&conn, key, config_update)
+ storage::Config::find_by_key(&conn, key)
.await
.map_err(Into::into)
.into_report()
}
- //update in DB and remove in redis and cache
- async fn update_config_cached(
- &self,
- key: &str,
- config_update: storage::ConfigUpdate,
- ) -> CustomResult<storage::Config, errors::StorageError> {
- cache::publish_and_redact(self, CacheKind::Config(key.into()), || {
- self.update_config_by_key(key, config_update)
- })
- .await
- }
-
//check in cache, then redis then finally DB, and on the way back populate redis and cache
- async fn find_config_by_key_cached(
+ async fn find_config_by_key(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
- cache::get_or_populate_in_memory(self, key, || self.find_config_by_key(key), &CONFIG_CACHE)
- .await
+ cache::get_or_populate_in_memory(
+ self,
+ key,
+ || self.find_config_by_key_from_db(key),
+ &CONFIG_CACHE,
+ )
+ .await
}
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {
@@ -138,18 +124,6 @@ impl ConfigInterface for MockDb {
Ok(config_new)
}
- async fn find_config_by_key(
- &self,
- key: &str,
- ) -> CustomResult<storage::Config, errors::StorageError> {
- let configs = self.configs.lock().await;
- let config = configs.iter().find(|c| c.key == key).cloned();
-
- config.ok_or_else(|| {
- errors::StorageError::ValueNotFound("cannot find config".to_string()).into()
- })
- }
-
async fn update_config_by_key(
&self,
key: &str,
@@ -174,30 +148,6 @@ impl ConfigInterface for MockDb {
result
}
- async fn update_config_cached(
- &self,
- key: &str,
- config_update: storage::ConfigUpdate,
- ) -> CustomResult<storage::Config, errors::StorageError> {
- let result = self
- .configs
- .lock()
- .await
- .iter_mut()
- .find(|c| c.key == key)
- .ok_or_else(|| {
- errors::StorageError::ValueNotFound("cannot find config to update".to_string())
- .into()
- })
- .map(|c| {
- let config_updated =
- ConfigUpdateInternal::from(config_update).create_config(c.clone());
- *c = config_updated.clone();
- config_updated
- });
-
- result
- }
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {
let mut configs = self.configs.lock().await;
@@ -216,7 +166,19 @@ impl ConfigInterface for MockDb {
result
}
- async fn find_config_by_key_cached(
+ async fn find_config_by_key_from_db(
+ &self,
+ key: &str,
+ ) -> CustomResult<storage::Config, errors::StorageError> {
+ let configs = self.configs.lock().await;
+ let config = configs.iter().find(|c| c.key == key).cloned();
+
+ config.ok_or_else(|| {
+ errors::StorageError::ValueNotFound("cannot find config".to_string()).into()
+ })
+ }
+
+ async fn find_config_by_key(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
diff --git a/crates/router/src/workflows/payment_sync.rs b/crates/router/src/workflows/payment_sync.rs
index 4225f780b0..744cce3132 100644
--- a/crates/router/src/workflows/payment_sync.rs
+++ b/crates/router/src/workflows/payment_sync.rs
@@ -124,7 +124,7 @@ pub async fn get_sync_process_schedule_time(
process_data::ConnectorPTMapping,
errors::StorageError,
> = db
- .find_config_by_key_cached(&format!("pt_mapping_{connector}"))
+ .find_config_by_key(&format!("pt_mapping_{connector}"))
.await
.map(|value| value.config)
.and_then(|config| {