[BUG] Payout routing cache invalidated under the payments key in the v2 profile-scoped activate/deactivate path
Found by code inspection on main @ 6482aa5fe5, while tracing how ROUTING_CACHE and CGRAPH_CACHE are invalidated. Not reachable through any route today — see "Why this is latent" — but it is a landmine for whoever wires up v2 payouts routing.
Bug Description
ROUTING_CACHE stores the active routing algorithm under two different key shapes, chosen by transaction type (core/payments/routing.rs:1996-2010):
TransactionType::Payment => format!("routing_config_{merchant_id}_{profile_id}"),
TransactionType::Payout => format!("routing_config_po_{merchant_id}_{profile_id}"),ProfileWrapper::update_profile_and_invalidate_routing_config_for_active_algorithm_id_update (core/admin.rs:4580) branches on transaction_type to pick which column to write:
let (routing_algorithm_id, payout_routing_algorithm_id) = match transaction_type {
TransactionType::Payment => (Some(algorithm_id), None),
#[cfg(feature = "payouts")]
TransactionType::Payout => (None, Some(algorithm_id)),
...
};…but it derives its invalidation key from get_routing_config_cache_key() (core/admin.rs:4565), which is hardcoded to the payments shape and ignores transaction_type entirely:
fn get_routing_config_cache_key(self) -> CacheKind<'static> {
CacheKind::Routing(format!("routing_config_{}_{}", merchant_id, profile_id).into())
}Expected Behavior
Activating or deactivating a payout routing algorithm should invalidate routing_config_po_{merchant_id}_{profile_id}, so the next payout request rebuilds ROUTING_CACHE from the updated profile.
Actual Behavior
Two faults, both from the same hardcoded key:
- The payouts key is never invalidated.
ROUTING_CACHEkeeps serving the previous payout routing algorithm until the entry expires — 30 minutes on the defaultCACHE_TTL. The DB write succeeds, so the API reports success while routing continues on the old algorithm. - The payments key is invalidated needlessly. A payout-only change flushes the payments routing entry, costing an avoidable redis/DB rebuild on the next payment.
Why this is latent (and why it is still worth fixing)
The only callers are link_routing_config_under_profile and unlink_routing_config_under_profile (core/routing.rs:1060, :1722), both #[cfg(feature = "v2")]. The v2 route tree passes &TransactionType::Payment as a literal (routes/app.rs:2646, :2659), so the Payout arm is unreachable in practice.
Every TransactionType::Payout routing wiring (routes/app.rs:1304-1396) is v1, and v1 goes through link_routing_config / unlink_routing_config, which additionally call helpers::redact_routing_cache — and that one publishes both key shapes (core/routing/helpers.rs:2936). So v1 is correct today. The bug fires the first time a v2 payouts routing route is added, and it will fail silently: stale routing, no error anywhere.
Secondary: a comment that does not match its code
core/routing/helpers.rs:271, in update_profile_active_algorithm_ref:
// Invalidate the routing cache for Payments and Payouts transaction types
if !transaction_type.is_three_ds_authentication() {
let routing_cache_key = cache::CacheKind::Routing(
format!("routing_config_{}_{}", merchant_id, profile_id).into(), // payments only
);It publishes the payments key only. This is currently harmless — both callers redundantly call redact_routing_cache right afterwards, which covers both shapes — but the comment claims coverage the code does not provide, and the redundancy is what is hiding it.
Steps To Reproduce
Not reproducible via the API on main; the affected arm is unreachable (above). Reachable by inspection, or by adding a v2 route that passes TransactionType::Payout to link_routing_config_under_profile:
- Add a v2 route wiring
routing_link_configwith&TransactionType::Payout. - Activate payout routing algorithm A on a profile; issue a payout so
routing_config_po_{m}_{p}is cached. - Activate payout routing algorithm B via that route.
- Issue another payout within
CACHE_TTL— it still routes by algorithm A.
Suggested fix
Make the key follow the transaction type, e.g. pass transaction_type into get_routing_config_cache_key, or simply delegate to helpers::redact_routing_cache, which already emits both shapes and is the established helper for this.
Not affected
CGRAPH_CACHE and PM_FILTERS_CGRAPH_CACHE are fine. create_connector, update_connector and delete_connector all call redact_cgraph_cache (core/admin.rs:2856, :3113, :3228), which publishes both cgraph_{m}_{p} and cgraph_po_{m}_{p}. PM_FILTERS_CGRAPH_CACHE has only one key shape, so it has nothing to miss.
Environment
Code inspection against main @ 6482aa5fe5; not environment-specific. Requires the payouts feature (on by default via common_default) for the affected arm to compile at all.
Source: juspay/hyperswitch