Overview
TheSessionKVStore is an abstract base class that defines the interface for TTL (time-to-live) key-value storage backends used by the SessionRegistry. The storage layer is a generic key-value store that works with bytes and knows nothing about nodes or sessions — the SessionRegistry owns the key scheme and all serialization.
Implementations should use TTL-based key expiry. Records that are not refreshed within the TTL period are considered expired and may be garbage-collected by the backend.
Key Conventions
The following key conventions are managed bySessionRegistry:
sessions/{call_id}/{session_id}→ JSON-serialized SessionInfoclose_requests/{call_id}/{session_id}→ empty bytes (close flag)
Interface Methods
Lifecycle Methods
start
close
Storage Methods
set
ttl seconds if not refreshed.
The key to store.
The value as bytes.
Time-to-live in seconds.
When True, the write is silently skipped if the key does not already exist.
mset
(key, value, ttl) tuple. Semantics per key are the same as set.
A list of (key, value, ttl) tuples.
get
The key to retrieve.
bytes | None - The value as bytes, or None if the key does not exist or has expired.
mget
The keys to retrieve.
list[bytes | None] - A list of values (or None) in the same order as the input keys.
expire
One or more keys to update.
New time-to-live in seconds.
keys
prefix.
The key prefix to match.
list[str] - A list of matching key strings.
delete
The keys to delete.
Built-in Implementations
InMemorySessionKVStore
In-memory TTL key-value store suitable for single-node deployments, development, and testing.Seconds between periodic expired-key sweeps.
- Single-node only (not shared across processes)
- Expired keys are cleaned up both lazily (on access) and periodically (via background task)
- Lightweight and fast for local development
RedisSessionKVStore
Redis-backed TTL key-value store suitable for multi-node deployments where session state must be shared across processes or machines.An existing
redis.asyncio.Redis client. Caller owns the lifecycle. Mutually exclusive with url.A Redis connection URL (e.g.
redis://localhost:6379/0). The store creates and owns the client. Mutually exclusive with client.Prefix prepended to every key for namespacing.
- Supports multi-node deployments
- Shared session state across processes and machines
- Uses Redis native TTL features (PX, PEXPIRE)
- Non-blocking key scanning with SCAN
Context Manager Support
AllSessionKVStore implementations support async context manager protocol:
Implementing a Custom Store
To implement a custom storage backend, subclassSessionKVStore and implement all abstract methods:
Performance Considerations
- InMemorySessionKVStore: Fastest for single-node, but not suitable for multi-node deployments
- RedisSessionKVStore: Network overhead but enables multi-node coordination
- Use
msetandmgetfor batch operations to reduce round trips - The
cleanup_intervalin InMemorySessionKVStore affects memory usage vs. CPU usage tradeoff - Redis SCAN operations are non-blocking and safe for production use