Laravel: Optimize Queue Jobs To Be "Dumb" With Fewer Parameters

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ส.ค. 2022
  • I will show you a before/after practical example of queue jobs, if we pass the full object, or if we pass the exact variable values only.
    Related links:
    - Scaling Laravel to 30,000 requests/min and over 100M jobs subscribe.mateusguimaraes.com...
    - My course "Queues in Laravel": laraveldaily.teachable.com/p/...
    - - - - -
    Support the channel by checking out my products:
    - My Laravel courses membership: laraveldaily.teachable.com/p/...
    - Laravel QuickAdminPanel: bit.ly/quickadminpanel
    - Livewire Kit Components: livewirekit.com
    - - - - -
    Other places to follow:
    - My weekly Laravel newsletter: bit.ly/laravel-newsletter
    - My personal Twitter: / povilaskorop
  • แนวปฏิบัติและการใช้ชีวิต

ความคิดเห็น • 42

  • @michaelcrash
    @michaelcrash 2 ปีที่แล้ว +9

    cool. I would use contructor() to deconstruct object and store what I want to be use in handle() later on.

  •  2 ปีที่แล้ว +3

    Great video. Thanks for the mention, Povilas!

  • @nadjinmalade8738
    @nadjinmalade8738 2 ปีที่แล้ว +2

    For Real, we learn a lot from your videos.
    Thanks.

  • @jrplops89
    @jrplops89 2 ปีที่แล้ว +5

    Your pronunciation of Portuguese names is very good! :)

    •  2 ปีที่แล้ว

      Haha I was going to mention this. Really good!

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว +4

      Benefit of watching football in childhood, including many famous Portuguese names so now I know how to pronounce them :)

  • @RodrigoNishino
    @RodrigoNishino ปีที่แล้ว

    Very nice! Thank you very much

  • @JoseHenrique-xg1lp
    @JoseHenrique-xg1lp 3 หลายเดือนก่อน

    Very useful, thank you

  • @daleryanaldover6545
    @daleryanaldover6545 2 ปีที่แล้ว +2

    I learned this the hard way, real hard.

  • @twentxx
    @twentxx 2 ปีที่แล้ว

    Thank you very much 👍

  • @GergelyCsermely
    @GergelyCsermely 2 ปีที่แล้ว

    Tahnks

  • @mmramadan496
    @mmramadan496 2 ปีที่แล้ว +2

    It's a huge time optimization, but it's important to pass a model instance for some cases, for illuminate, what if you need to queue sending mails when the user write his email and let's assume that the user wrote an invalid mail and he realized that after clicking submit button, in this case when the queue get started working he will not recieve any email because we pass his mail not model instance, but what if after realizing that he wrote invalid mail, he write a valid one, and we pass model instance, Laravel will retrieve the last mail because Laravel uses the `SerializesModels` trait.

    • @Shez-dc3fn
      @Shez-dc3fn 2 ปีที่แล้ว +2

      i guess this video only works if you only need 'selective' things - so use it when you need, not EVERY TIME..

  • @linasgutauskas5528
    @linasgutauskas5528 2 ปีที่แล้ว +1

    What if i need to store results from job to database? Or if i want to save job status in database (if it was successful)? Is there any good practices on these cases? Thank you.

    • @daleryanaldover6545
      @daleryanaldover6545 2 ปีที่แล้ว +1

      There is something new called Job Events, I don't remember that kind of functionality before but now you can pass results from job to database using before or after events. Check out the docs for more info.

  • @ryonnl
    @ryonnl 2 ปีที่แล้ว +1

    What would you do if you need the actual model in the job itself? Query it?

    •  2 ปีที่แล้ว +1

      When you pass the model, Laravel automatically queries it behind the scenes when it picks up the job. So that’s another thing to be aware of: if you pass the actual model, the state you get by the time your queue worker picks it up might be different from what you expect.

  • @budiprihhastomo5936
    @budiprihhastomo5936 2 ปีที่แล้ว

    But does this case apply to very large payloads?
    For example, I created a job to store logs in the database, of course the logs will contain the information we need, and if it's a lot, won't passing it into parameters will only make the payload stored in the database fatter than we use serialize model?

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว +1

      Your example is pretty extreme, I would say. Yes in some cases it's worth storing the model and then query the data when needed.

    • @budiprihhastomo5936
      @budiprihhastomo5936 2 ปีที่แล้ว

      @@LaravelDaily Thank you for the answer sir, maybe now I understand better if I have to use one of them. 👍

  • @cykablyatii2086
    @cykablyatii2086 2 ปีที่แล้ว

    is it better if we dispatch a job inside a loop?
    or do the loop inside the job?

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว

      I prefer many small individual jobs.

  • @Shez-dc3fn
    @Shez-dc3fn 2 ปีที่แล้ว

    i dont understand if I send the USER to a job, Its serialised and saved in jobs table next to that job - when the job is run, why would LARAVEL make a new call to user table? surely it deserializes the saved user and works with that?

    •  2 ปีที่แล้ว +2

      Laravel doesn’t serialize the state, but the model’s class and ID so it can query it when the job is picked up. That allows you to avoid working with incorrect state and also make the payload smaller

  • @SLIMBOY489
    @SLIMBOY489 2 ปีที่แล้ว

    Off topic question but, in your experience, how many requests can handle a Laravel API per second?

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว

      Haven't tested it. Googled "laravel benchmarks", found this 2017 article: medium.com/@taylorotwell/benchmarking-laravel-symfony-zend-2c01c2b270f8#:~:text=Laravel%3A%20521.64%20requests%20per%20second%20(mean)
      But generally, it depends on what your API does.

  • @ivan_adamovich
    @ivan_adamovich 2 ปีที่แล้ว +1

    Sir, can i ask you to make a video about websocket? how it works, how to use and ect.

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว +1

      Not in plans, for now, here's why: twitter.com/PovilasKorop/status/1470640480593801216

  • @danjanuspineda4330
    @danjanuspineda4330 2 ปีที่แล้ว

    how about chunking so it does not run out of memory, most of the times this speed in sending email are not that much necessary, wdyt sir, thanks for your content

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว

      Sending email is just an example, jobs may be for other things, it's a general advice. But yes, batching and grouping jobs is also possible, but not sure how it would save memory exactly.

    • @Saifallak
      @Saifallak 2 ปีที่แล้ว

      ​@@LaravelDaily i guess he means while getting the data `User::all()`, chunking here saves you, because loading much data in memory is a bad idea

    • @daleryanaldover6545
      @daleryanaldover6545 2 ปีที่แล้ว

      @@Saifallak he should never use User::all() with jobs. It's always better to trigger jobs on a single instance of model. But for a similar case, I used to generate reports using jobs, and passing a huge number of models would simply make php ran out of memory. In this case trim out unnecessary attributes from the models. This can be done by querying only needed columns explicitly so the result of User::all() would only include teh needed attributes.

  •  2 ปีที่แล้ว

    so helpful

  • @unitaxi1644
    @unitaxi1644 2 ปีที่แล้ว

    When will be published new course in teachable platforme ?

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว +1

      Today or tomorrow, if everything is according to the plan

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว

      It's out: laraveldaily.teachable.com/p/laravel-testing-for-beginners-phpunit-and-pest

  • @iamriwash7943
    @iamriwash7943 2 ปีที่แล้ว

    can u make a video about how to send email with email template support of CSS ????

    • @LaravelDaily
      @LaravelDaily  2 ปีที่แล้ว

      That wouldn't be video about Laravel, then. It would be about some CSS template in HTML email, I'm not an expert in that, look for that info on some front-end related channel.

    • @iamriwash7943
      @iamriwash7943 2 ปีที่แล้ว

      @@LaravelDaily no in laravel when we send email in laravel CSS does not show in email

    • @abduzzy
      @abduzzy 2 ปีที่แล้ว +1

      you can use css with inline or internal style in blade template. If you want to use tailwind as css style, you should check maizzle.

    • @daleryanaldover6545
      @daleryanaldover6545 2 ปีที่แล้ว

      @@abduzzy laravel actually comes with a mail feature, complete with css customisations. He can simply publish the assets and modify it from there. It will be converted to in-line css by the mailable class.

    • @daleryanaldover6545
      @daleryanaldover6545 2 ปีที่แล้ว

      @@iamriwash7943 laravel had a documentation about mailable class, it's pretty easy to understand.