Skip to main content
< All Topics
Print

Redis Operations

name: redis-operations

description: Redis usage patterns in Docker environments including caching strategies, Celery broker configuration, pub/sub, key management, and monitoring. Use when configuring Redis for caching or message brokering, debugging Redis connectivity, or optimizing cache performance.

Redis Operations

Instructions

Operate Redis instances in Docker environments for caching and message brokering.

Caching strategies:

  • Set TTL on all cache keys — no unbounded keys: SET key value EX 3600
  • Use LRU eviction policy (maxmemory-policy allkeys-lru) to prevent OOM
  • Cache invalidation: delete on write, or use short TTLs for eventually-consistent data
  • Hash types for structured objects: HSET user:123 name "Alice" email "alice@example.com"

Celery broker configuration:

  • Broker URL: redis://redis:6379/1 — use DB 1 to separate from cache on DB 0
  • Result backend: redis://redis:6379/2 for task results (separate DB again)
  • Set broker_transport_options with visibility_timeout matching your longest task duration
  • Monitor queues: redis-cli -n 1 LLEN celery to check queue depth

Pub/sub for real-time events:

  • Publish: PUBLISH channel:notifications '{"event": "update", "id": 123}'
  • Subscribe in application code; do not use redis-cli SUBSCRIBE in production
  • Pub/sub messages are fire-and-forget — use Redis Streams for durable messaging

Key naming conventions:

  • Pattern: namespace:entity:id — e.g., dify:session:abc123, iti:cache:user:456
  • Use colons as separators for visual grouping in redis-cli KEYS output
  • Prefix all keys with application namespace to prevent collisions in shared instances

Health checks:

  • Docker healthcheck: redis-cli ping — expects PONG response
  • Compose config: healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 10s timeout: 5s retries: 3

Monitoring:

  • redis-cli INFO — memory usage, connected clients, keyspace stats
  • redis-cli INFO memory — check used_memory_human and maxmemory
  • redis-cli MONITOR — real-time command stream (development only; significant performance impact)
  • redis-cli DBSIZE — total key count per database
  • Alert on memory usage >80% of maxmemory

ITI reference pattern:

  • Image: redis:7-alpine for minimal footprint
  • Shared instance serving Dify cache (DB 0) and Celery broker (DB 1)
  • Persist with AOF (appendonly yes) for durability; RDB snapshots as supplementary backup
  • Resource allocation: 512 MB maxmemory sufficient for typical ITI workloads
Table of Contents