This episode is for members only

Sign up to access "Laravel Performance" right now.

Get started
Already a member? Sign in to continue
Playing
14. Queuing emails

Transcript

00:00
Queues in Laravel are incredibly powerful,
00:02
but one really quick win that you can implement is queuing emails. It's likely that your app is going to be sending emails out at some point.
00:09
And it makes sense to queue these, so the sending of this is done behind the scenes. It can be fairly slow to actually do this. So we're going to head over to the article page itself.
00:19
So we're just going to click on any of these articles. And down the bottom here, we've got this share this article by email, where we enter an email address and hit Share. And that's going to go ahead and send that to someone by email.
00:29
So if we just head over to Routes and Web here, we can see that we've got this post share controller. All this is doing is just validating this email. We're going to send the email here,
00:39
and then it's just going to return back. I've also included this null response here, which we can actually use to check the speed of this, because Laravel debug bar will only
00:48
give us the speed for the current page we're on and not this page before we're redirected back. So we'll switch stuff around in just a second. OK, let's look at actually sending the email first of all
00:59
if you are new to Laravel. So we're going to go ahead and make a mail in here. And we're just going to call this share post. And we'll just leave this.
01:06
Let's generate a markdown mailable actually, so email and share post. So that should be OK. Let's maybe put this under an email.
01:16
OK, so let's open up the share post email. And we're just going to leave pretty much everything as it is here. We've got a markdown template here.
01:26
We're not going to change anything about that. Now, to send an email with Laravel, we're going to go ahead and say mail to. We're going to choose who it's to.
01:35
That's just going to be the email that we get through here. And then we go ahead and say send. And we pass in a new instance of the email that we want to send.
01:43
In this case, it's share post. We can pass anything down to here. And that's just going to send the email off. Now, I have this configured with hello,
01:50
which is just an email, local email testing piece of software. And I've configured my email in here to go through to that particular service. But it doesn't matter what you're using to test emails.
02:02
It's going to work in exactly the same way. OK, so let's just check this out and actually see if this email comes through. So if we just hit this,
02:08
you can see that we've got some validation in here. I'm going to go ahead and send this over to myself, hit share. And we should get an email come through in just a second. And there we go.
02:17
So we've got this share post email come through. And you might have noticed there was a slight delay there. It's not a massive delay, but if you are sending out emails,
02:25
it makes sense to do this behind the scenes within a queue. So we're also going to look at the very, very basics of queues here, which you can then use to queue further things later on
02:35
if you need to sort of more intensive tasks. OK, so to get started with queues, what we're going to do is create out a database queue for now.
02:45
I'd recommend switching this out later on. It's not ideal to have your jobs being put into your database, but of course it's better than executing these
02:55
just normally at runtime. So if we just head over to our EMV file and we look for queue, you can see at the moment, the queue connection is set to sync.
03:05
Sync basically means this is going to happen at runtime. The email is going to get sent. All of your jobs are just going to be processed as they are. They're not going to be put into any kind of queue.
03:14
We're going to change this over to database, but we need to generate the tables where all of the jobs are going to be stored. So we can do this using PHP artisan.
03:22
And I think it's queue table, maybe. There we go. So we've created a migration here. If we just open up the create jobs table,
03:31
that's just going to create out the job table for us. We don't need to change anything in here. We don't even really need to know what's going on. So let's go ahead and migrate that and we are done.
03:40
So now over in our database, we have a jobs table where any jobs are going to be inserted into ready to be run in the background. And that includes sending emails.
03:50
So to actually run our queue, all we need to do is run PHP artisan queue work. Queues are incredibly powerful. There are loads of different options
03:58
you can pass through to here. Things can get pretty advanced, but at the moment this is going to hang and it's waiting to process things
04:05
that have been put into this table. Now, things that have been put into the table include emails, jobs that you are doing. You can have inline jobs as well.
04:15
It really doesn't matter. So we're going to go over to the controller that we were just working on. So let's go ahead and find that again,
04:22
post share controller. And instead of saying send, we're very simply just going to set this to queue. That's all we need to do to queue out an email.
04:30
We could of course have a completely separate job that has this code inside of it, but Laravel contains this helpful queue method, which makes it a lot easier just to queue an email.
04:40
Okay, so any change we make here, we're going to go ahead and rerun our queue worker. And now let's just go ahead and see that if this actually sends.
04:48
So let's go ahead and enter my email address, hit share, and let's go over to our email client. And there we go, it's been sent. And what's actually happened behind the scenes here,
04:57
this would have been inserted into this table. It's gone now because it's been processed, but it would have been inserted into there. And then the runner just here,
05:05
you can see has run this and completed this share post email being sent. So that is pretty much it. Now we want to check how much quicker this is,
05:15
because of course we want to see if we actually have some sort of benefit to this. So I'm going to go ahead and uncomment return back, and we're going to set this over to the normal send
05:24
just to see what happens. So let's go over and enter an email address in here again. And that's going to give us this page here. If we open up our network tab,
05:35
we're going to have to do that again, actually. So let's just go get rid of debug bar just for a moment. Let's send this across again. And what we'll see here is 200 milliseconds.
05:46
Now that's slower than the actual page that we are currently on with the article. So it's 200 milliseconds is not a huge amount of time, but it's more than it can be.
05:56
And with a very, very simple change, we can get this right down to a simple database insert. So let's go ahead and look at the speed that we get when we queue this instead.
06:05
So let's come back over. I think I can just refresh this or we'll just go back and try this again. And let's hit share.
06:13
And there we go, we're down to 62 milliseconds. So that's a 140 millisecond difference between changing over a method name and setting up a very simple database queue here
06:23
to insert this into the database and run in the background. You can see all of these being processed really nicely. We have a failure here. That's probably just because we were tweaking things around,
06:32
but that's absolutely fine. Okay, so we have created a very, very simple queue to work from the database. This then goes ahead and sends this email in the background.
06:44
So when your user submits this form, they get back to this page a lot quicker. Not a massively noticeable difference, but of course it is a difference
06:53
that we can make very, very easily. Also, SharePost is pretty straightforward, but if you were doing anything more complex inside of here, like doing something in the constructor
07:01
or building up some sort of body by making requests to your database here, for example, of course this email is gonna end up getting slower. So the fact that we're queuing the sending of this
07:11
is gonna make things a lot quicker. Let's return this back to return back, and we now have an email being sent here, but it's very, very, very quick.
15 episodes1 hr 9 mins

Overview

Let's keep our Laravel applications feeling snappy! In this course, we cover the absolute fundamentals you need to keep in mind when building anything with Laravel.

While Laravel handles a lot for you, it's easy to fall into the trap of not considering and monitoring performance as you go. Keep these tips in your toolbelt, and you'll be able to develop faster apps, from the beginning.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!