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

perf(cache): coordinate in-memory cache population per key (#14513)

Hrithikesha160f4ef1a60
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index a1e8e0a37b..01b2c03d6f 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -5716,7 +5716,7 @@ pub async fn call_surcharge_decision_management(
#[cfg(feature = "v2")]
let algorithm_ref: routing_types::RoutingAlgorithmRef = todo!();
- let (surcharge_results, merchant_sucharge_configs) =
+ let (surcharge_results, merchant_sucharge_configs) = Box::pin(
perform_surcharge_decision_management_for_payment_method_list(
&state,
algorithm_ref,
@@ -5724,10 +5724,11 @@ pub async fn call_surcharge_decision_management(
&payment_intent,
billing_address.as_ref().map(Into::into),
response_payment_method_types,
- )
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("error performing surcharge decision operation")?;
+ ),
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("error performing surcharge decision operation")?;
if !surcharge_results.is_empty_result() {
surcharge_results
.persist_individual_surcharge_details_in_redis(&state, business_profile)
@@ -5778,13 +5779,13 @@ pub async fn call_surcharge_decision_management_for_saved_card(
let algorithm_ref: routing_types::RoutingAlgorithmRef = todo!();
// TODO: Move to business profile surcharge column
- let surcharge_results = perform_surcharge_decision_management_for_saved_cards(
+ let surcharge_results = Box::pin(perform_surcharge_decision_management_for_saved_cards(
state,
algorithm_ref,
payment_attempt,
&payment_intent,
&mut customer_payment_method_response.customer_payment_methods,
- )
+ ))
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("error performing surcharge decision operation")?;
@@ -6671,14 +6672,14 @@ async fn perform_surcharge_ops(
.zip(business_profile)
.map(|((pa, pi), bp)| (pa, pi, bp))
{
- call_surcharge_decision_management_for_saved_card(
+ Box::pin(call_surcharge_decision_management_for_saved_card(
state,
platform,
&business_profile,
payment_attempt,
payment_intent.clone(),
response,
- )
+ ))
.await?;
}
diff --git a/crates/router/src/core/payment_methods/surcharge_decision_configs.rs b/crates/router/src/core/payment_methods/surcharge_decision_configs.rs
index 7f547a96aa..dda3d93a5e 100644
--- a/crates/router/src/core/payment_methods/surcharge_decision_configs.rs
+++ b/crates/router/src/core/payment_methods/surcharge_decision_configs.rs
@@ -136,11 +136,11 @@ pub async fn perform_surcharge_decision_management_for_payment_method_list(
surcharge_decision_configs::MerchantSurchargeConfigs::default(),
),
(None, Some(algorithm_id)) => {
- let cached_algo = ensure_algorithm_cached(
+ let cached_algo = Box::pin(ensure_algorithm_cached(
&*state.store,
&payment_attempt.merchant_id,
algorithm_id.as_str(),
- )
+ ))
.await?;
let merchant_surcharge_config = cached_algo.merchant_surcharge_configs.clone();
@@ -251,11 +251,11 @@ pub async fn perform_surcharge_decision_management_for_session_flow(
SurchargeSource::Predetermined(request_surcharge_details)
}
(None, Some(algorithm_id)) => {
- let cached_algo = ensure_algorithm_cached(
+ let cached_algo = Box::pin(ensure_algorithm_cached(
&*state.store,
&payment_attempt.merchant_id,
algorithm_id.as_str(),
- )
+ ))
.await?;
SurchargeSource::Generate(cached_algo)
@@ -302,11 +302,11 @@ pub async fn perform_surcharge_decision_management_for_saved_cards(
SurchargeSource::Predetermined(request_surcharge_details)
}
(None, Some(algorithm_id)) => {
- let cached_algo = ensure_algorithm_cached(
+ let cached_algo = Box::pin(ensure_algorithm_cached(
&*state.store,
&payment_attempt.merchant_id,
algorithm_id.as_str(),
- )
+ ))
.await?;
SurchargeSource::Generate(cached_algo)
@@ -510,11 +510,15 @@ pub async fn ensure_algorithm_cached(
.change_context(errors::StorageError::ValueNotFound("Program".to_string()))
.attach_printable("Error initializing DSL interpreter backend")
};
- let interpreter =
- cache::get_or_populate_in_memory(store, &key, value_to_cache, cache::CacheId::Surcharge)
- .await
- .change_context(ConfigError::CacheMiss)
- .attach_printable("Unable to retrieve cached routing algorithm even after refresh")?;
+ let interpreter = Box::pin(cache::get_or_populate_in_memory_redis(
+ store,
+ &key,
+ value_to_cache(),
+ cache::CacheId::Surcharge,
+ ))
+ .await
+ .change_context(ConfigError::CacheMiss)
+ .attach_printable("Unable to retrieve cached routing algorithm even after refresh")?;
Ok(interpreter)
}
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index f4fe630d0e..e9b5b73001 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -914,12 +914,12 @@ where
payment_data.set_connector_customer_id(connector_customer_id);
- let authentication_type = call_decision_manager(
+ let authentication_type = Box::pin(call_decision_manager(
state,
platform.get_processor(),
&business_profile,
&payment_data,
- )
+ ))
.await?;
payment_data.set_authentication_type_in_attempt(authentication_type);
@@ -1589,7 +1589,7 @@ where
ConnectorCallType::SessionMultiple(connectors) => {
let session_surcharge_details =
- call_surcharge_decision_management_for_session_flow(
+ Box::pin(call_surcharge_decision_management_for_session_flow(
state,
platform.get_processor(),
&business_profile,
@@ -1597,7 +1597,7 @@ where
payment_data.get_payment_intent(),
payment_data.get_billing_address(),
&connectors,
- )
+ ))
.await?;
vault_session::populate_vault_session_details(
@@ -2444,12 +2444,12 @@ where
.attach_printable("Could not decode the routing algorithm")?
.unwrap_or_default();
- let output = perform_decision_management(
+ let output = Box::pin(perform_decision_management(
state,
algorithm_ref,
processor.get_account().get_id(),
&payment_dsl_data,
- )
+ ))
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Could not decode the conditional config")?;
@@ -2943,7 +2943,7 @@ pub async fn call_surcharge_decision_management_for_session_flow(
#[cfg(feature = "v2")]
let algorithm_ref: api::routing::RoutingAlgorithmRef = todo!();
- let surcharge_results =
+ let surcharge_results = Box::pin(
surcharge_decision_configs::perform_surcharge_decision_management_for_session_flow(
state,
algorithm_ref,
@@ -2951,10 +2951,11 @@ pub async fn call_surcharge_decision_management_for_session_flow(
payment_intent,
billing_address,
&payment_method_type_list,
- )
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("error performing surcharge decision operation")?;
+ ),
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("error performing surcharge decision operation")?;
Ok(if surcharge_results.is_empty_result() {
None
diff --git a/crates/router/src/core/payments/conditional_configs.rs b/crates/router/src/core/payments/conditional_configs.rs
index 73744780e5..c1e56e8a0c 100644
--- a/crates/router/src/core/payments/conditional_configs.rs
+++ b/crates/router/src/core/payments/conditional_configs.rs
@@ -48,12 +48,12 @@ pub async fn perform_decision_management(
.attach_printable("Error initializing DSL interpreter backend")
};
- let interpreter = cache::get_or_populate_in_memory(
+ let interpreter = Box::pin(cache::get_or_populate_in_memory_redis(
db,
&key,
- find_key_from_db,
+ find_key_from_db(),
cache::CacheId::DecisionManager,
- )
+ ))
.await
.change_context(ConfigError::DslCachePoisoned)?;
diff --git a/crates/router/src/core/routing/helpers.rs b/crates/router/src/core/routing/helpers.rs
index ec2803af98..e421637d8a 100644
--- a/crates/router/src/core/routing/helpers.rs
+++ b/crates/router/src/core/routing/helpers.rs
@@ -662,13 +662,12 @@ pub trait DynamicRoutingCache {
key: &str,
) -> Option<Arc<Self>>;
- async fn refresh_dynamic_routing_cache<T, F, Fut>(
+ async fn refresh_dynamic_routing_cache<T, Fut>(
state: &SessionState,
key: &str,
- func: F,
+ func: Fut,
) -> RouterResult<T>
where
- F: FnOnce() -> Fut + Send,
T: Cacheable + serde::Serialize + serde::de::DeserializeOwned + Debug + Clone,
Fut: futures::Future<Output = errors::CustomResult<T, errors::StorageError>> + Send;
}
@@ -691,17 +690,16 @@ impl DynamicRoutingCache for routing_types::SuccessBasedRoutingConfig {
.await
}
- async fn refresh_dynamic_routing_cache<T, F, Fut>(
+ async fn refresh_dynamic_routing_cache<T, Fut>(
state: &SessionState,
key: &str,
- func: F,
+ func: Fut,
) -> RouterResult<T>
where
- F: FnOnce() -> Fut + Send,
T: Cacheable + serde::Serialize + serde::de::DeserializeOwned + Debug + Clone,
Fut: futures::Future<Output = errors::CustomResult<T, errors::StorageError>> + Send,
{
- cache::get_or_populate_in_memory(
+ cache::get_or_populate_in_memory_redis(
&*state.store,
key,
func,
@@ -731,17 +729,16 @@ impl DynamicRoutingCache for routing_types::ContractBasedRoutingConfig {
.await
}
- async fn refresh_dynamic_routing_cache<T, F, Fut>(
+ async fn refresh_dynamic_routing_cache<T, Fut>(
state: &SessionState,
key: &str,
- func: F,
+ func: Fut,
) -> RouterResult<T>
where
- F: FnOnce() -> Fut + Send,
T: Cacheable + serde::Serialize + serde::de::DeserializeOwned + Debug + Clone,
Fut: futures::Future<Output = errors::CustomResult<T, errors::StorageError>> + Send,
{
- cache::get_or_populate_in_memory(
+ cache::get_or_populate_in_memory_redis(
&*state.store,
key,
func,
@@ -771,17 +768,16 @@ impl DynamicRoutingCache for routing_types::EliminationRoutingConfig {
.await
}
- async fn refresh_dynamic_routing_cache<T, F, Fut>(
+ async fn refresh_dynamic_routing_cache<T, Fut>(
state: &SessionState,
key: &str,
- func: F,
+ func: Fut,
) -> RouterResult<T>
where
- F: FnOnce() -> Fut + Send,
T: Cacheable + serde::Serialize + serde::de::DeserializeOwned + Debug + Clone,
Fut: futures::Future<Output = errors::CustomResult<T, errors::StorageError>> + Send,
{
- cache::get_or_populate_in_memory(
+ cache::get_or_populate_in_memory_redis(
&*state.store,
key,
func,
@@ -840,7 +836,7 @@ where
};
let dynamic_routing_config =
- T::refresh_dynamic_routing_cache(state, key.as_str(), func).await?;
+ T::refresh_dynamic_routing_cache(state, key.as_str(), func()).await?;
Ok(dynamic_routing_config)
}
diff --git a/crates/router/src/db/api_keys.rs b/crates/router/src/db/api_keys.rs
index 8c4ad159df..414066bc65 100644
--- a/crates/router/src/db/api_keys.rs
+++ b/crates/router/src/db/api_keys.rs
@@ -187,12 +187,12 @@ impl ApiKeyInterface for Store {
#[cfg(feature = "accounts_cache")]
{
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
&_hashed_api_key.into_inner(),
- find_call,
+ find_call(),
cache::CacheId::Accounts,
- )
+ ))
.await
}
}
@@ -526,16 +526,16 @@ mod tests {
db.find_api_key_by_hash_optional(hashed_api_key.clone())
.await
};
- let _: Option<storage::ApiKey> = cache::get_or_populate_in_memory(
+ let _: Option<storage::ApiKey> = Box::pin(cache::get_or_populate_in_memory_redis(
&db,
&format!(
"{}_{}",
merchant_id.get_string_repr(),
hashed_api_key.clone().into_inner()
),
- find_call,
+ find_call(),
cache::CacheId::Accounts,
- )
+ ))
.await
.unwrap();
diff --git a/crates/router/src/db/merchant_connector_account.rs b/crates/router/src/db/merchant_connector_account.rs
index 4b31a117ca..50ad6a0481 100644
--- a/crates/router/src/db/merchant_connector_account.rs
+++ b/crates/router/src/db/merchant_connector_account.rs
@@ -272,18 +272,19 @@ mod merchant_connector_account_cache_tests {
.await
.change_context(errors::StorageError::DecryptionError)
};
- let _: storage::MerchantConnectorAccount = cache::get_or_populate_in_memory(
- &db,
- &format!(
- "{}_{}",
- merchant_id.get_string_repr(),
- profile_id.get_string_repr(),
- ),
- find_call,
- cache::CacheId::Accounts,
- )
- .await
- .unwrap();
+ let _: storage::MerchantConnectorAccount =
+ Box::pin(cache::get_or_populate_in_memory_redis(
+ &db,
+ &format!(
+ "{}_{}",
+ merchant_id.get_string_repr(),
+ profile_id.get_string_repr(),
+ ),
+ find_call(),
+ cache::CacheId::Accounts,
+ ))
+ .await
+ .unwrap();
let delete_call = || async {
db.delete_merchant_connector_account_by_merchant_id_merchant_connector_id(
@@ -453,18 +454,19 @@ mod merchant_connector_account_cache_tests {
.change_context(errors::StorageError::DecryptionError)
};
- let _: storage::MerchantConnectorAccount = cache::get_or_populate_in_memory(
- &db,
- &format!(
- "{}_{}",
- merchant_id.clone().get_string_repr(),
- profile_id.get_string_repr()
- ),
- find_call,
- cache::CacheId::Accounts,
- )
- .await
- .unwrap();
+ let _: storage::MerchantConnectorAccount =
+ Box::pin(cache::get_or_populate_in_memory_redis(
+ &db,
+ &format!(
+ "{}_{}",
+ merchant_id.clone().get_string_repr(),
+ profile_id.get_string_repr()
+ ),
+ find_call(),
+ cache::CacheId::Accounts,
+ ))
+ .await
+ .unwrap();
let delete_call = || async { db.delete_merchant_connector_account_by_id(&id).await };
diff --git a/crates/router/src/db/organization.rs b/crates/router/src/db/organization.rs
index 47b1cc024d..e5c1858cc3 100644
--- a/crates/router/src/db/organization.rs
+++ b/crates/router/src/db/organization.rs
@@ -59,12 +59,12 @@ impl OrganizationInterface for Store {
#[cfg(feature = "accounts_cache")]
{
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
org_id.get_string_repr(),
- find_call,
+ find_call(),
cache::CacheId::Accounts,
- )
+ ))
.await
}
}
diff --git a/crates/storage_impl/Cargo.toml b/crates/storage_impl/Cargo.toml
index 5c6e05fb60..f3f0113377 100644
--- a/crates/storage_impl/Cargo.toml
+++ b/crates/storage_impl/Cargo.toml
@@ -52,7 +52,7 @@ moka = { version = "0.12", features = ["future"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
thiserror = "1.0.69"
-tokio = { version = "1.48.0", features = ["rt-multi-thread"] }
+tokio = { version = "1.48.0", features = ["rt-multi-thread", "sync", "time"] }
[lints]
workspace = true
diff --git a/crates/storage_impl/src/business_profile.rs b/crates/storage_impl/src/business_profile.rs
index 0d4545324a..8dc4a889cf 100644
--- a/crates/storage_impl/src/business_profile.rs
+++ b/crates/storage_impl/src/business_profile.rs
@@ -216,10 +216,10 @@ impl<T: DatabaseStore> ProfileInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- Box::pin(cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
&profile_cache_key(profile_id),
- fetch_func,
+ fetch_func(),
CacheId::Accounts,
))
.await?
@@ -264,10 +264,10 @@ impl<T: DatabaseStore> ProfileInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- Box::pin(cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
&profile_cache_key(profile_id),
- fetch_func,
+ fetch_func(),
CacheId::Accounts,
))
.await?
diff --git a/crates/storage_impl/src/card_issuer.rs b/crates/storage_impl/src/card_issuer.rs
index e2db70836a..10e0ba584b 100644
--- a/crates/storage_impl/src/card_issuer.rs
+++ b/crates/storage_impl/src/card_issuer.rs
@@ -89,12 +89,12 @@ impl<T: DatabaseStore> CardIssuersInterface for RouterStore<T> {
.await
.map_err(|error| report!(StorageError::from(error)))
};
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
CARD_ISSUERS_LIST_CACHE_KEY,
- fetch_func,
+ fetch_func(),
cache::CacheId::Config,
- )
+ ))
.await
}
diff --git a/crates/storage_impl/src/configs.rs b/crates/storage_impl/src/configs.rs
index 4cad28a16c..eb35808d3d 100644
--- a/crates/storage_impl/src/configs.rs
+++ b/crates/storage_impl/src/configs.rs
@@ -124,8 +124,13 @@ impl<T: DatabaseStore> ConfigInterface for RouterStore<T> {
}
}
};
- cache::get_or_populate_in_memory(self, key, find_config_by_key_from_db, CacheId::Config)
- .await
+ Box::pin(cache::get_or_populate_in_memory_redis(
+ self,
+ key,
+ find_config_by_key_from_db(),
+ CacheId::Config,
+ ))
+ .await
}
#[instrument(skip_all)]
diff --git a/crates/storage_impl/src/errors.rs b/crates/storage_impl/src/errors.rs
index 32be57877b..32b52579ad 100644
--- a/crates/storage_impl/src/errors.rs
+++ b/crates/storage_impl/src/errors.rs
@@ -41,6 +41,12 @@ pub enum StorageError {
RedisError(error_stack::Report<RedisError>),
#[error("InvalidDataFormat: {0}")]
InvalidDataFormat(String),
+ #[error("In-memory cache compute returned an operation it was never asked for")]
+ CacheInvariantViolation,
+ #[error("In-memory cache populate was cut off at the configured cap")]
+ CachePopulateTimedOut,
+ #[error("In-memory cache entry could not be downcast to the expected type")]
+ CacheTypeMismatch,
}
impl From<error_stack::Report<RedisError>> for StorageError {
diff --git a/crates/storage_impl/src/merchant_account.rs b/crates/storage_impl/src/merchant_account.rs
index 2e28543495..bb73294de5 100644
--- a/crates/storage_impl/src/merchant_account.rs
+++ b/crates/storage_impl/src/merchant_account.rs
@@ -204,12 +204,12 @@ impl<T: DatabaseStore> MerchantAccountInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
merchant_id.get_string_repr(),
- fetch_func,
+ fetch_func(),
CacheId::Accounts,
- )
+ ))
.await?
.convert(
state,
@@ -304,12 +304,12 @@ impl<T: DatabaseStore> MerchantAccountInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- merchant_account = cache::get_or_populate_in_memory(
+ merchant_account = Box::pin(cache::get_or_populate_in_memory_redis(
self,
publishable_key,
- fetch_by_pub_key_func,
+ fetch_by_pub_key_func(),
CacheId::Accounts,
- )
+ ))
.await?;
}
let key_store = self
diff --git a/crates/storage_impl/src/merchant_connector_account.rs b/crates/storage_impl/src/merchant_connector_account.rs
index 6597de734d..37b96f68ff 100644
--- a/crates/storage_impl/src/merchant_connector_account.rs
+++ b/crates/storage_impl/src/merchant_connector_account.rs
@@ -98,12 +98,12 @@ async fn list_all_by_merchant_id<T: DatabaseStore>(
#[cfg(feature = "accounts_cache")]
{
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
store,
&list_cache::merchant_scope_key(merchant_id),
- find_call,
+ find_call(),
cache::CacheId::McaList,
- )
+ ))
.await
}
}
@@ -136,12 +136,12 @@ async fn list_all_by_merchant_id_profile_id<T: DatabaseStore>(
#[cfg(feature = "accounts_cache")]
{
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
store,
&list_cache::merchant_profile_scope_key(merchant_id, profile_id),
- find_call,
+ find_call(),
cache::CacheId::McaList,
- )
+ ))
.await
}
}
@@ -453,12 +453,12 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
&format!("{}_{}", merchant_id.get_string_repr(), connector_label),
- find_call,
+ find_call(),
cache::CacheId::Accounts,
- )
+ ))
.await
.async_and_then(|item| async {
item.convert(
@@ -509,12 +509,12 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
&format!("{}_{}", profile_id.get_string_repr(), connector_name),
- find_call,
+ find_call(),
cache::CacheId::Accounts,
- )
+ ))
.await
.async_and_then(|item| async {
item.convert(
@@ -538,7 +538,7 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
connector_name: &str,
key_store: &MerchantKeyStore,
) -> CustomResult<Vec<domain::MerchantConnectorAccount>, Self::Error> {
- let accounts = list_all_by_merchant_id(self, merchant_id)
+ let accounts = Box::pin(list_all_by_merchant_id(self, merchant_id))
.await?
.into_iter()
.filter(|account| account.connector_name == connector_name)
@@ -582,16 +582,16 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
&format!(
"{}_{}",
merchant_id.get_string_repr(),
merchant_connector_id.get_string_repr()
),
- find_call,
+ find_call(),
cache::CacheId::Accounts,
- )
+ ))
.await?
.convert(
self.get_keymanager_state()
@@ -634,12 +634,12 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
id.get_string_repr(),
- find_call,
+ find_call(),
cache::CacheId::Accounts,
- )
+ ))
.await?
.convert(
self.get_keymanager_state()
@@ -706,11 +706,15 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
key_store: &MerchantKeyStore,
connector_type: common_enums::ConnectorType,
) -> CustomResult<Vec<domain::MerchantConnectorAccount>, Self::Error> {
- let accounts = list_all_by_merchant_id_profile_id(self, &key_store.merchant_id, profile_id)
- .await?
- .into_iter()
- .filter(|account| account.is_enabled() && account.connector_type == connector_type)
- .collect();
+ let accounts = Box::pin(list_all_by_merchant_id_profile_id(
+ self,
+ &key_store.merchant_id,
+ profile_id,
+ ))
+ .await?
+ .into_iter()
+ .filter(|account| account.is_enabled() && account.connector_type == connector_type)
+ .collect();
decrypt_all(self, accounts, key_store).await
}
@@ -722,7 +726,7 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
get_disabled: bool,
key_store: &MerchantKeyStore,
) -> CustomResult<domain::MerchantConnectorAccounts, Self::Error> {
- let accounts = list_all_by_merchant_id(self, merchant_id)
+ let accounts = Box::pin(list_all_by_merchant_id(self, merchant_id))
.await?
.into_iter()
.filter(|account| get_disabled || account.is_enabled())
@@ -741,7 +745,7 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
merchant_id: &common_utils::id_type::MerchantId,
get_disabled: bool,
) -> CustomResult<domain::MerchantConnectorAccountsWithoutEncrypted, Self::Error> {
- let accounts = list_all_by_merchant_id(self, merchant_id)
+ let accounts = Box::pin(list_all_by_merchant_id(self, merchant_id))
.await?
.into_iter()
.filter(|account| get_disabled || account.is_enabled());
@@ -762,9 +766,13 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
merchant_id: &common_utils::id_type::MerchantId,
profile_id: &common_utils::id_type::ProfileId,
) -> CustomResult<domain::MerchantConnectorAccountsWithoutEncrypted, Self::Error> {
- let accounts = list_all_by_merchant_id_profile_id(self, merchant_id, profile_id)
- .await?
- .into_iter();
+ let accounts = Box::pin(list_all_by_merchant_id_profile_id(
+ self,
+ merchant_id,
+ profile_id,
+ ))
+ .await?
+ .into_iter();
let output = accounts
.map(domain::MerchantConnectorAccountWithoutEncrypted::try_from)
@@ -782,10 +790,14 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {
merchant_id: &common_utils::id_type::MerchantId,
profile_id: &common_utils::id_type::ProfileId,
) -> CustomResult<domain::MerchantConnectorAccountsWithoutEncrypted, Self::Error> {
- let accounts = list_all_by_merchant_id_profile_id(self, merchant_id, profile_id)
- .await?
- .into_iter()
- .filter(|account| account.is_enabled());
+ let accounts = Box::pin(list_all_by_merchant_id_profile_id(
+ self,
+ merchant_id,
+ profile_id,
+ ))
+ .await?
+ .into_iter()
+ .filter(|account| account.is_enabled());
let output = accounts
.map(domain::MerchantConnectorAccountWithoutEncrypted::try_from)
diff --git a/crates/storage_impl/src/merchant_key_store.rs b/crates/storage_impl/src/merchant_key_store.rs
index 2f9bb3c54d..c9ed8408b1 100644
--- a/crates/storage_impl/src/merchant_key_store.rs
+++ b/crates/storage_impl/src/merchant_key_store.rs
@@ -136,12 +136,12 @@ impl<T: DatabaseStore> MerchantKeyStoreInterface for RouterStore<T> {
{
let key_store_cache_key =
format!("merchant_key_store_{}", merchant_id.get_string_repr());
- cache::get_or_populate_in_memory(
+ Box::pin(cache::get_or_populate_in_memory_redis(
self,
&key_store_cache_key,
- fetch_func,
+ fetch_func(),
CacheId::Accounts,
- )
+ ))
.await?
.convert(state, key, merchant_id.clone().into())
.await
diff --git a/crates/storage_impl/src/metrics.rs b/crates/storage_impl/src/metrics.rs
index 2d9cfc3af3..e79b49d16a 100644
--- a/crates/storage_impl/src/metrics.rs
+++ b/crates/storage_impl/src/metrics.rs
@@ -21,5 +21,11 @@ counter_metric!(IN_MEMORY_CACHE_HIT, GLOBAL_METER);
counter_metric!(IN_MEMORY_CACHE_MISS, GLOBAL_METER);
counter_metric!(IN_MEMORY_CACHE_EVICTION_COUNT, GLOBAL_METER);
+// Metrics for in-memory cache population coalescing
+counter_metric!(IN_MEMORY_CACHE_POPULATION_AVOIDED, GLOBAL_METER); // Miss served by another caller
+counter_metric!(IN_MEMORY_CACHE_POPULATE_TIMEOUT, GLOBAL_METER); // Populate hit the timeout cap
+counter_metric!(IN_MEMORY_CACHE_TYPE_MISMATCH, GLOBAL_METER); // Entry had an unexpected type
+counter_metric!(IN_MEMORY_CACHE_INVARIANT_VIOLATION, GLOBAL_METER); // Unreachable compute outcome
+
// Metrics for cache invalidation
counter_metric!(CACHE_REDACTION_FAILURE_COUNT, GLOBAL_METER);
diff --git a/crates/storage_impl/src/redis/cache.rs b/crates/storage_impl/src/redis/cache.rs
index a8d04fab0b..94e3559683 100644
--- a/crates/storage_impl/src/redis/cache.rs
+++ b/crates/storage_impl/src/redis/cache.rs
@@ -1,4 +1,4 @@
-use std::{any::Any, borrow::Cow, fmt::Debug, sync::Arc};
+use std::{any::Any, borrow::Cow, fmt::Debug, num::NonZeroU64, sync::Arc, time::Duration};
use common_utils::{
errors::{self, CustomResult},
@@ -6,7 +6,10 @@ use common_utils::{
};
use dyn_clone::DynClone;
use error_stack::{Report, ResultExt};
-use moka::future::Cache as MokaCache;
+use moka::{
+ future::Cache as MokaCache,
+ ops::compute::{CompResult, Op},
+};
use redis_interface::{errors::RedisError, RedisConnectionWithContext, RedisValue};
use router_env::{
logger,
@@ -28,6 +31,13 @@ pub const DEFAULT_CACHE_TTL: u64 = 30 * 60;
/// Default time to idle 10 mins
pub const DEFAULT_CACHE_TTI: u64 = 10 * 60;
+/// Ceiling on how long a single populate attempt may run, 500ms — well above the worst-case
+/// chain of a redis read, a DB read, and a redis write (sub-millisecond, 3-5ms,
+/// sub-millisecond respectively). Not deployment-configurable: no `config/*.toml` has ever
+/// set the field this used to back.
+pub const DEFAULT_POPULATE_TIMEOUT_IN_MS: NonZeroU64 =
+ NonZeroU64::new(500).expect("500 is nonzero");
+
/// Default entry ceiling, deliberately the number the caches have always been built with.
///
/// It was written as `30` and documented as megabytes, then multiplied by `1024 * 1024` on the
@@ -71,7 +81,7 @@ impl CacheSettings {
self.tti_in_secs.unwrap_or(DEFAULT_CACHE_TTI)
}
- /// Resolves the entry ceiling against the cache's own default.
+ /// Resolves the ceiling this configuration asks for.
///
/// `None` means unbounded, and an explicitly configured `0` is how a bounded cache is made
/// unbounded, since the key's absence already means "use the default".
@@ -85,14 +95,16 @@ impl CacheSettings {
/// Builds the cache this configuration describes.
///
- /// `default` is the cache's own ceiling when the configuration names none, `None` meaning
- /// unbounded.
- fn build(&self, name: &'static str, default: Option<u64>) -> Cache {
+ /// `id` names the cache being built, and supplies the name it reports itself under in
+ /// metrics. `default` is the cache's own ceiling when the configuration names none,
+ /// `None` meaning unbounded.
+ fn build(&self, id: CacheId, default: Option<u64>) -> Cache {
Cache::new(
- name,
+ id.name(),
self.time_to_live(),
self.time_to_idle(),
self.max_entries(default),
+ Duration::from_millis(DEFAULT_POPULATE_TIMEOUT_IN_MS.get()),
)
}
}
@@ -172,27 +184,27 @@ impl Caches {
.invalidation_channel
.clone()
.unwrap_or_else(|| DEFAULT_IMC_INVALIDATION_CHANNEL.to_string()),
- config: config.config.build("CONFIG_CACHE", UNBOUNDED),
- accounts: config.accounts.build("ACCOUNTS_CACHE", DEFAULT),
- mca_list: config.mca_list.build("MCA_LIST_CACHE", DEFAULT),
- routing: config.routing.build("ROUTING_CACHE", DEFAULT),
+ config: config.config.build(CacheId::Config, UNBOUNDED),
+ accounts: config.accounts.build(CacheId::Accounts, DEFAULT),
+ mca_list: config.mca_list.build(CacheId::McaList, DEFAULT),
+ routing: config.routing.build(CacheId::Routing, DEFAULT),
decision_manager: config
.decision_manager
- .build("DECISION_MANAGER_CACHE", DEFAULT),
- surcharge: config.surcharge.build("SURCHARGE_CACHE", DEFAULT),
- cgraph: config.cgraph.build("CGRAPH_CACHE", DEFAULT),
+ .build(CacheId::DecisionManager, DEFAULT),
+ surcharge: config.surcharge.build(CacheId::Surcharge, DEFAULT),
+ cgraph: config.cgraph.build(CacheId::CGraph, DEFAULT),
pm_filters_cgraph: config
.pm_filters_cgraph
- .build("PM_FILTERS_CGRAPH_CACHE", DEFAULT),
+ .build(CacheId::PmFiltersCGraph, DEFAULT),
success_based_dynamic_algorithm: config
.success_based_dynamic_algorithm
- .build("SUCCESS_BASED_DYNAMIC_ALGORITHM_CACHE", DEFAULT),
+ .build(CacheId::SuccessBasedDynamicAlgorithm, DEFAULT),
elimination_based_dynamic_algorithm: config
.elimination_based_dynamic_algorithm
- .build("ELIMINATION_BASED_DYNAMIC_ALGORITHM_CACHE", DEFAULT),
+ .build(CacheId::EliminationBasedDynamicAlgorithm, DEFAULT),
contract_based_dynamic_algorithm: config
.contract_based_dynamic_algorithm
- .build("CONTRACT_BASED_DYNAMIC_ALGORITHM_CACHE", DEFAULT),
+ .build(CacheId::ContractBasedDynamicAlgorithm, DEFAULT),
}
}
@@ -321,6 +333,30 @@ pub enum CacheId {
EliminationBasedDynamicAlgorithm,
ContractBasedDynamicAlgorithm,
}
+
+impl CacheId {
+ /// The name this cache reports itself under in metrics and traces, as the `cache_type`
+ /// attribute.
+ ///
+ /// Kept verbatim: dashboards and alerts match on these strings, so they are part of the
+ /// deployment's interface rather than an internal label.
+ pub const fn name(self) -> &'static str {
+ match self {
+ Self::Config => "CONFIG_CACHE",
+ Self::Accounts => "ACCOUNTS_CACHE",
+ Self::McaList => "MCA_LIST_CACHE",
+ Self::Routing => "ROUTING_CACHE",
+ Self::DecisionManager => "DECISION_MANAGER_CACHE",
+ Self::Surcharge => "SURCHARGE_CACHE",
+ Self::CGraph => "CGRAPH_CACHE",
+ Self::PmFiltersCGraph => "PM_FILTERS_CGRAPH_CACHE",
+ Self::SuccessBasedDynamicAlgorithm => "SUCCESS_BASED_DYNAMIC_ALGORITHM_CACHE",
+ Self::EliminationBasedDynamicAlgorithm => "ELIMINATION_BASED_DYNAMIC_ALGORITHM_CACHE",
+ Self::ContractBasedDynamicAlgorithm => "CONTRACT_BASED_DYNAMIC_ALGORITHM_CACHE",
+ }
+ }
+}
+
/// Trait which defines the behaviour of types that's gonna be stored in Cache
pub trait Cacheable: Any + Send + Sync + DynClone {
fn as_any(&self) -> &dyn Any;
@@ -408,6 +444,9 @@ dyn_clone::clone_trait_object!(Cacheable);
pub struct Cache {
name: &'static str,
inner: MokaCache<String, Arc<dyn Cacheable>>,
+ /// How long a single populate attempt may run before it's treated as failed. See
+ /// [`Self::get_or_populate_in_memory`].
+ populate_timeout: Duration,
}
impl Debug for Cache {
@@ -458,6 +497,19 @@ fn in_memory_cache_key(key: CacheKey) -> String {
physical
}
+/// Downcasts a stored value back to its concrete type.
+///
+/// The explicit deref is load-bearing. `Arc<dyn Cacheable>` satisfies the blanket `Cacheable`
+/// impl itself, so `val.as_any()` would resolve to the `Arc`'s own impl and hand back a
+/// `&dyn Any` describing the `Arc` — downcasting which silently turns every read into a miss.
+/// Deref first so `as_any` comes from the value inside.
+fn downcast_cacheable<T>(val: Arc<dyn Cacheable>) -> Option<T>
+where
+ T: Clone + Cacheable,
+{
+ (*val).as_any().downcast_ref::<T>().cloned()
+}
+
impl Cache {
/// With given `time_to_live` and `time_to_idle` creates a moka cache.
///
@@ -470,6 +522,7 @@ impl Cache {
time_to_live: u64,
time_to_idle: u64,
max_entries: Option<u64>,
+ populate_timeout: Duration,
) -> Self {
// Record the metrics of manual invalidation of cache entry by the application
let eviction_listener = move |_, _, cause| {
@@ -482,8 +535,8 @@ impl Cache {
);
};
let mut cache_builder = MokaCache::builder()
- .time_to_live(std::time::Duration::from_secs(time_to_live))
- .time_to_idle(std::time::Duration::from_secs(time_to_idle))
+ .time_to_live(Duration::from_secs(time_to_live))
+ .time_to_idle(Duration::from_secs(time_to_idle))
.eviction_listener(eviction_listener);
// No weigher is configured, so moka counts entries — which is what this number has
@@ -495,6 +548,123 @@ impl Cache {
Self {
name,
inner: cache_builder.build(),
+ populate_timeout,
+ }
+ }
+
+ fn record_populate_timeout(&self) {
+ metrics::IN_MEMORY_CACHE_POPULATE_TIMEOUT
+ .add(1, router_env::metric_attributes!(("cache_type", self.name)));
+ logger::warn!(
+ cache_type = self.name,
+ timeout_secs = self.populate_timeout.as_secs(),
+ "An in-memory cache populate attempt was cut off at the configured cap"
+ );
+ }
+
+ fn record_population_avoided(&self) {
+ metrics::IN_MEMORY_CACHE_POPULATION_AVOIDED
+ .add(1, router_env::metric_attributes!(("cache_type", self.name)));
+ }
+
+ fn record_type_mismatch(&self) {
+ metrics::IN_MEMORY_CACHE_TYPE_MISMATCH
+ .add(1, router_env::metric_attributes!(("cache_type", self.name)));
+ logger::error!(
+ cache_type = self.name,
+ "An in-memory cache entry could not be downcast to the expected type"
+ );
+ }
+
+ fn record_invariant_violation(&self) {
+ metrics::IN_MEMORY_CACHE_INVARIANT_VIOLATION
+ .add(1, router_env::metric_attributes!(("cache_type", self.name)));
+ logger::error!(
+ cache_type = self.name,
+ "and_try_compute_with returned a CompResult variant get_or_populate_in_memory never asks for"
+ );
+ }
+
+ /// Reads `key`, and on a miss populates it by awaiting `populate`.
+ ///
+ /// Concurrent callers for one key are serialized by moka's per-key lock
+ /// (`and_try_compute_with`): each caller's own `populate` runs in turn against the
+ /// then-current state, so a failure or a timed-out attempt only ever affects the caller it
+ /// happened to, never whoever's queued behind it. A single attempt is capped at
+ /// `populate_timeout`; since a caller's turn ends the moment its future resolves, this
+ /// also bounds how long anyone queued behind it can be made to wait. Every attempt is
+ /// capped — there is no value of `populate_timeout` that opts out of the cap.
+ pub async fn get_or_populate_in_memory<T, Fut, E>(
+ &self,
+ key: CacheKey,
+ populate: Fut,
+ ) -> Result<T, E>
+ where
+ T: Cacheable + serde::Serialize + serde::de::DeserializeOwned + Clone,
+ Fut: futures::Future<Output = Result<T, E>> + Send,
+ E: From<StorageError> + Send + Sync + 'static,
+ {
+ // Fast path: a hit never touches moka's entry API or its per-key lock at all.
+ if let Some(val) = self.get_val::<T>(key.clone()).await {
+ return Ok(val);
+ }
+
+ let moka_key = in_memory_cache_key(key.clone());
+ let populate_timeout = self.populate_timeout;
+
+ // moka's `Op::Put` does the real insert; `record_populate_insert` below just mirrors
+ // `push`'s deja boundary so the write is still recorded.
+ let outcome = self
+ .inner
+ .entry(moka_key)
+ .and_try_compute_with(move |maybe_entry| async move {
+ if maybe_entry.is_some() {
+ return Ok(Op::Nop);
+ }
+
+ match tokio::time::timeout(populate_timeout, populate).await {
+ Ok(Ok(val)) => {
+ let val: Arc<dyn Cacheable> = Arc::new(val);
+ Ok(Op::Put(val))
+ }
+ Ok(Err(e)) => Err(Some(e)),
+ Err(_elapsed) => Err(None),
+ }
+ })
+ .await;
+
+ match outcome {
+ // A same-typed entry is expected here; a downcast failure means some other
+ // caller used this key for a different `T`.
+ Ok(CompResult::Unchanged(entry)) => {
+ self.record_population_avoided();
+ downcast_cacheable::<T>(entry.into_value()).ok_or_else(|| {
+ self.record_type_mismatch();
+ StorageError::CacheTypeMismatch.into()
+ })
+ }
+ // `ReplacedWith` can't actually happen here (we never `Op::Put` over an existing
+ // entry), but it carries a value just like `Inserted` does.
+ Ok(CompResult::Inserted(entry) | CompResult::ReplacedWith(entry)) => {
+ self.record_populate_insert(key.clone()).await;
+ downcast_cacheable::<T>(entry.into_value()).ok_or_else(|| {
+ self.record_type_mismatch();
+ StorageError::CacheTypeMismatch.into()
+ })
+ }
+ // Can't happen either: we never ask for `Op::Remove`, and `Op::Nop` only follows
+ // an already-present entry.
+ Ok(CompResult::Removed(_) | CompResult::StillNone(_)) => {
+ self.record_invariant_violation();
+ Err(StorageError::CacheInvariantViolation.into())
+ }
+ Err(Some(e)) => Err(e),
+ // Our own cap elapsed, not the backend — there's no real `E` to report. Our turn
+ // already ended, so anyone queued behind us has already moved on.
+ Err(None) => {
+ self.record_populate_timeout();
+ Err(StorageError::CachePopulateTimedOut.into())
+ }
}
}
@@ -518,6 +688,26 @@ impl Cache {
.await;
}
+ /// Records `push`'s exact `in_memory_push` boundary for a write that moka's own
+ /// `and_try_compute_with` (`Op::Put`) performed directly. deja's call-site identity hashes
+ /// the `boundary`/`operation` literals, not this function's body, so this is
+ /// indistinguishable from a real `push` call to its recording.
+ #[cfg_attr(
+ feature = "deja",
+ deja::boundary(
+ boundary = "imc",
+ component = "storage_impl::redis::cache",
+ operation = "in_memory_push",
+ replay = Execute,
+ effect = Imc,
+ codec = SerdeCodec,
+ args = deja_in_memory_args(self.name, &key),
+ )
+ )]
+ async fn record_populate_insert(&self, key: CacheKey) {
+ let _ = key;
+ }
+
// Deja: the L1 seam, instrumented on the method itself so no call path can
// bypass it. A recorded `Some(v)` substitutes on replay; a recorded `None`
// re-triggers the caller's fallback. The serde bound is deliberately
@@ -549,11 +739,7 @@ impl Cache {
.add(1, router_env::metric_attributes!(("cache_type", self.name)));
}
- // The explicit deref is load-bearing. `Arc<dyn Cacheable>` satisfies the blanket
- // `Cacheable` impl itself, so `val?.as_any()` would resolve to the `Arc`'s own impl and
- // hand back a `&dyn Any` describing the `Arc` — downcasting which silently turns every
- // read into a miss. Deref first so `as_any` comes from the value inside.
- (*val?).as_any().downcast_ref::<T>().cloned()
+ val.and_then(downcast_cacheable::<T>)
}
/// Check if a key exists in cache
@@ -631,15 +817,14 @@ impl Cache {
}
#[instrument(skip_all)]
-pub async fn get_or_populate_redis<T, F, Fut>(
+pub async fn get_or_populate_redis<T, Fut>(
redis: &RedisConnectionWithContext,
key: impl AsRef<str>,
ttl: Option<i64>,
- fun: F,
+ populate: Fut,
) -> CustomResult<T, StorageError>
where
T: serde::Serialize + serde::de::DeserializeOwned + Debug,
- F: FnOnce() -> Fut + Send,
Fut: futures::Future<Output = CustomResult<T, StorageError>> + Send,
{
let type_name = std::any::type_name::<T>();
@@ -647,23 +832,20 @@ where
let redis_val = redis
.get_and_deserialize_key::<T>(&key.into(), type_name)
.await;
- let get_data_set_redis = || async {
- let data = fun().await?;
- match ttl {
- Some(ttl) => {
- redis
- .serialize_and_set_key_with_expiry(&key.into(), &data, ttl)
- .await
- }
- None => redis.serialize_and_set_key(&key.into(), &data).await,
- }
- .change_context(StorageError::KVError)?;
- Ok::<_, Report<StorageError>>(data)
- };
match redis_val {
Err(err) => match err.current_context() {
RedisError::NotFound | RedisError::JsonDeserializationFailed => {
- get_data_set_redis().await
+ let data = populate.await?;
+ match ttl {
+ Some(ttl) => {
+ redis
+ .serialize_and_set_key_with_expiry(&key.into(), &data, ttl)
+ .await
+ }
+ None => redis.serialize_and_set_key(&key.into(), &data).await,
+ }
+ .change_context(StorageError::KVError)?;
+ Ok(data)
}
_ => Err(err
.change_context(StorageError::KVError)
@@ -681,15 +863,14 @@ fn deja_in_memory_args(cache_name: &str, key: &CacheKey) -> serde_json::Value {
}
#[instrument(skip_all)]
-pub async fn get_or_populate_in_memory<T, F, Fut, S>(
+pub async fn get_or_populate_in_memory_redis<T, Fut, S>(
store: &S,
key: &str,
- fun: F,
+ fun: Fut,
cache: CacheId,
) -> CustomResult<T, StorageError>
where
T: Cacheable + serde::Serialize + serde::de::DeserializeOwned + Debug + Clone,
- F: FnOnce() -> Fut + Send,
Fut: futures::Future<Output = CustomResult<T, StorageError>> + Send,
S: RedisConnInterface + CacheInterface + Send + Sync + ?Sized,
{
@@ -699,24 +880,21 @@ where
prefix: store.cache_key_prefix().to_string(),
};
- // An in-memory hit answers on its own. The redis connection is acquired only on a miss,
- // so a redis outage degrades this to a cold cache rather than an error.
- if let Some(val) = cache.get_val::<T>(cache_key.clone()).await {
- return Ok(val);
- }
-
- let redis = &store
- .get_redis_conn()
- .change_context(StorageError::RedisError(
- RedisError::RedisConnectionError.into(),
- ))
- .attach_printable("Failed to get redis connection")?;
- // The redis round trip already materialized the payload, so its size is free here and
- // does not have to be recomputed to bound the cache.
- let val = get_or_populate_redis(redis, key, None, fun).await?;
- cache.push(cache_key, val.clone()).await;
-
- Ok(val)
+ // The redis connection is acquired only when this caller is the one populating, so an
+ // in-memory hit answers during a redis outage rather than erroring, and concurrent
+ // misses for one key cost a single redis round trip between them. The round trip also
+ // reports the payload size, so weighing the entry costs nothing extra.
+ cache
+ .get_or_populate_in_memory(cache_key, async {
+ let redis = store
+ .get_redis_conn()
+ .change_context(StorageError::RedisError(
+ RedisError::RedisConnectionError.into(),
+ ))
+ .attach_printable("Failed to get redis connection")?;
+ get_or_populate_redis(&redis, key, None, fun).await
+ })
+ .await
}
#[instrument(skip_all)]
@@ -802,8 +980,259 @@ where
#[cfg(test)]
mod cache_tests {
+ use std::sync::atomic::{AtomicUsize, Ordering};
+
use super::*;
+ /// Satisfies `get_or_populate_in_memory`'s `E: From<StorageError>` bound for these tests' `E = String`.
+ impl From<StorageError> for String {
+ fn from(err: StorageError) -> Self {
+ err.to_string()
+ }
+ }
+
+ /// Long enough that a correctly coalescing test never trips it, short enough that a test
+ /// asserting the cap does not drag.
+ const TEST_POPULATE_TIMEOUT: Duration = Duration::from_millis(500);
+
+ fn test_key(key: &str) -> CacheKey {
+ CacheKey {
+ key: key.to_string(),
+ prefix: "prefix".to_string(),
+ }
+ }
+
+ /// A populate that counts its own invocations, so a test can assert how many callers
+ /// actually reached the backend.
+ fn counting_populate(
+ calls: &Arc<AtomicUsize>,
+ delay: Duration,
+ value: impl Into<String>,
+ ) -> futures::future::BoxFuture<'static, Result<String, String>> {
+ let calls = Arc::clone(calls);
+ let value = value.into();
+ Box::pin(async move {
+ calls.fetch_add(1, Ordering::SeqCst);
+ tokio::time::sleep(delay).await;
+ Ok(value)
+ })
+ }
+
+ /// A populate that always fails, counting its own invocations the same way.
+ fn failing_populate(
+ calls: &Arc<AtomicUsize>,
+ delay: Duration,
+ message: impl Into<String>,
+ ) -> futures::future::BoxFuture<'static, Result<String, String>> {
+ let calls = Arc::clone(calls);
+ let message = message.into();
+ Box::pin(async move {
+ calls.fetch_add(1, Ordering::SeqCst);
+ tokio::time::sleep(delay).await;
+ Err(message)
+ })
+ }
+
+ #[tokio::test]
+ async fn concurrent_misses_for_one_key_populate_once() {
+ let cache = Arc::new(Cache::new("test", 1800, 1800, None, TEST_POPULATE_TIMEOUT));
+ let calls = Arc::new(AtomicUsize::new(0));
+
+ let readers = (0..20).map(|_| {
+ let cache = Arc::clone(&cache);
+ let populate = counting_populate(&calls, Duration::from_millis(50), "val");
+ tokio::spawn(async move {
+ cache
+ .get_or_populate_in_memory(test_key("key"), populate)
+ .await
+ })
+ });
+
+ for result in futures::future::join_all(readers).await {
+ assert_eq!(result.unwrap(), Ok("val".to_string()));
+ }
+ assert_eq!(calls.load(Ordering::SeqCst), 1);
+ }
+
+ #[tokio::test]
+ async fn a_reader_arriving_during_a_populate_waits_and_hits() {
+ let cache = Arc::new(Cache::new("test", 1800, 1800, None, TEST_POPULATE_TIMEOUT));
+ let populating_calls = Arc::new(AtomicUsize::new(0));
+ let reader_calls = Arc::new(AtomicUsize::new(0));
+
+ let populating = {
+ let cache = Arc::clone(&cache);
+ let populate = counting_populate(&populating_calls, Duration::from_millis(100), "val");
+ tokio::spawn(async move {
+ cache
+ .get_or_populate_in_memory(test_key("key"), populate)
+ .await
+ })
+ };
+
+ // Let the first caller take the write side before the second one arrives.
+ tokio::time::sleep(Duration::from_millis(20)).await;
+
+ let reader = {
+ let cache = Arc::clone(&cache);
+ let populate = counting_populate(&reader_calls, Duration::ZERO, "other");
+ tokio::spawn(async move {
+ cache
+ .get_or_populate_in_memory(test_key("key"), populate)
+ .await
+ })
+ };
+
+ assert_eq!(populating.await.unwrap(), Ok("val".to_string()));
+ assert_eq!(reader.await.unwrap(), Ok("val".to_string()));
+ assert_eq!(populating_calls.load(Ordering::SeqCst), 1);
+ // The reader never reached the backend — it read what the populate wrote.
+ assert_eq!(reader_calls.load(Ordering::SeqCst), 0);
+ }
+
+ #[tokio::test]
+ async fn reads_of_a_populated_key_do_not_block_each_other() {
+ let cache = Arc::new(Cache::new("test", 1800, 1800, None, TEST_POPULATE_TIMEOUT));
+ cache.push(test_key("key"), "val".to_string()).await;
+ let calls = Arc::new(AtomicUsize::new(0));
+
+ // Far more concurrent readers than the runtime has worker threads: if the read side
+ // serialized them, this could not finish well inside the wait budget.
+ let started = std::time::Instant::now();
+ let readers = (0..200).map(|_| {
+ let cache = Arc::clone(&cache);
+ let populate = counting_populate(&calls, Duration::ZERO, "other");
+ tokio::spawn(async move {
+ cache
+ .get_or_populate_in_memory(test_key("key"), populate)
+ .await
+ })
+ });
+
+ for result in futures::future::join_all(readers).await {
+ assert_eq!(result.unwrap(), Ok("val".to_string()));
+ }
+ assert_eq!(calls.load(Ordering::SeqCst), 0);
+ assert!(started.elapsed() < TEST_POPULATE_TIMEOUT);
+ }
+
+ #[tokio::test]
+ async fn a_capped_populate_fails_that_caller_while_a_queued_caller_proceeds_independently() {
+ // A cap far below the populate's delay, so the first caller is guaranteed to be capped.
+ let cache = Arc::new(Cache::new(
+ "test",
+ 1800,
+ 1800,
+ None,
+ Duration::from_millis(50),
+ ));
+ let slow_calls = Arc::new(AtomicUsize::new(0));
+ let queued_calls = Arc::new(AtomicUsize::new(0));
+
+ let slow = {
+ let cache = Arc::clone(&cache);
+ let populate = counting_populate(&slow_calls, Duration::from_millis(600), "slow");
+ tokio::spawn(async move {
+ cache
+ .get_or_populate_in_memory(test_key("key"), populate)
+ .await
+ })
+ };
+
+ // Let the first caller start (and be capped) before the second one arrives.
+ tokio::time::sleep(Duration::from_millis(20)).await;
+
+ let queued = {
+ let cache = Arc::clone(&cache);
+ let populate = counting_populate(&queued_calls, Duration::ZERO, "own");
+ tokio::spawn(async move {
+ cache
+ .get_or_populate_in_memory(test_key("key"), populate)
+ .await
+ })
+ };
+
+ // Cut off at the cap, no retry: invoked once despite never completing.
+ assert_eq!(
+ slow.await.unwrap(),
+ Err(StorageError::CachePopulateTimedOut.to_string())
+ );
+ assert_eq!(slow_calls.load(Ordering::SeqCst), 1);
+
+ // The failed turn still ended at the cap, so the queued caller gets its own turn
+ // against an empty cache and populates for itself.
+ assert_eq!(queued.await.unwrap(), Ok("own".to_string()));
+ assert_eq!(queued_calls.load(Ordering::SeqCst), 1);
+ }
+
+ #[tokio::test]
+ async fn a_populate_failure_fails_only_that_caller_not_a_queued_one() {
+ let cache = Arc::new(Cache::new("test", 1800, 1800, None, TEST_POPULATE_TIMEOUT));
+ let failing_calls = Arc::new(AtomicUsize::new(0));
+ let queued_calls = Arc::new(AtomicUsize::new(0));
+
+ let failing = {
+ let cache = Arc::clone(&cache);
+ // Delayed, so the second caller below arrives while this is still the active
+ // turn, not after it has already finished and freed the key.
+ let populate = failing_populate(&failing_calls, Duration::from_millis(100), "boom");
+ tokio::spawn(async move {
+ cache
+ .get_or_populate_in_memory(test_key("key"), populate)
+ .await
+ })
+ };
+
+ tokio::time::sleep(Duration::from_millis(20)).await;
+
+ let queued_started = std::time::Instant::now();
+ let queued = {
+ let cache = Arc::clone(&cache);
+ let populate = counting_populate(&queued_calls, Duration::ZERO, "own");
+ tokio::spawn(async move {
+ cache
+ .get_or_populate_in_memory(test_key("key"), populate)
+ .await
+ })
+ };
+
+ assert_eq!(failing.await.unwrap(), Err("boom".to_string()));
+ assert_eq!(failing_calls.load(Ordering::SeqCst), 1);
+
+ // Proves real queuing rather than lucky scheduling: the second caller's own populate
+ // has no delay, so an uncontended run would resolve in well under 50ms.
+ assert_eq!(queued.await.unwrap(), Ok("own".to_string()));
+ assert!(queued_started.elapsed() >= Duration::from_millis(50));
+ assert_eq!(queued_calls.load(Ordering::SeqCst), 1);
+ }
+
+ #[tokio::test]
+ async fn distinct_keys_never_block_each_other() {
+ let cache = Arc::new(Cache::new("test", 1800, 1800, None, TEST_POPULATE_TIMEOUT));
+ let calls = Arc::new(AtomicUsize::new(0));
+
+ let populates = (0..8).map(|index| {
+ let cache = Arc::clone(&cache);
+ let populate =
+ counting_populate(&calls, Duration::from_millis(50), format!("val{index}"));
+ tokio::spawn(async move {
+ cache
+ .get_or_populate_in_memory(test_key(&format!("key{index}")), populate)
+ .await
+ })
+ });
+
+ for (index, result) in futures::future::join_all(populates)
+ .await
+ .into_iter()
+ .enumerate()
+ {
+ assert_eq!(result.unwrap(), Ok(format!("val{index}")));
+ }
+ // Every key is distinct, so every one of them populates.
+ assert_eq!(calls.load(Ordering::SeqCst), 8);
+ }
+
/// The `[cache]` documentation in `config/*.toml` promises this syntax.
#[test]
fn the_documented_toml_syntax_deserializes() {
@@ -881,7 +1310,7 @@ mod cache_tests {
#[tokio::test]
async fn construct_and_get_cache() {
- let cache = Cache::new("test", 1800, 1800, None);
+ let cache = Cache::new("test", 1800, 1800, None, TEST_POPULATE_TIMEOUT);
cache
.push(
CacheKey {
@@ -904,7 +1333,7 @@ mod cache_tests {
#[tokio::test]
async fn eviction_on_size_test() {
- let cache = Cache::new("test", 2, 2, Some(0));
+ let cache = Cache::new("test", 2, 2, Some(0), TEST_POPULATE_TIMEOUT);
cache
.push(
CacheKey {
@@ -927,7 +1356,7 @@ mod cache_tests {
#[tokio::test]
async fn invalidate_cache_for_key() {
- let cache = Cache::new("test", 1800, 1800, None);
+ let cache = Cache::new("test", 1800, 1800, None, TEST_POPULATE_TIMEOUT);
cache
.push(
CacheKey {
@@ -958,7 +1387,7 @@ mod cache_tests {
#[tokio::test]
async fn eviction_on_time_test() {
- let cache = Cache::new("test", 2, 2, None);
+ let cache = Cache::new("test", 2, 2, None, TEST_POPULATE_TIMEOUT);
cache
.push(
CacheKey {
@@ -968,7 +1397,7 @@ mod cache_tests {
"val".to_string(),
)
.await;
- tokio::time::sleep(std::time::Duration::from_secs(3)).await;
+ tokio::time::sleep(Duration::from_secs(3)).await;
assert_eq!(
cache
.get_val::<String>(CacheKey {