laravelredisjobs

How to retry dispatching job when redis cache is not working?


My app is dispatching a job, it was working, but there's one job that doesn't get dispatch because there's an error on redis, but this is just one job out of thousands.

RedisClusterException: Error processing response from Redis node!

I'm dispatching the job via this way:

HereTheJob::dispatch($this);

Here's my setup Queue: SQS Cache: redis

I think its using cache here because my job has ShouldBeUnique, its saving the unique job on cache.

So my question is, when there's an error on redis, how do I retry to dispatch the job?


Solution

  • ✅ How Laravel uses Redis for ShouldBeUnique?

    Yes, by default, it uses whatever cache store is configured. If it’s Redis, it will hit Redis to check/create that lock before adding the job to the queue.

    Laravel Doc Reference

    ✅ What’s happening in your case:
    ✅ How to handle Redis errors during dispatch?
    Solution 1:

    You can wrap your job dispatch logic within retry() helper. It will try to dispatch job upto attempt limits. When job will not throw error it won't try further.

    retry(3, function () {
        HereTheJob::dispatch($this);
    }, 1000);
    

    Laravel Doc Reference

    Solution 2:

    If your Redis cluster has occasional instability and it's only for job uniqueness, consider using database or a single-node Redis instance for unique_job_cache using uniqueVia() method within your job class:

    public function uniqueVia(): Repository
        {
            return Cache::driver('database');
        }
    

    Laravel Doc Reference