Search code examples
laravelredislaravel-horizon

Laravel Horizon Jobs - What exactly is stored in Redis?


If I have a queuable Job class that I've set up to be dispatched and handled through horizon with redis, I have some questions about exactly how redis stores the information for that job.

So, the base case is a job that takes a big string payload - let's just say it's a payload with 1GB of text data

Class Job implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private string $requestInfo;

    public function __construct(requestInfo)
    {
        $this->requestInfo = $requestInfo;
    }

Now in this case, if I dispatch the Job with the Payload, I would of course expect the full payload to be stored on Redis -- the full 1GB would be all stored on that redis record.

Now, Imagine instead that I wrote the Payload to a database, and instead just sent the ID of that database record:

Class Job implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private string $requestInfo;

    public function __construct(int $requestInfoId)
    {
        $this->requestInfo = RequestInfo::find($requestInfoId)->info;
    }

So I'm still loading the 1GB payload in the constructor function, but I'm only sending the constructor the Integer ID of the database record. In this case, what does Redis store about this job? Does it record only what I need to initiate the job class (the requestInfoId) or does it also record the information I set on the class instance in the Constructor? Is it still storing that 1GB payload in Redis?


Solution

  • When you dispatch a job, your Job instance will be serialized and placed into the store (e.g. Redis in your case). Arguments passed into the job should be serializable (e.g. you cannot pass SimpleXML object there as its not serializable).

    Then handler pulls the job from the store, unserialize it and run handle() method.

    So, if your job needs a big amount of data to handle inside, it's much better to pass its Id (in your case) and then pull the data in handler method, not in constructor.

    Class Job implements ShouldQueue
    {
    
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    private int $requestInfoId;
    
    public function __construct(int $requestInfoId)
    {
        $this->requestInfoId = $requestInfoId;
    }
    
    public function handle()
    {
       $requestInfoData = RequestInfo::find($this->requestInfoId)->info;
    }
    

    In that case only Id will be stored in Redis.