Redis Queue Cleanup
This guide explains how to configure and manage Redis memory usage for BullMQ queues in OsmoX.The Problem
BullMQ queues store completed and failed jobs in Redis indefinitely by default. Over time, this can cause:- High Redis memory usage
- Slower queue operations
- Potential Redis out-of-memory errors
Solution
OsmoX supports automatic cleanup of completed and failed jobs through configuration.Configuration
Environment Variables
Add these to your.env file:
How It Works
When a job completes successfully, BullMQ automatically removes older completed jobs, keeping only the last N jobs.
When a job fails, BullMQ automatically removes older failed jobs, keeping only the last N jobs.
Recommended Values
| Environment | REDIS_REMOVE_ON_COMPLETE | REDIS_REMOVE_ON_FAIL | Reasoning |
|---|---|---|---|
| Development | 50-100 | 500-1000 | Keep enough for debugging |
| Production | 100-500 | 1000-5000 | Balance debugging and memory |
| High Volume | 10-50 | 100-500 | Minimize memory with high throughput |
Factors to Consider
- Debugging needs: Keep more jobs if you need to debug issues
- Traffic volume: Higher volume = more aggressive cleanup
- Available Redis memory: Less memory = more aggressive cleanup
- Retention requirements: Compliance needs may require keeping jobs longer
Manual Cleanup
Using the API Endpoint
OsmoX provides a REST endpoint for manual cleanup:Using the Cleanup Script
For cleaning up existing jobs accumulated before this feature was enabled:Example Script Output
Example Script Output
Monitoring Redis Memory
Check Memory Usage
used_memory_human: Total memory used by Redisused_memory_peak_human: Peak memory usagemaxmemory: Maximum memory limit (if configured)
Check Queue Job Counts
Best Practices
1
Start Conservative
Start with higher values (e.g., 500 completed, 2000 failed) and reduce based on available Redis memory and actual debugging needs.
2
Keep More Failed Jobs
Failed jobs are more important for debugging than completed ones. Keep a higher count of failed jobs.
3
Regular Monitoring
Set up monitoring for Redis memory usage trends, queue job counts, and application error rates.
4
One-Time Cleanup After Migration
When first enabling this feature:
- Run the manual cleanup script to remove existing jobs
- Restart the API to apply new configuration
- Monitor Redis memory to verify it’s working
5
Consider Archiving
If you need to keep job history for compliance, use the notification archival feature already in OsmoX.
Troubleshooting
Memory Still High After Configuration
Memory Still High After Configuration
Problem: Redis memory doesn’t decrease after adding configuration.Solution: The cleanup only applies to new jobs. Run the manual cleanup:
Jobs Cleaned Up Too Aggressively
Jobs Cleaned Up Too Aggressively
Problem: Need more job history for debugging.Solution: Increase the retention counts:
Script Fails with Connection Error
Script Fails with Connection Error
Problem: Can’t connect to Redis.Solution:
- Verify Redis is running:
redis-cli ping - Check
.envhas correctREDIS_HOSTandREDIS_PORT - Ensure no firewall blocking the connection
Disable Cleanup Temporarily
Disable Cleanup Temporarily
Problem: Need to keep all jobs temporarily.Solution: Set both values to 0:Then restart the API.