[MUSIC PLAYING]
Hi.
My name is Femi Taiwo and I am the CTO for INITS Limited.
Today, I'll be talking about how we build apps that scale, more
like the things we do to ensure that our applications are
able to handle a large amount of traffic.
So imagine you're designing a system that
will need to send out notifications
to users, 100,000 users, for example, via SMS, email,
and push notifications for mobile phones.
If there are just 10 users or 20 users,
it would be relatively easy and it
would be executed within a few milliseconds
or a second or two.
When you're talking about large amounts of people, 100,000,
for example, these processes can take a very long time.
A queue mechanism would simply take
the data you've provided to it and process it
in the background, and then provide feedback afterwards
to the admin about the status of the messages
that the admin has created.
This is an app which requires users to verify their accounts
by entering the code sent to their phone number
after signing up.
I have written this code hopefully
in a way that makes it extremely easy to follow
with as few lines as possible.
It's not meant for production at all.
And I'm going to just fill out the form now and put
the mobile number and submit.
Waiting, waiting, waiting.
It's back.
Great.
Now, let's take a look at the code behind.
Here is the form with all the elements inside.
This is the part where it saves the information that
has been submitted by the user.
Let's take a look at that function.
Now, I'm using Memcache just for ease of demonstration.
This is not what I should use, really,
but I'm using it just to ease up the process of demonstrating
saving to a database and retrieving information from it.
What you see here is I'm generating an ID,
generating a code as well, and saving to my database.
Lastly, I have a method called Send Welcome Message, which
essentially lets me [INAUDIBLE], which essentially creates
parameters for sending an SMS to the client or recipient, which
is my phone number in this case.
Now, to simulate a network or service delay,
I have this sleep for three seconds
here, which is why you notice that when I submitted
the form earlier, it took a few seconds before it responded
to me.
Now, a problem with this design is
that it means the user who just submitted the form would have
to wait for the entire interaction between my server
and the SMS provider to be completed before the next page
loads, and any number of things can go wrong at this point.
What if, for example, your third party provider at the SMS
gateway is not reachable?
What if the service is temporarily unavailable?
What happens?
A better design is to simply send it to a queue.
Here's what I should be doing instead.
When the user fills out this form,
now I'm not going to submit this form now.
I'm just going to fill it out for now.
What should have happened is that in place
of trying to connect to the API at this point in time,
I should just add it to a queue.
I already have a method called Queue of Welcome Message, which
helps me add it to a queue.
Let's visit that function.
Now, I'm using Google App Engine for this demonstration.
And Google App Engine has an awesome in-built queuing system
that requires zero set up.
What I just need to do is to create an instance of this push
queue class, tell it what URL it needs to call
and parameters I should pass to it.
Then I can add that particular task to the queue.
Now that I've done this, I'm just
going to go ahead and submit this form.
You notice that it will return very fast.
Now, what has happened is that it
has transferred to that task and the tasks in the background.
And there you go.
SMS now is on my phone so I didn't
have to keep the user waiting at all.
Here are three more reasons why queues are very useful.
In summary, if you want to scale, you should use a queue.
[MUSIC PLAYING]