Redis is now mandatory for production deployments. The application will refuse to start if Redis connectivity cannot be established, ensuring quotas, rate limiting, and background job queues work correctly in distributed environments.
Redis provides essential infrastructure for:
- Distributed Rate Limiting: Prevent quota bypass across multiple workers
- Counter Synchronization: Track API usage across load-balanced instances
- Background Job Queue: Async validation processing with RQ
- Multi-Worker Safety: Shared state for horizontal scaling
# Required - Application will fail to start without this
REDIS_URL=redis://localhost:6379
# Or with authentication
REDIS_URL=redis://:password@localhost:6379
# Or Redis Cloud
REDIS_URL=redis://username:password@host:portFor local development without Redis:
DEMO_MODE=trueDEMO_MODE uses ephemeral in-memory backends. Data is lost on restart. NOT safe for production.
✓ Counter backend initialized (Redis)
✓ Queue service initialized (Redis)
→ Application starts
❌ Redis validation failed: REDIS_URL environment variable is required
→ Application REFUSES to start
⚠ Using in-memory counters (DEMO_MODE)
⚠ Using in-memory queue (DEMO_MODE)
→ Application starts with warnings
Main health check with Redis status:
{
"status": "healthy",
"version": "2.0.0",
"environment": "production",
"redis": {
"status": "connected",
"backend": "redis"
},
"queue": {
"status": "connected",
"backend": "redis",
"queue_depth": 0
}
}Returns HTTP 503 if Redis is down in production.
Detailed Redis monitoring:
{
"counter_backend": {
"status": "connected",
"backend": "redis"
},
"queue_service": {
"status": "connected",
"backend": "redis",
"queue_depth": 0
},
"demo_mode": false
}Local Development:
# macOS
brew install redis
brew services start redis
# Ubuntu/Debian
sudo apt-get install redis-server
sudo systemctl start redis
# Docker
docker run -d -p 6379:6379 redis:7-alpinePython Dependencies:
pip install redis[hiredis] rq- Render: Add Redis add-on to your service
- Railway: Add Redis database
- Upstash: Serverless Redis
- Redis Cloud: Managed Redis hosting
Run the Redis connectivity tests:
# With DEMO_MODE (uses in-memory fallback)
DEMO_MODE=true pytest tests/test_redis_mandatory.py -v
# Production mode tests (requires Redis)
REDIS_URL=redis://localhost:6379 pytest tests/test_redis_mandatory.py -v-
Set DEMO_MODE for existing dev environments:
# .env DEMO_MODE=true -
Install Redis for production:
- Add Redis add-on to hosting platform
- Update
REDIS_URLenvironment variable - Remove
DEMO_MODE(or set tofalse)
-
Verify health checks:
curl http://localhost:8000/health curl http://localhost:8000/admin/health/redis
-
Monitor startup logs:
✓ Redis counter backend connected: localhost:6379 ✓ Counter backend initialized ✓ Redis queue service connected ✓ Queue service initialized
Cause: Running in production mode without Redis configured.
Solutions:
- Set
REDIS_URLenvironment variable - Or enable
DEMO_MODE=truefor local development only
Cause: Redis server is not running or unreachable.
Solutions:
- Start Redis server:
redis-serverorbrew services start redis - Check Redis is listening:
redis-cli ping(should returnPONG) - Verify
REDIS_URLis correct - Check firewall/network connectivity
Cause: Redis connectivity lost during runtime.
Solutions:
- Check Redis server status
- Review Redis logs
- Restart application to re-establish connection
Cause: DEMO_MODE=true is enabled.
Impact:
- Rate limits and quotas NOT synchronized across workers
- Background jobs NOT durable across restarts
- Data loss on application restart
Solution: Deploy Redis and disable DEMO_MODE for production
| Variable | Required | Default | Description |
|---|---|---|---|
REDIS_URL |
Yes (prod) | - | Redis connection string |
DEMO_MODE |
No | false |
Allow in-memory fallback for local dev |
ENVIRONMENT |
No | development |
Environment name (logs/monitoring) |
app/services/state_backend.py- Enforces Redis for counter backendapp/services/queue.py- Enforces Redis for job queueapp/main.py- Validates Redis on startup.env.example- Documents mandatory Redis configurationtests/test_redis_mandatory.py- Comprehensive test suite
┌─────────────────────────────────────────────────────────┐
│ FastAPI Application │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ Startup Validation │ │
│ │ 1. Check DEMO_MODE │ │
│ │ 2. Validate REDIS_URL │ │
│ │ 3. Test Redis connectivity (ping) │ │
│ │ 4. Initialize counter backend │ │
│ │ 5. Initialize queue service │ │
│ │ → Fail fast if Redis unavailable in production │ │
│ └──────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Rate Limiter │──Redis──→ │ Counters │ │
│ └──────────────┘ └──────────────┘ │
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Queue Worker │──Redis──→ │ Background │ │
│ │ │ │ Jobs (RQ) │ │
│ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────┘
▲
│
│ Health Checks
│ /health
│ /admin/health/redis
▼
┌──────────┐
│ Redis │
│ Server │
└──────────┘
- Redis should not be exposed to the internet
- Use authentication:
REDIS_URL=redis://:password@host:port - Enable TLS for cloud deployments
- Restrict network access with firewall rules
- Monitor Redis logs for unauthorized access attempts
- Redis adds ~1ms latency per operation
- Use connection pooling (handled by redis-py automatically)
- Monitor Redis memory usage
- Consider Redis cluster for high-traffic deployments
For issues related to Redis connectivity:
- Check application logs for detailed error messages
- Verify Redis server is running:
redis-cli ping - Test connectivity:
redis-cli -u $REDIS_URL ping - Review health endpoint:
GET /health - File an issue with logs and Redis configuration