The resque-throttler gem now supports concurrent job limiting in addition to rate limiting. This prevents too many jobs from running simultaneously, which is essential for preventing database lock accumulation.
-
Configuration: Add
:concurrentoption to rate_limitResque.rate_limit(:my_queue, at: 5, per: 5, concurrent: 3)
-
Tracking: The gem tracks active jobs using Redis counters
- Increments when job starts
- Decrements when job completes (success or failure)
-
Enforcement: Workers check concurrent limit before starting new jobs
- If at limit, queue is skipped
- Job remains in queue for next worker cycle
# Check active job count
Resque.active_job_count(:my_queue)
# Check if at concurrent limit
Resque.queue_at_or_over_concurrent_limit?(:my_queue)
# Check if queue has concurrent limit configured
Resque.queue_has_concurrent_limit?(:my_queue)-
Redis Keys:
- Active jobs counter:
throttler:active_jobs:{queue_name} - Automatically cleaned up (no expiration needed)
- Active jobs counter:
-
Worker Hooks:
- Overrides
performmethod to track job lifecycle - Uses
alias_methodto preserve original behavior - Only tracks rate-limited queues
- Overrides
-
Thread Safety:
- Uses existing lock mechanism from rate limiting
- Atomic Redis operations (INCR/DECR)
Your issue: Database-intensive jobs accumulate, causing lock contention.
Solution:
# In config/initializers/resque.rb
Resque.rate_limit(:lsq_rl_pro, at: 5, per: 5, concurrent: 3)This ensures:
- Rate limit: Max 5 jobs start per 5 seconds
- Concurrent limit: Max 3 jobs run at once
- Even if jobs take 10+ seconds, only 3 run concurrently
-
Mount the local gem in Docker:
volumes: - /Users/parikshitsingh/Desktop/opensource/resque-throttler:/resque-throttler
-
Update Gemfile:
gem 'resque-throttler', path: '/resque-throttler'
-
Run tests:
docker exec -it ninjastool rake throttler:test:all -
Monitor:
docker exec -it ninjastool ruby script/throttler_live_monitor.rb
After testing:
- Push gem updates to your fork
- Update Gemfile to point to new version
- Deploy with new concurrent limits
- Monitor database metrics for improvement
The feature is production-ready and backward compatible.