Storage Backends

Pluggable storage for FSM state persistence. Choose between in-memory for development, Redis for production, or implement your own.

Choosing a Backend

BackendPersistenceDistributedBest For
MemoryStorage❌ In-process only❌ Single instanceDevelopment, testing, small bots
RedisStorage✅ Survives restarts✅ Multi-instanceProduction, distributed bots
CustomYou decideYou decideSpecialized requirements

BaseStorage Interface

All storage backends implement this abstract interface:

MethodDescription
set_state(id, state, ttl=None)Store state data with optional TTL
get_state(id)Retrieve state data by identifier
delete_state(id)Remove state data
compare_and_set(id, new, expected, ttl)Atomic compare-and-swap for concurrency safety
list_keys(pattern)List all keys matching a glob pattern
clear_namespace()Remove all keys in the storage namespace
start() / stop()Lifecycle hooks for connection management
health()Health check returning True if storage is operational

Quick Setup

Memory (development)
from pyrogram_patch.fsm import MemoryStorage

manager = await patch(app)
manager.set_storage(MemoryStorage(default_ttl=3600))  # 1h TTL
Redis (production)
import redis.asyncio as redis
from pyrogram_patch.fsm import RedisStorage

redis_client = redis.Redis(host="localhost", port=6379)
manager = await patch(app)
manager.set_storage(RedisStorage(redis_client, prefix="mybot:"))