Redis State Store

High-performance in-memory state storage for fast agent data access

Redis stores agent state in memory for sub-millisecond access — pair it with a SQL backend for conversation history.

python
from praisonaiagents import Agent, MemoryConfig
from praisonaiagents import db

agent = Agent(
    name="FastBot",
    instructions="You are a helpful assistant.",
    memory=MemoryConfig(
        db=db(state_url="redis://localhost:6379"),
        session_id="redis-session",
    ))
agent.start("Store my preferences in Redis")

The user updates session state; Redis serves fast reads while SQL can hold conversation history.

Diagram

🤖 Agent

📦 Redis State

⚡ RedisStorageAdapter

Redis

Quick Start

  1. Simple Usage
    bash
    pip install redis praisonai
    python
    from praisonaiagents import Agent, MemoryConfig
    from praisonaiagents import db
    
    agent = Agent(
        name="FastBot",
        memory=MemoryConfig(
            db=db(state_url="redis://localhost:6379"),
            session_id="session-1",
        ))
    agent.start("Hello!")
  2. With Configuration

    Hybrid setup — SQL for conversations, Redis for state:

    python
    from praisonaiagents import Agent, MemoryConfig
    from praisonaiagents import db
    
    agent = Agent(
        name="HybridBot",
        memory=MemoryConfig(
            db=db(
                database_url="postgresql://user:pass@localhost/conversations",
                state_url="redis://localhost:6379",
            ),
            session_id="hybrid-session",
        ))
    agent.start("Conversations in Postgres, state in Redis")

How It Works

Redis is a state store — fast key-value access for agent preferences and runtime data. Conversation history uses database_url separately.

Diagram
RedisRedisStateStoreAgentRedisRedisStateStoreAgentset(key, value, ttl?)SET praison:keyget(key)value
FeatureDescription
In-memory speedSub-millisecond reads and writes
TTL supportExpire keys automatically with ttl
Key prefixNamespace keys with prefix (default praison:)

Configuration Options

OptionTypeDefaultDescription
urlstrNoneFull Redis URL (overrides host/port/db/password)
hoststr"localhost"Redis host when url is not set
portint6379Redis port
dbint0Redis database number
passwordstrNoneRedis password
prefixstr"praison:"Key prefix for namespacing
decode_responsesboolTrueCompatibility flag (not used internally)
socket_timeoutint5Socket timeout in seconds
max_connectionsint10Compatibility flag for pool size

URL formats

python
db(state_url="redis://localhost:6379")
db(state_url="redis://:password@host:6379/0")

Production notes

Redis is single-threaded, so a slow admin command stalls every other client on the same instance. To list many keys, call scan_prefix() instead of keys() — it walks the keyspace with a non-blocking cursor SCAN (batches of 500) so a large read never freezes agents sharing the instance.

python
from praisonai.persistence import create_state_store

store = create_state_store("redis", url="redis://localhost:6379")

recent = store.scan_prefix("session:")
MethodUnder the hoodSafe on large keyspaces
store.keys(pattern)Redis KEYS (O(N), blocking)Small keyspaces only
store.scan_prefix(prefix)SCAN cursor (batch 500)✅
Diagram
RedisRedisStateStoreAgentRedisRedisStateStoreAgentloop[batched cursor scan]scan_prefix(“session:”)SCAN cursor MATCH session:* COUNT 500cursor, chunkkeys[]

Best Practices

Prefer scan_prefix() for large listings

scan_prefix(prefix) uses a non-blocking SCAN cursor (batch 500), so admin or garbage-collection reads on a large keyspace never stall other agents sharing the Redis instance. Reserve keys(pattern) for small keyspaces.

Pair Redis with a SQL conversation backend

Use database_url for chat history and state_url for fast ephemeral state.

Set TTL on ephemeral keys

Pass ttl when storing session-scoped data that should expire automatically.

Use a key prefix per environment

Set prefix="prod:" vs prefix="staging:" to isolate keys on a shared Redis instance.

Enable persistence for durability

Configure Redis AOF or RDB snapshots if state must survive Redis restarts.


Related