What is a Message Queue and Where is it used? - YouTube

Channel: Gaurav Sen

[0]
Hi everyone!
[1]
Welcome to a new system design video which is on messaging queues.
[5]
The example that we are taking is that of a standard pizza shop.
[9]
In a normal pizza shop you must be seeing that there is somebody taking orders.
[13]
When the pizzas are being made they don't stop taking orders, of course.
[17]
They keep taking orders from the new clients.
[20]
So multiple clients request for pizzas and they get their responses immediately like:
[25]
"Please sit down" or "Can you come back after sometime?".
[28]
So you relieve the client from expecting an immediate response by giving them a response
[35]
which is not the pizza but a confirmation that the order has been placed.
[41]
What you will need then is a list.
[43]
A list which maintains order no.
[46]
1 order no.
[47]
2 and so on and so forth.
[50]
Once you are maintaining this list, you note down the order.
[53]
You start making pizzas.
[54]
When the second client comes in you you can stop making the pizza, or if you have multiple
[58]
people somebody takes the order and just adds it to the queue of making pizzas.
[64]
If this is a queue of pizza orders.
[68]
Pizza order 1, pizza order 2 and so on and so forth.
[73]
That's being mirrored by the list.
[76]
And while you're working on this pizza you can take as many add orders as you like.
[80]
It's a simple architecture.
[81]
And when you are done making a pizza you remove it from this queue, which also removes it
[87]
from this queue.
[89]
This might be the same queue itself.
[91]
Once you remove it, you ask for the client to pay and the client sends you back some
[98]
money.
[100]
And now they're entirely relieved.
[102]
But the special thing about this was that the whole thing is asynchronous.
[105]
Meaning that they did not wait.
[106]
You did not make the client wait for your response for the payment or for the pizza.
[113]
What happens this is that the client was able to do other tasks during that time.
[117]
They might be checking out their phone, they might be going out, it doesn't really matter.
[121]
But you allowed the client to be happier to be distributing its resources elsewhere instead
[126]
of just focusing on you.
[128]
Similarly this allows you, as the pizza maker, to order your tasks according to your priority.
[135]
There might be one pizza which has to be made immediately.
[138]
There might be something which is very easy to do: like filling up a coke can.
[143]
Maybe.
[144]
So those orders can be put according to the priority.
[149]
And so you are able to manipulate this queue according to the priority and allow clients
[153]
to spend the time more judiciously just by using asynchronous processing.
[161]
With this in mind, what could possibly happen?
[166]
One really good scenario is that you become super successful and you have a chain.
[171]
You have multiple outlets in your pizza shop, something like dominos.
[174]
Let's say pizza shop no.1, pizza shop no.2 and pizza shop no.3.
[181]
Of course, each pizza shop has multiple clients connected to them.
[192]
Assume the worst.
[193]
Assume that one of these shops actually goes down: there's a power outage, could be anything.
[198]
If it does go down then we need to get rid of all our takeaway orders.
[201]
So that's easy: we just need to dump them.
[204]
But the delivery orders can actually be sent to the other shops so they can complete those
[209]
delivery orders.
[210]
And you can still save some money in which case let's say pizza shop no.3 goes down.
[217]
If it does, then the clients which are connected to it had some orders.
[221]
Now they should be connected to these shops and their orders should be sent to them.
[227]
How do you do this?
[229]
Well, the simple way of maintaining a list in memory won't work because once the shop
[234]
is down it loses electricity.
[236]
The computer is going to shut down.
[238]
You need some sort of persistence in your data.
[240]
Which means you need a database.
[245]
And this list has to be stored in that database.
[249]
So in our new and slightly complicated architecture, what we are going to have is a set of serveres
[255]
from 1 to 4.
[257]
And we also have a database which is going to be storing the list of all orders that
[261]
you have.
[262]
So the order ID and the contents and 'is it done or not?'.
[265]
Now let's say that the servers get orders.
[269]
So this is containing order no.
[271]
3 order no.
[272]
8 or no.
[274]
20 and order no.
[276]
9.
[277]
Now let's put some more over here which is order number 11.
[282]
So 9 and 11 are being served by s3 and s3 crashes.
[286]
If that's the case, 9 and 11 need to be rerouted somewhere.
[291]
But how do you do this one?
[293]
One way is to check in the database which orders belong to s3.
[299]
So you can note down the server ID which is actually handling the order every time it's
[303]
making an entry but this is getting complicated so instead what you could do is have some
[309]
sort of a notifier, so that will be somewhere over here, which is going to be checking for
[317]
a heartbeat in each server.
[321]
It talks to each server and asks them every 15 seconds.
[327]
What happens with that is, if a server does not respond, then the notifier assumes that
[331]
server is dead.
[332]
If it is dead, it can't handle orders and then it can query the database to find all
[339]
of those orders which are not done.
[342]
Once they are not done it picks those orders and distributes them to these three servers
[350]
remaining servers.
[352]
Problem!
[355]
What if there is duplication?
[357]
What if order no.
[359]
3 is not yet done and it's picked by the query in the database.
[365]
So no.3 no.8 no.20 no.9 and no.11 are picked up, and then distributed.
[372]
So order no.
[373]
3, let's say this time, goes to server no.
[375]
1.
[376]
Server no.
[377]
2 already has order no.
[378]
3 and is processing it, and is going to end up making a pizza sending it to the same place
[381]
as s1.
[383]
And you have a big loss and lots of confusion.
[386]
So one of the things you can do is go for some sort of load balancing.
[393]
Now, the load balancing seems like sending the right amount of load to each server.
[400]
But the principles of load balancing ensure that you do not have duplicates: duplicate
[405]
requests to the same server.
[407]
If you haven't checked out the video for load balancing I suggest you do because consistent
[411]
hashing is one technique by which you can actually get rid of duplicates also.
[417]
That principle itself will take care of two things:
[421]
1) Balancing the load 2) Not sending duplicates to the same server
[428]
Reason for that is because s1 is going to be handling a set of buckets.
[432]
s2 is going to be handling a set of buckets.
[434]
Once this server crashes s2 won't lose its buckets it'll only get new buckets added to
[439]
it.
[440]
s1 will also get new buckets added to it and therefore the third order will now come because
[445]
they belong to s2 even now.
[448]
Maybe the other orders of no.9 might come in and no.11 might come in here.
[453]
So that's how it works!
[454]
Okay...through load balancing and through some sort of a heartbeat mechanism, you can
[462]
notify all the failed orders to the newer servers.
[469]
Now what if you want all the features of assignment or notification, load balancing, a heartbeat
[476]
and persistence in one thing?
[480]
That would be a message queue.
[484]
And for us it's not so much a message as a task queue.
[489]
So what this does is it takes tasks, persists them, assigns them to the correct server and
[498]
waits for them to complete.
[499]
If it's taking too long for the server to give an acknowledgment, it feels that server
[503]
is dead and then assigns it to the next server.
[507]
So there are multiple strategies of course for assigning it.
[510]
That is just like load balancing has multiple strategies, but that's all encapsulated by
[516]
a task queue.
[518]
And this is an important concept in system design: using messaging queues or task queues
[522]
to get work done easily so that you can encapsulate all that complexity into just one thing.
[528]
The pizza example is a little extreme because it does everything together.
[533]
That's exactly what a pizza shop would want a: task queue, right?
[538]
And an example of messaging queues is Rabbit MQ.
[543]
There are some libraries like zeroMQ which allow you to write a messaging queue quite
[549]
easily.
[550]
There's JMS, which is Java Messaging Service.
[554]
And then so on and so forth.
[555]
I think Amazon also has a few messaging queues, but yeah, you can try out messaging queues.
[561]
They are really good encapsulations for complexities in the server side.
[567]
This is just a fundamental concept of system design: not really a system which has been
[572]
designed.
[573]
But yes, if you are going to make a pizza shop, it seems like a good idea.
[578]
If you want to have some more discussion on this, let's have a discussion in the comments
[581]
below.
[582]
If you liked the video press 'like' button.
[584]
If you want to subscribe for further system design notifications, please do so.
[588]
I'll see you next time!