Storage Backends
Pluggable storage for FSM state persistence. Choose between in-memory for development, Redis for production, or implement your own.
Choosing a Backend
| Backend | Persistence | Distributed | Best For |
|---|---|---|---|
MemoryStorage | ❌ In-process only | ❌ Single instance | Development, testing, small bots |
RedisStorage | ✅ Survives restarts | ✅ Multi-instance | Production, distributed bots |
| Custom | You decide | You decide | Specialized requirements |
BaseStorage Interface
All storage backends implement this abstract interface:
| Method | Description |
|---|---|
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 TTLRedis (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:"))