mosquito-cr / mosquito

A background task runner for crystal applications supporting periodic (CRON) and manually queued jobs
MIT License
227 stars 24 forks source link

dramatically decreases the time spent listing queues #120

Closed robacarp closed 1 year ago

robacarp commented 1 year ago

One of the most frequent commands issued to the mosquito redis backend is strongly frowned upon by the redis community.

keys is listed as both slow and dangerous because it iterates the entire keyspace in O(n) time.

This replaces keys when used to enumerate the list of queues that a mosquito runner should be checking. In a redis server with 10,000 stored keys, listing search queues takes 3-5ms.

After replacing keys with zadd and zrangeby, listing search queues takes 0.3ms. I've chosen to use a sorted set to open up the possibility for automatically pruning the list.

This change is innocuous except in the rare case that a long list of jobs have been enqueued which need to be consumed but are no longer being enqueued. In the rare possibility that use case actually exist there are two workarounds:

I considered automatically falling back to the old behavior on the contingency that no queues are found but I consider the likelihood that someone is using mosquito in a manner that would require this mitigation is tiny. The amount of effort required to mitigate the problem is small, and the amount of effort required to maintain the old behavior on some sort of fallback is significant.