feat: use custom storage service if found #6094
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Relates to
Addresses the need for pluggable storage backends for production deployments and fixes documentation discrepancy for session storage.
Risks
Medium
Affected areas:
Background
What does this PR do?
This PR introduces a pluggable Key-Value Store Service interface (
IKVStoreService) that allows developers to implement custom storage backends for state caching and session management. The implementation:Adds new interfaces in
packages/core/src/types/store.ts:IKVStore<T, M>: Generic key-value store interface withget,set,delete,entries, and optionalgetMetricsmethodsIKVStoreService: Service interface for obtaining named KV storesisKVStoreService: Type guard helper functionIntegrates KV Store into runtime:
Fixes documentation discrepancy:
What kind of change is this?
Why are we doing this? Any context or related work?
Motivation 1: Production Scalability with Replicas
In production environments, agents can throttle CPU usage when handling high loads. Running multiple replicas is a common solution, but the current in-memory-only storage causes issues:
With this KV Store interface, users can implement Redis, Memcached, or other distributed storage solutions to maintain state across replicas without losing data.
Motivation 2: Documentation Accuracy
The Sessions API documentation explicitly mentions in-memory storage as the default:
However, this was never actually implemented as a proper fallback. This PR corrects this discrepancy by providing a true in-memory implementation when no custom KV store service is registered.
Documentation changes needed?
IKVStoreServiceTesting
Where should a reviewer start?
packages/core/src/types/store.tspackages/core/src/runtime.tsfor state cachingpackages/server/src/api/messaging/sessions.tsDetailed testing steps
Test 1: Default In-Memory Behavior (No Custom Service)
Test 2: State Cache with Default Storage
Test 3: Custom KV Store Service (Integration Test)
getMetrics()works if implementedentries()async generator functionalityTest 4: Production Replica Scenario (Manual)
Database changes
None. This PR adds an abstraction layer but does not modify database schemas.
Deploy Notes
Deployment instructions
For existing deployments: No action required. The default in-memory behavior maintains backward compatibility.
For production deployments with replicas:
IKVStoreService(e.g., Redis-backed)getMetrics()is implementedEnvironment variables to consider adding:
KV_STORE_BACKEND(e.g., "redis", "memory")REDIS_URLor similar connection strings for external storesKV_STORE_TTLfor automatic expiration policiesDiscord username
alexd000