What is a MESSAGE QUEUE and Where is it used?

Gaurav Sen
6 May 201809:59

Summary

TLDRThis video script explores the concept of messaging queues using a pizza shop analogy. It explains how a queue system can handle multiple orders asynchronously, improving customer satisfaction and operational efficiency. The script delves into the complexities of scaling, such as handling server failures and ensuring data persistence. It introduces the idea of a task queue for load balancing and redundancy, highlighting the importance of messaging queues in system design, with Rabbit MQ and JMS as examples. The video concludes by inviting viewers to discuss and subscribe for more insights.

Takeaways

  • πŸ• **Asynchronous Processing:** The script illustrates how a pizza shop uses messaging queues to handle orders asynchronously, allowing clients to receive confirmation without waiting for the pizza to be made.
  • πŸ“ **Order Management:** It emphasizes the importance of maintaining an ordered list to manage incoming pizza orders efficiently.
  • πŸ”„ **Queue System:** The pizza shop uses a queue system to manage the order of pizza making, which mirrors the list of orders and allows for the addition of new orders without interrupting the process.
  • πŸ’Έ **Payment Process:** Once a pizza is made, it's removed from the queue, and the client is asked to pay, completing the transaction without the need for the client to wait.
  • πŸ›οΈ **Client Experience:** The system design aims to improve the client experience by allowing them to multitask while waiting for their order, enhancing satisfaction.
  • πŸ”„ **Task Prioritization:** The pizza shop can prioritize tasks, such as making a pizza immediately or filling a coke can, and manage the queue accordingly.
  • πŸ”— **Scalability and Redundancy:** The script discusses the scalability of the system with multiple pizza shops and the redundancy in case one shop goes down, ensuring orders are still fulfilled.
  • πŸ”’ **Data Persistence:** It highlights the need for data persistence using a database to store orders, ensuring that orders are not lost in case of a power outage or server failure.
  • πŸ“‘ **Heartbeat Mechanism:** A notifier is introduced to monitor server health through a heartbeat mechanism, detecting server failures and redistributing orders.
  • πŸ”„ **Load Balancing:** The script explains the use of load balancing to prevent duplication of orders and to distribute the workload evenly across servers.
  • πŸ“¦ **Task Queues:** It concludes with the concept of task queues, which encapsulate the complexities of order management, server assignment, and notification into a single system, making it an essential tool for system design.

Q & A

  • What is the primary purpose of a messaging queue in the context of the pizza shop example?

    -The primary purpose of a messaging queue in the pizza shop example is to manage and maintain the order of tasks (pizza orders) asynchronously, allowing the shop to continue taking orders without making the client wait for immediate service or response.

  • How does the pizza shop handle multiple clients requesting pizzas simultaneously?

    -The pizza shop handles multiple clients by using a list that maintains the order numbers and notes down each order. This list acts as a queue, allowing the shop to take new orders while working on existing ones without making clients wait.

  • What is the significance of giving clients a non-immediate response in the form of a confirmation?

    -The significance of giving clients a non-immediate response in the form of a confirmation is to relieve the client from expecting an immediate service, allowing them to engage in other activities while their order is being processed.

  • How does the asynchronous nature of the pizza shop's system benefit the clients?

    -The asynchronous nature of the system benefits clients by allowing them to perform other tasks while waiting for their order, improving their experience and making better use of their time.

  • What happens when a pizza shop in a chain goes down due to a power outage or other issues?

    -When a pizza shop in a chain goes down, the takeaway orders are typically canceled, while delivery orders can be rerouted to other operational shops to be completed, ensuring customer satisfaction and minimizing losses.

  • Why is persistence in data storage necessary for the pizza shop's system?

    -Persistence in data storage is necessary to ensure that the list of orders is not lost in case of a power outage or system failure, allowing the shop to maintain and recover the order queue.

  • What is the role of a notifier in the system architecture of the pizza shop?

    -The notifier's role is to check for a heartbeat from each server at regular intervals. If a server does not respond, the notifier assumes the server is down and takes action to redistribute the uncompleted orders to other servers.

  • How can the system prevent duplication of orders when redistributing tasks to other servers?

    -The system can prevent duplication by using load balancing techniques, such as consistent hashing, which ensures that orders are not sent to the same server more than once.

  • What is the advantage of using a task queue in the pizza shop's system design?

    -A task queue simplifies system design by encapsulating the complexity of task assignment, notification, load balancing, heartbeat monitoring, and persistence into a single entity, making it easier to manage and scale.

  • Can you name some examples of messaging queues or libraries that can be used to implement the pizza shop's system?

    -Examples of messaging queues include RabbitMQ, and libraries like zeroMQ and JMS (Java Messaging Service). These tools can be used to easily implement the messaging queue functionality in the pizza shop's system.

  • What is the importance of using a messaging or task queue in system design for scalability and reliability?

    -Messaging or task queues are important in system design as they provide a way to handle tasks asynchronously, improve scalability by distributing workloads, and enhance reliability by ensuring tasks are not lost or duplicated, even in the event of server failures.

Outlines

00:00

πŸ• Asynchronous Order Processing in Pizza Shops

This paragraph introduces the concept of messaging queues using the analogy of a pizza shop. It explains how a pizza shop handles multiple orders without making customers wait by using a list to maintain order sequence. The shop can take new orders while making pizzas, similar to how a queue works. The system is asynchronous, allowing customers to do other tasks while waiting for their pizzas. The paragraph also touches on how this system can scale to a chain of pizza shops, with the need for a database to ensure persistence in case of a power outage or server failure.

05:03

πŸ›‘ Handling Server Failures with Heartbeats and Load Balancing

The second paragraph delves into the complexities of maintaining order in a distributed system, such as a chain of pizza shops. It discusses the use of a 'notifier' that checks for server heartbeats to detect failures. If a server goes down, the notifier queries the database for unfinished orders and redistributes them to the remaining servers. The paragraph addresses the issue of potential duplication of orders and how load balancing techniques, such as consistent hashing, can prevent this while also balancing the load across servers. It concludes by highlighting the benefits of using a task queue for managing tasks, ensuring no single point of failure and maintaining efficiency in the system.

Mindmap

Keywords

πŸ’‘Messaging Queues

Messaging queues are a fundamental concept in system design used to handle and manage tasks or messages in a way that allows for asynchronous processing. In the context of the video, messaging queues are likened to a pizza shop's order system, where orders are taken and processed without requiring the customer to wait for the pizza to be made. The script uses the pizza shop analogy to explain how messaging queues can manage multiple orders efficiently and handle scenarios where a part of the system (like a pizza shop) goes down.

πŸ’‘Asynchronous Processing

Asynchronous processing refers to the ability of a system to handle multiple tasks concurrently without waiting for one task to complete before starting another. In the video's pizza shop example, the shop can take new orders while making pizzas, allowing customers to engage in other activities instead of waiting for their order. This concept is crucial for improving efficiency and user experience in system design.

πŸ’‘Order Confirmation

Order confirmation is the immediate response given to a customer to acknowledge that their order has been received and will be processed. In the script, the pizza shop uses order confirmations to manage customer expectations and to allow the order-taking process to continue without interruption, which is a simple yet effective way to handle multiple requests in a service-oriented system.

πŸ’‘Queue

A queue is a data structure or a list that maintains the order of elements, such as pizza orders in the video. It follows the First-In-First-Out (FIFO) principle, where the earliest order placed is the first to be processed. The script uses the queue to illustrate how orders are managed and how additional orders can be added without disrupting the existing order of service.

πŸ’‘Pizza Shop Chain

A pizza shop chain in the script represents a scaled-up version of the initial pizza shop example, with multiple outlets serving numerous clients. It is used to discuss the complexities that arise with growth, such as the need for persistent data storage and the ability to reroute orders in case one of the shops goes down.

πŸ’‘Persistence

Persistence in system design refers to the ability of a system to retain data over time, even in the event of a power outage or system failure. The video script mentions the need for a database to store order information persistently, ensuring that orders are not lost if a pizza shop in the chain goes down.

πŸ’‘Load Balancing

Load balancing is the distribution of tasks or workload across multiple servers or systems to ensure no single system is overwhelmed and that all systems are used efficiently. In the script, load balancing is discussed as a strategy to prevent duplicate orders and to distribute orders among the remaining servers when one server fails.

πŸ’‘Heartbeat Mechanism

A heartbeat mechanism is a way for a system to check the status of its components by regularly sending out signals or 'heartbeats' to see if they are still operational. In the video, a notifier uses a heartbeat mechanism to detect if a server is down and, if so, to reroute the orders to other servers.

πŸ’‘Task Queue

A task queue is a specific type of messaging queue that is designed to handle tasks or jobs. It ensures that tasks are persisted, assigned to the appropriate server, and completed as expected. The script uses the term 'task queue' to describe the advanced system that incorporates features like assignment, notification, load balancing, and persistence.

πŸ’‘Consistent Hashing

Consistent hashing is a technique used in distributed systems to ensure that data is evenly distributed across servers and to minimize reorganization when servers are added or removed. In the context of the video, consistent hashing is mentioned as a method to prevent duplicate orders and to balance the load effectively among servers.

πŸ’‘System Design

System design is the process of planning and building systems that are both functional and efficient. The video script uses the concept of system design to explain how messaging queues and task queues can be used to manage complexity and improve the performance of systems, such as a hypothetical pizza shop chain.

Highlights

Introduction to system design using the example of a pizza shop and its order process.

Explanation of how a pizza shop takes orders without stopping to make pizzas, using a list to maintain order numbers.

The importance of giving immediate responses to clients, even if it's just a confirmation of the order, to improve customer experience.

The concept of using a queue for pizza orders, allowing the shop to handle multiple orders efficiently.

The benefits of asynchronous processing, allowing clients to do other tasks while waiting for their pizza.

The pizza maker can prioritize tasks, such as making a pizza immediately or filling a coke can, based on order priority.

Handling the scenario of a pizza shop going down, and the need to reroute orders to other shops to maintain service.

The need for data persistence, such as a database, to store order information when a shop goes down.

Introduction of a new architecture with multiple servers and a database to handle orders and server failures.

The use of a heartbeat mechanism and a notifier to check server status and reroute orders in case of server failure.

The problem of potential order duplication when rerouting, and the need for load balancing to prevent it.

The principles of load balancing to ensure no duplicate requests are sent to the same server.

The concept of consistent hashing as a technique to eliminate duplicates and balance load.

The role of a message queue or task queue in encapsulating complexity and managing tasks efficiently.

Examples of messaging queues like RabbitMQ, zeroMQ, JMS, and Amazon's messaging queues.

The importance of messaging queues in system design for managing complexity and improving efficiency.

Invitation for further discussion on the topic in the comments section and a call to like and subscribe for more system design content.

Transcripts

play00:00

Hi everyone!

play00:01

Welcome to a new system design video which is on messaging queues.

play00:05

The example that we are taking is that of a standard pizza shop.

play00:09

In a normal pizza shop you must be seeing that there is somebody taking orders.

play00:13

When the pizzas are being made they don't stop taking orders, of course.

play00:17

They keep taking orders from the new clients.

play00:20

So multiple clients request for pizzas and they get their responses immediately like:

play00:25

"Please sit down" or "Can you come back after sometime?".

play00:28

So you relieve the client from expecting an immediate response by giving them a response

play00:35

which is not the pizza but a confirmation that the order has been placed.

play00:41

What you will need then is a list.

play00:43

A list which maintains order no.

play00:46

1 order no.

play00:47

2 and so on and so forth.

play00:50

Once you are maintaining this list, you note down the order.

play00:53

You start making pizzas.

play00:54

When the second client comes in you you can stop making the pizza, or if you have multiple

play00:58

people somebody takes the order and just adds it to the queue of making pizzas.

play01:04

If this is a queue of pizza orders.

play01:08

Pizza order 1, pizza order 2 and so on and so forth.

play01:13

That's being mirrored by the list.

play01:16

And while you're working on this pizza you can take as many add orders as you like.

play01:20

It's a simple architecture.

play01:21

And when you are done making a pizza you remove it from this queue, which also removes it

play01:27

from this queue.

play01:29

This might be the same queue itself.

play01:31

Once you remove it, you ask for the client to pay and the client sends you back some

play01:38

money.

play01:40

And now they're entirely relieved.

play01:42

But the special thing about this was that the whole thing is asynchronous.

play01:45

Meaning that they did not wait.

play01:46

You did not make the client wait for your response for the payment or for the pizza.

play01:53

What happens this is that the client was able to do other tasks during that time.

play01:57

They might be checking out their phone, they might be going out, it doesn't really matter.

play02:01

But you allowed the client to be happier to be distributing its resources elsewhere instead

play02:06

of just focusing on you.

play02:08

Similarly this allows you, as the pizza maker, to order your tasks according to your priority.

play02:15

There might be one pizza which has to be made immediately.

play02:18

There might be something which is very easy to do: like filling up a coke can.

play02:23

Maybe.

play02:24

So those orders can be put according to the priority.

play02:29

And so you are able to manipulate this queue according to the priority and allow clients

play02:33

to spend the time more judiciously just by using asynchronous processing.

play02:41

With this in mind, what could possibly happen?

play02:46

One really good scenario is that you become super successful and you have a chain.

play02:51

You have multiple outlets in your pizza shop, something like dominos.

play02:54

Let's say pizza shop no.1, pizza shop no.2 and pizza shop no.3.

play03:01

Of course, each pizza shop has multiple clients connected to them.

play03:12

Assume the worst.

play03:13

Assume that one of these shops actually goes down: there's a power outage, could be anything.

play03:18

If it does go down then we need to get rid of all our takeaway orders.

play03:21

So that's easy: we just need to dump them.

play03:24

But the delivery orders can actually be sent to the other shops so they can complete those

play03:29

delivery orders.

play03:30

And you can still save some money in which case let's say pizza shop no.3 goes down.

play03:37

If it does, then the clients which are connected to it had some orders.

play03:41

Now they should be connected to these shops and their orders should be sent to them.

play03:47

How do you do this?

play03:49

Well, the simple way of maintaining a list in memory won't work because once the shop

play03:54

is down it loses electricity.

play03:56

The computer is going to shut down.

play03:58

You need some sort of persistence in your data.

play04:00

Which means you need a database.

play04:05

And this list has to be stored in that database.

play04:09

So in our new and slightly complicated architecture, what we are going to have is a set of serveres

play04:15

from 1 to 4.

play04:17

And we also have a database which is going to be storing the list of all orders that

play04:21

you have.

play04:22

So the order ID and the contents and 'is it done or not?'.

play04:25

Now let's say that the servers get orders.

play04:29

So this is containing order no.

play04:31

3 order no.

play04:32

8 or no.

play04:34

20 and order no.

play04:36

9.

play04:37

Now let's put some more over here which is order number 11.

play04:42

So 9 and 11 are being served by s3 and s3 crashes.

play04:46

If that's the case, 9 and 11 need to be rerouted somewhere.

play04:51

But how do you do this one?

play04:53

One way is to check in the database which orders belong to s3.

play04:59

So you can note down the server ID which is actually handling the order every time it's

play05:03

making an entry but this is getting complicated so instead what you could do is have some

play05:09

sort of a notifier, so that will be somewhere over here, which is going to be checking for

play05:17

a heartbeat in each server.

play05:21

It talks to each server and asks them every 15 seconds.

play05:27

What happens with that is, if a server does not respond, then the notifier assumes that

play05:31

server is dead.

play05:32

If it is dead, it can't handle orders and then it can query the database to find all

play05:39

of those orders which are not done.

play05:42

Once they are not done it picks those orders and distributes them to these three servers

play05:50

remaining servers.

play05:52

Problem!

play05:55

What if there is duplication?

play05:57

What if order no.

play05:59

3 is not yet done and it's picked by the query in the database.

play06:05

So no.3 no.8 no.20 no.9 and no.11 are picked up, and then distributed.

play06:12

So order no.

play06:13

3, let's say this time, goes to server no.

play06:15

1.

play06:16

Server no.

play06:17

2 already has order no.

play06:18

3 and is processing it, and is going to end up making a pizza sending it to the same place

play06:21

as s1.

play06:23

And you have a big loss and lots of confusion.

play06:26

So one of the things you can do is go for some sort of load balancing.

play06:33

Now, the load balancing seems like sending the right amount of load to each server.

play06:40

But the principles of load balancing ensure that you do not have duplicates: duplicate

play06:45

requests to the same server.

play06:47

If you haven't checked out the video for load balancing I suggest you do because consistent

play06:51

hashing is one technique by which you can actually get rid of duplicates also.

play06:57

That principle itself will take care of two things:

play07:01

1) Balancing the load 2) Not sending duplicates to the same server

play07:08

Reason for that is because s1 is going to be handling a set of buckets.

play07:12

s2 is going to be handling a set of buckets.

play07:14

Once this server crashes s2 won't lose its buckets it'll only get new buckets added to

play07:19

it.

play07:20

s1 will also get new buckets added to it and therefore the third order will now come because

play07:25

they belong to s2 even now.

play07:28

Maybe the other orders of no.9 might come in and no.11 might come in here.

play07:33

So that's how it works!

play07:34

Okay...through load balancing and through some sort of a heartbeat mechanism, you can

play07:42

notify all the failed orders to the newer servers.

play07:49

Now what if you want all the features of assignment or notification, load balancing, a heartbeat

play07:56

and persistence in one thing?

play08:00

That would be a message queue.

play08:04

And for us it's not so much a message as a task queue.

play08:09

So what this does is it takes tasks, persists them, assigns them to the correct server and

play08:18

waits for them to complete.

play08:19

If it's taking too long for the server to give an acknowledgment, it feels that server

play08:23

is dead and then assigns it to the next server.

play08:27

So there are multiple strategies of course for assigning it.

play08:30

That is just like load balancing has multiple strategies, but that's all encapsulated by

play08:36

a task queue.

play08:38

And this is an important concept in system design: using messaging queues or task queues

play08:42

to get work done easily so that you can encapsulate all that complexity into just one thing.

play08:48

The pizza example is a little extreme because it does everything together.

play08:53

That's exactly what a pizza shop would want a: task queue, right?

play08:58

And an example of messaging queues is Rabbit MQ.

play09:03

There are some libraries like zeroMQ which allow you to write a messaging queue quite

play09:09

easily.

play09:10

There's JMS, which is Java Messaging Service.

play09:14

And then so on and so forth.

play09:15

I think Amazon also has a few messaging queues, but yeah, you can try out messaging queues.

play09:21

They are really good encapsulations for complexities in the server side.

play09:27

This is just a fundamental concept of system design: not really a system which has been

play09:32

designed.

play09:33

But yes, if you are going to make a pizza shop, it seems like a good idea.

play09:38

If you want to have some more discussion on this, let's have a discussion in the comments

play09:41

below.

play09:42

If you liked the video press 'like' button.

play09:44

If you want to subscribe for further system design notifications, please do so.

play09:48

I'll see you next time!

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
System DesignAsynchronousMessaging QueuesPizza ShopTask QueueLoad BalancingPersistenceHeartbeatQueue ManagementServer ReliabilityTech Tutorial