This episode is for members only

Sign up to access "Laravel Performance" right now.

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


Queues in Laravel are incredibly powerful,
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.
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.
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.
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,
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
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
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.
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.
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.
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.
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.
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,
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.
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,
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.
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,
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
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.
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
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.
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.
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.
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,
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.
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.
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
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
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.
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,
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.
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.
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.
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,
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,
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,
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
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,
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.
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.
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.
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.
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
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,
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.
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
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
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
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


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!


No comments, yet. Be the first to leave a comment.