feat: add Hanzo Redis config with caching patterns

- Add redis.conf with performance tuning
- Add compose.yml for Redis Stack + exporter
- Add LLM.md with caching patterns documentation
- Configure for sessions, rate limiting, LLM caching
This commit is contained in:
Zach Kelling
2026-01-19 02:58:19 -08:00
parent c42d07a76e
commit 2761a2bd7b
3 changed files with 277 additions and 0 deletions
+167
View File
@@ -0,0 +1,167 @@
# Hanzo Memory (Redis Fork)
## Overview
**Hanzo Memory** is a fork of Redis optimized for the Hanzo AI platform's caching and real-time needs. It provides:
- **Session Storage** - User sessions, API keys
- **Rate Limiting** - Token bucket, sliding window
- **Real-time PubSub** - WebSocket notifications
- **Caching Layer** - LLM responses, embeddings
- **Queue Management** - Background job processing
Repository: https://github.com/hanzoai/redis
## Quick Start
```bash
# Start Redis with Hanzo config
cd hanzo
docker compose up -d
# Connect to Redis
docker exec -it hanzo-redis redis-cli
# Test connection
PING
```
## Hanzo Modules
Pre-configured modules:
- **RedisJSON** - Native JSON support
- **RediSearch** - Full-text search
- **RedisTimeSeries** - Time-series data
- **RedisBloom** - Probabilistic data structures
## Key Namespaces
```
hanzo:session:{session_id} - User sessions
hanzo:api_key:{key_hash} - API key data
hanzo:rate:{org_id}:{endpoint} - Rate limit counters
hanzo:cache:llm:{hash} - LLM response cache
hanzo:cache:embed:{hash} - Embedding cache
hanzo:queue:{queue_name} - Job queues
hanzo:pubsub:{channel} - Real-time channels
```
## Integration Points
### With hanzo/console (LangFuse fork)
Console uses Redis for caching:
```env
REDIS_URL=redis://localhost:6379
```
### With hanzo/llm (LiteLLM fork)
LLM Gateway caches responses:
```env
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=hanzo_dev
```
### With hanzo/iam (Casdoor fork)
IAM uses for session storage:
```env
redisEndpoint=localhost:6379
```
## Syncing with Upstream
```bash
# Fetch upstream changes
git fetch upstream
# Merge upstream unstable
git merge upstream/unstable
# Keep hanzo/ directory
git checkout --ours hanzo/
git push origin unstable
```
## Performance Tuning
### Memory Management
```
# redis.conf
maxmemory 2gb
maxmemory-policy allkeys-lru
# Persistence
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
```
### Client Limits
```
# Connection limits
maxclients 10000
timeout 300
# Slow log
slowlog-log-slower-than 10000
slowlog-max-len 128
```
## Docker Compose
See `hanzo/compose.yml` for local development with:
- Redis Stack (includes modules)
- RedisInsight for management
- Prometheus metrics export
## Caching Patterns
### LLM Response Caching
```python
# Cache key: sha256(model + prompt + params)
cache_key = f"hanzo:cache:llm:{hash}"
ttl = 3600 # 1 hour
# Set with JSON
redis.json().set(cache_key, "$", response)
redis.expire(cache_key, ttl)
```
### Rate Limiting (Sliding Window)
```python
# Key: hanzo:rate:{org_id}:{endpoint}
key = f"hanzo:rate:{org_id}:chat_completions"
window_seconds = 60
max_requests = 100
# Lua script for atomic operation
```
### Session Storage
```python
# Key: hanzo:session:{session_id}
session_key = f"hanzo:session:{session_id}"
ttl = 86400 # 24 hours
redis.json().set(session_key, "$", session_data)
redis.expire(session_key, ttl)
```
## Related Repositories
- **hanzo/console** - AI observability (caching)
- **hanzo/llm** - LLM Gateway (response cache)
- **hanzo/iam** - Identity management (sessions)
- **hanzo/datastore** - ClickHouse fork (OLAP)
- **hanzo/relational** - PostgreSQL fork (OLTP)
+42
View File
@@ -0,0 +1,42 @@
services:
redis:
image: redis/redis-stack:latest
container_name: hanzo-redis
ports:
- "6379:6379" # Redis
- "8001:8001" # RedisInsight
volumes:
- redis-data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf:ro
environment:
REDIS_ARGS: "--requirepass ${REDIS_PASSWORD:-hanzo_dev}"
healthcheck:
test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD:-hanzo_dev}", "ping"]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
command: >
redis-stack-server
--appendonly yes
--maxmemory 2gb
--maxmemory-policy allkeys-lru
exporter:
image: oliver006/redis_exporter:latest
container_name: hanzo-redis-exporter
ports:
- "9121:9121"
environment:
REDIS_ADDR: redis://redis:6379
REDIS_PASSWORD: ${REDIS_PASSWORD:-hanzo_dev}
depends_on:
- redis
restart: unless-stopped
volumes:
redis-data:
networks:
default:
name: hanzo-memory
+68
View File
@@ -0,0 +1,68 @@
# Hanzo Memory - Redis Configuration
# Network
bind 0.0.0.0
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 300
# Memory
maxmemory 2gb
maxmemory-policy allkeys-lru
maxmemory-samples 5
# Persistence
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
# Append-only file
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# Logging
loglevel notice
logfile ""
# Clients
maxclients 10000
# Slow log
slowlog-log-slower-than 10000
slowlog-max-len 128
# Event notifications (for pub/sub)
notify-keyspace-events "Ex"
# Lua scripting
lua-time-limit 5000
# Latency monitoring
latency-monitor-threshold 100
# Active defragmentation
activedefrag yes
active-defrag-ignore-bytes 100mb
active-defrag-threshold-lower 10
active-defrag-threshold-upper 100
active-defrag-cycle-min 1
active-defrag-cycle-max 25
# Thread I/O
io-threads 4
io-threads-do-reads yes
# Module loading (redis-stack includes these)
# loadmodule /opt/redis-stack/lib/rejson.so
# loadmodule /opt/redis-stack/lib/redisearch.so
# loadmodule /opt/redis-stack/lib/redistimeseries.so
# loadmodule /opt/redis-stack/lib/redisbloom.so