Redis Alternatives Implementation Guide¶
Overview¶
This guide explains how to replace expensive Redis infrastructure with cost-effective alternatives while maintaining the same caching functionality.
Cost Comparison¶
| Solution | Monthly Cost | Performance | Persistence | Scalability |
|---|---|---|---|---|
| Redis (Current) | $50-100 | Excellent | Yes | Excellent |
| File Cache | $0 | Good | Yes | Limited |
| PostgreSQL Cache | $0 | Good | Yes | Good |
| Hybrid (File + Postgres) | $0 | Good | Yes | Good |
Available Cache Implementations¶
1. File-Based Cache (Immediate 90% Cost Reduction)¶
Best for: Development, staging, small production deployments
Pros: - Zero infrastructure cost - Simple implementation - Persists across restarts - TTL support
Cons: - Single-server only - File I/O performance - No distributed caching
Configuration:
2. PostgreSQL Cache (85% Cost Reduction + Better Performance)¶
Best for: Production environments, better performance than file cache
Pros: - Uses existing database infrastructure - Better performance than file cache - Supports transactions - Can be distributed (with read replicas) - TTL support with automatic cleanup
Cons: - Slightly more complex than file cache - Database overhead
Configuration:
3. Auto-Detection (Recommended)¶
Best for: Flexible deployment
Configuration:
Auto-detection logic: 1. If Redis is available and configured → Use Redis 2. If PostgreSQL is available → Use PostgreSQL cache 3. Otherwise → Use File cache
Implementation Steps¶
Step 1: Deploy New Cache Implementations¶
-
Update Environment Variables:
-
Run Database Migration (if using PostgreSQL cache):
Step 2: Update Infrastructure¶
-
Remove Redis from Docker Compose (optional):
-
Update Environment Variables:
Step 3: Update Cloud Infrastructure¶
For GCP (Remove Memorystore):¶
-
Update Terraform (
infra/terraform_gcp/redis.tf): -
Update Cloud Run Configuration (
infra/terraform_gcp/cloud_run.tf):
For AWS (Remove ElastiCache):¶
-
Update Terraform (
infra/terraform_aws/elasticache.tf): -
Update ECS Configuration (
infra/terraform_aws/ecs.tf):
Step 4: Deploy and Test¶
-
Deploy the changes:
-
Test the application:
Migration Strategies¶
Strategy 1: Gradual Migration (Recommended)¶
- Phase 1: Deploy with
CACHE_TYPE=auto(keeps Redis if available) - Phase 2: Switch to
CACHE_TYPE=postgresin staging - Phase 3: Test performance and functionality
- Phase 4: Switch production to
CACHE_TYPE=postgres - Phase 5: Remove Redis infrastructure
Strategy 2: Immediate Migration (Maximum Cost Savings)¶
- Deploy with
CACHE_TYPE=filefor immediate savings - Remove Redis infrastructure immediately
- Upgrade to PostgreSQL cache when ready
Strategy 3: Hybrid Approach¶
- Keep Redis for critical caching (AI responses)
- Use PostgreSQL cache for less critical data
- Implement cache routing based on data type
Performance Optimization¶
File Cache Optimization¶
# Use environment variables for file cache configuration
CACHE_DIR=/tmp/macumba_cache # Use fast storage
CACHE_CLEANUP_INTERVAL=100 # Cleanup frequency
PostgreSQL Cache Optimization¶
-- Create indexes for better performance
CREATE INDEX CONCURRENTLY idx_cache_entries_expires_at_not_null
ON cache_entries (expires_at) WHERE expires_at IS NOT NULL;
-- Optimize cleanup queries
CREATE INDEX CONCURRENTLY idx_cache_entries_cleanup
ON cache_entries (expires_at) WHERE expires_at < NOW();
Monitoring and Observability¶
-
Add cache metrics:
-
Set up alerts:
- Cache hit rate below 70%
- Cache size growing too large
- Cache cleanup failures
Troubleshooting¶
Common Issues¶
-
Cache not working:
-
Performance issues:
-
Database migration issues:
Recovery Procedures¶
-
Rollback to Redis:
-
Clear cache:
Expected Cost Savings¶
Monthly Cost Reduction¶
| Environment | Current Redis Cost | New Cost | Savings |
|---|---|---|---|
| Development | $15-30 | $0 | $15-30 (100%) |
| Staging | $25-50 | $0 | $25-50 (100%) |
| Production | $50-100 | $0 | $50-100 (100%) |
| Total | $90-180 | $0 | $90-180 (100%) |
Performance Comparison¶
| Metric | Redis | PostgreSQL Cache | File Cache |
|---|---|---|---|
| Get Operation | 0.1ms | 2-5ms | 1-3ms |
| Set Operation | 0.1ms | 3-8ms | 2-5ms |
| Memory Usage | High | Low | Low |
| Disk Usage | Low | Medium | Medium |
| Network Usage | Medium | Low | None |
Conclusion¶
Implementing Redis alternatives can save $90-180+ per month (100% Redis cost reduction) while maintaining equivalent functionality. The PostgreSQL cache provides the best balance of performance and cost savings for most production deployments.
Support¶
For questions or issues: 1. Check application logs for cache-related errors 2. Verify database connectivity (for PostgreSQL cache) 3. Test cache health endpoint 4. Review this guide for troubleshooting steps