How to rate limit jobs in Laravel vapor. Job bucket?
Does anyone have any ideas on this...
I need a way to rate limit jobs run on vapor without using up invocations.
The rate limiting should be done on a key basis as with the usual redis style rate limiters.
The reason I do not think I can use a traditional approach is that they effectively work by starting the job and then checking if it can run. If it cannot then the job is released back into the queue. I want to be in a position where I can add 1000 jobs to a queue under a certain key and then allow (for example) 100 of them to be released and completed every minute.
I believe I could do this with a kind of job hopper. Fill a table up with serialized jobs, their key and rate etc. and then use a seeder job with a leaky bucket algorithm to dispatch jobs from my hopper.
What do you think? Any better ideas or a way this could be done within vapor natively?
If you need something like that then use Redis/Horizon. Recently Laravel team made it possible to use Redis/Horizon with Vapor. Maybe you should check that out.
Not sure why you would want to do something like that but I guess you have your own reasons.
Thanks, but Horizon is just for monitering right? Unless they changed something recently.
My issue here is more to do with ensuring that we do not throw thousands of jobs into the queue that are going to get rejected and released many times. It just feels like a waste to do this to me...
What we have come up with for now, is a "job hopper" model/table, with a serialised instance of the job to be dispatched, which we dispatch to the queue with a throttle or concurrency check before actually dispatching. So this means we should in theory never have any jobs in the queue that our server or the API's do not have capacity for.
I know that Redis itself has the funnel and throttle methods but these require the job to be put into the queue and then released potentially hundreds or thousands of time until there is space... which does not feel very nice. I would love it if the throttle and funnel methods did not increase the attempts counter on the job.
@bugsysha Can you just confirm your point on horizon? I tried to install it today and it does not seem compatible with Vapor even as a monitoring tool. Once installed we just get redis connection exceptions and a blank dashboard (our vapor does have a redis cache attached)
I could not find the tweet which mentioned that it is or it will be possible to use Horizon on Vapor. Sorry. Maybe my memory is playing tricks on me cause it does not make sense that I can not find it.