#14499·hyperswitch

[BUG] Cache stampede on concurrent cache misses

Author: Parikalp-BhardwajCreated Sep 29, 2026Updated Sep 29, 2026
LabelsC-bugS-awaiting-triage

Bug Description

Concurrent requests for the same missing cache key can all execute the fallback at the same time.

The current flow in get_or_populate_in_memory is roughly:

L1 cache miss
→ Redis miss
→ fallback
→ populate Redis
→ populate L1

There is currently no per-key coordination between concurrent callers.

Because of this, several requests for the same key can perform the same backend work before the cache is populated.

Expected Behavior

Concurrent requests for the same cache key should share a single in-flight load.

One request should execute the fallback and populate the cache, while the remaining requests wait for the same result.

Requests for different cache keys should still be able to load independently.

Actual Behavior

Concurrent requests for the same missing key execute the fallback independently.

I reproduced this with 100 concurrent requests using the same missing Redis key:

Concurrent Redis requests: 100
Fallback executions: 100

All 100 callers executed the fallback instead of sharing a single load.

Steps To Reproduce

  1. Start a local Redis instance.

  2. Add a concurrency test around get_or_populate_redis using the same missing Redis key for every request.

  3. Start 100 concurrent tasks at roughly the same time.

  4. Increment an atomic counter inside the fallback and add a small delay to simulate a database read.

  5. Run:

bash
cargo test -p storage_impl \
  --features v1 \
  concurrent_redis_misses_measure_fallback_executions \
  -- --nocapture
  1. Observe:
Concurrent Redis requests: 100
Fallback executions: 100

The fallback is executed independently by every caller.

Context For The Bug

This cache path is used by several database-backed caches, including dynamic payment routing.

For example, fetch_dynamic_routing_configs can eventually fall back to:

find_routing_algorithm_by_profile_id_algorithm_id

when both the in-memory cache and Redis miss.

That lookup reaches PostgreSQL, which means concurrent requests for the same routing configuration can trigger duplicate database reads.

This is most relevant when the Redis key is genuinely missing, such as after:

  • cache invalidation
  • a cold Redis instance
  • Redis restart or eviction
  • accessing a new cache key for the first time

A normal L1 cache expiry is less concerning if Redis still contains the value.

Environment

Operating System:

macOS, Apple Silicon

Rust version && Cargo version::

rustc 1.99.0-nightly 

Redis:

Redis 7 running locally in Docker

Redis container:

bash
docker run --name hyperswitch-redis \
  -p 6379:6379 \
  -d redis:7

Redis check:

bash
docker exec -it hyperswitch-redis redis-cli ping

Have you spent some time checking if this bug has been raised before?

  • I checked and didn't find a similar issue

Have you read the Contributing Guidelines?

Are you willing to submit a PR?

Yes, I am willing to submit a PR!