-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Queue performance question/documentation update? #27148
Comments
Sorry if this might be a slightly off-topic comment. I never directly passed models, always only primary keys and used First time I saw the serialization result of this for a job, I immediately had a bad feeling about this. It simply didn't feel right. This however requires that your models state is persisted already in the DB. |
Are you using the SerializesModels trait? |
Can you please post some code which recreates the problem? |
@driesvints I have an example repo here: https://github.com/owenconti/laravel-queues-performance-repo After more research, I think this functionality is intended, it just isn't documented as such. Here's what I think is happening:
Thus, in my original example, I had a ton of relationships already loaded, so when processing the job, it took a few seconds to load all the data out of the database, even though I wasn't accessing any of it. I'm not sure whether that is intended functionality (I could argue on either side for/against it), but if it is intended, I think this section of the documentation should mention it:
|
The "If your queued job..." docs mentioned in the root post would have been written before this change was made in Laravel 5.6: #21229
You can still use the The framework change was intended to allow broadcast event payloads (to Pusher or the Laravel WebSockets package) to contain nested relationship data since beforehand However it can turn queued jobs, listeners, and mailables into greedy database query resource hogs. The queue worker can run a dozen queries that reload hundreds (or thousands) of models unrelated to the job being run, all because pre-dispatch those relations were hydrated in the serialized model. Unless you're using event broadcasting, I suggest overriding |
Thanks, that's quite interesting and I didn't know about that! But thinking again, this is a potential pitfall: if you don't foresee this and change the model in advance, you can be exposed to this problem. I guess I'm erring on the safe side, keeping Job payloads small and just resolve data within the jobs. I prefer it being more explicit. Oh, and feels wrong to me that the model would have to have knowledge about this… |
I sent in a PR to easily unset relations for a queue job here: #30802 This way they won't be serialized. I'll also make sure to document this in the queue docs once it's merged. |
Documented here: laravel/docs#5655 |
Description:
I noticed my queue was taking a long time to process a specific type of job. While debugging the issue, I eventually got to the point where the
handle()
method of my job wouldreturn
right away, without doing anything. Even when returning right away, the job was taking ~10 seconds to run.Upon digging into the internals, it seems like the entire model may be stored in Redis, instead of just the identifier of the model, as documented here:
I have some screenshots to help demonstrate the issue.
First, the Redis value stored for a job. Note you can see data is being stored about the relations on the model (this may be normal/intended, I'm not sure):
Second, a screenshot showing the processing time when the job is passed an Eloquent model. The first and fourth lines are for the same job. Note the 7 second time to run:
Third, a screenshot showing the processing time when the job is passed a primitive:
Steps To Reproduce:
Dispatch a job with a lot of nested relations. Compare the processing time against the same job, but passed the ID of the model instead.
Note
This does not cause a performance issue with all models. In our case, it tends to only come up with models that have a lot of nested relations.
If everything seems normal or is working as expected, can someone recommended what we can do to improve performance of the queue, other than passing primitives instead of models?
The text was updated successfully, but these errors were encountered: