Repository navigation
Closed
Labels
kind/discussionCategory issues related to discussionCategory issues related to discussion
Description
Background
McpCacheIndex is the cache abstraction used by MCP-related query paths, and its default implementation is MemoryMcpCacheIndex.
The current implementation maintains:
- in-memory
name -> idandid -> dataindexes - a hand-rolled LRU doubly linked list
- capacity eviction
- periodic cleanup
However, the current design has a concurrency and maintainability problem:
- cache read paths (
getMcpId,getMcpServerById) also update LRU order - this makes reads effectively carry write semantics
- the implementation currently relies on
ReadWriteLockplussynchronizedlinked-list operations to keep the list consistent - under high-QPS read scenarios, this can become a hotspot and reduces the benefit of concurrent reads
Problem
There are three main concerns in the current implementation:
-
Concurrency model mismatch
- read operations mutate the LRU list
- multiple concurrent readers can still contend on internal mutation paths
-
Performance hotspot
synchronizedprotection around LRU node movement serializes hot reads- this can degrade throughput in MCP lookup scenarios
-
Complexity and consistency risk
- the cache maintains two maps plus a custom linked list
- expiration, eviction, and invalidation need to keep multiple structures consistent
- removing by ID may require extra cleanup work to keep
name -> idmappings correct
Proposal
Replace the hand-written LRU implementation in MemoryMcpCacheIndex with a Caffeine-backed local cache while keeping the McpCacheIndex interface unchanged.
Recommended direction:
- keep
McpCacheIndexAPI unchanged - keep
MemoryMcpCacheIndexas the default implementation class to minimize integration churn - use Caffeine for:
- maximum size based eviction
- expire-after-write semantics
- access tracking / eviction policy management
- keep
name -> idmapping as a lightweight secondary index - use cache removal callbacks to clean related name mappings and preserve index consistency
- keep existing configuration properties as much as possible to avoid operational impact
Expected Benefits
- remove read-path manual LRU mutation and lock contention
- reduce custom concurrent data structure complexity
- improve maintainability and correctness
- keep changes localized to the
aimodule and minimize PR merge conflict risk - preserve current business-facing interface and wiring
Compatibility Notes
To reduce review risk, this refactor should:
- avoid changing
McpCacheIndexconsumers - preserve current configuration names
- prefer
expireAfterWriteto stay close to current semantics - add focused tests for:
- eviction behavior
- expiration cleanup
name/idindex consistency- concurrent read/write access
- shutdown behavior
Scope
Impacted areas are expected to be mainly inside the ai module:
MemoryMcpCacheIndexMcpServerIndexConfigurationai/pom.xml- related cache tests
Metadata
Metadata
Assignees
Labels
kind/discussionCategory issues related to discussionCategory issues related to discussion
Activity
YutShang commentedon Apr 15, 2026
I have resloved it #14933
KomachiSion commentedon Apr 15, 2026
Great suggestion, But I see the solution is use new depend
Caffeine.Why not use the guava LRU?
YutShang commentedon Apr 15, 2026
Thanks for the suggestion.
I did consider Guava Cache as well. My main reason for leaning toward Caffeine is that this issue is not just about replacing the LRU logic, but also about reducing contention on the read path.
Compared with Guava Cache, Caffeine is generally a better fit for high-concurrency local cache scenarios, especially when cache access is on a hot path. Since
MemoryMcpCacheIndexsits in the MCP lookup path, I felt it was safer to choose the more cache-focused implementation here.Another reason is maintainability. If we are already replacing the hand-written concurrent LRU logic with a library, my preference is to use the option that is more specialized for this kind of use case, while still keeping the change local:
McpCacheIndexThat said, I do not strongly depend on Caffeine itself. The main goal of this proposal is to remove the custom concurrent LRU logic and avoid the current read-path synchronization hotspot. If the project prefers Guava for dependency consistency, Iโm happy to adjust the implementation in that direction.
KomachiSion commentedon Apr 16, 2026
Infact nacos common depend guava by grpc, and nacos server core depend common so most of nacos server module will auto depend guava.
I see the todo comment also keep the
the LRU cache should be re-designed or use stabled high performance LRU cache such as guava.So I think don't need add new depend
Caffeine, use guava LRU to refactor this will be better. How about you?[ISSUE alibaba#14932] Refactor MemoryMcpCacheIndex to remove read-patโฆ
[ISSUE #14932] Refactor MemoryMcpCacheIndex to remove read-path LRU cโฆ