The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Architecture and operations
What is a message queue, and how is it different from calling a server directly?
A message queue is a line, placed between the server that asks for work and the server that does the work, in which the work is written down. The code box has three lines. In the first line, the order server writes the work for the notification server as a short text (a message) called "Notify order N" and puts it in the queue, and when it gets the result it moves straight on to the next job (the side that sends messages). In the second line, the queue keeps the messages in the order they were put in. In the third line, the notification server takes messages out of the queue one at a time and handles them (the side that receives messages). In this lab you remove the queue line so that the order server sends requests straight to the notification server, or you keep the notification server stopped while the 3 orders are sent and start it later, and you see the result the order server gets and how the notifications are handled. The code box is not the source of a program that was run. It is text that says what does what, and the queue used in the runs is a very small queue server written for this lab (it is not a real product such as Kafka or RabbitMQ).
Checked against real execution · Ran four combinations with two Node.js 24.18.0 http servers (a small queue server written for this lab and a notification server that receives direct requests) and a program that plays the order server, and checked against the model. Real products such as Kafka and RabbitMQ were not run
# Order server: for each of the 3 orders, puts a "Notify order N" message in the queue # Queue: keeps the messages in the order they were put in # Notification server: takes them out of the queue one at a time and handles them
I ran all four combinations for real, only inside 127.0.0.1 of the PC used to write this lab, and checked them against the model. I did not connect to any outside network, and nothing new was downloaded. The queue used in the runs is a very small queue server written for this lab as an http server of Node.js 24.18.0. It puts messages in one array in memory and accepts only two kinds of requests, one that puts a message in and one that takes the message at the head out, and it is not a real message queue product such as Kafka or RabbitMQ (no such product is on this PC and I did not install one). So what this lab shows is the simplest shape of "what a queue does," and it is not a check of how a real product behaves. The program that plays the order server sends the 3 orders one after another, sending the next order after it gets the result of the previous one. In the combinations that use the queue, it sends a put request to the queue server and takes the reply as the result, and in the combinations that call directly, it sends a request to the notification server and takes the reply after handling is finished as the result. The notification server is either an http server that receives direct requests (the direct combinations) or a loop that takes one message at a time from the queue server every 0.1s and handles it (the queue combinations), and either way handling one notification was made to take 0.3s and it handles one at a time. The 0.1s and the 0.3s are values chosen for this lab. In the combinations where the notification server is stopped and starts later, I started the notification server after the results of all 3 orders had been received, and while it was stopped there was no program waiting at that port number. The program that ran made the same text as the code on the screen and read it (how many, whether there is a queue line, how the notification server receives, whether it is stopped and starts later) and started things according to that content. What I compared is the result the order server got (success or failure, and whether it got the result after the notification handling finished or before it finished, judged from the record of the order in which events happened), the handled notifications and their order, and the number of messages in the queue when the notification server starts. I also confirmed that the order of taking out from the queue was the same as the order of putting in, that a request sent directly to the stopped notification server failed every time with a connection refused error (ECONNREFUSED), and that even when I watched for 1s after the notification server started, no notification was handled. In the combination that puts messages in the queue, the queue held 3 messages when the order server got all the results, and this is because of the structure of this run, in which the notification server looks into the queue every 0.1s even when it is running. That the queue was empty when the notification server started is because in this run the notification server was started before the orders. What I checked against documentation is the Introduction of RabbitMQ tutorial 1 (Hello World) (a program that sends messages is a producer, a program that mostly waits to receive messages is a consumer, a queue is like a post box and is in effect a large message buffer, and RabbitMQ is a message broker that accepts and forwards messages) and the Introduction page of the Apache Kafka documentation (https://kafka.apache.org/intro, Main Concepts and Terminology: a producer is a client application that writes events, a consumer is the side that reads and processes events, and the two are fully decoupled and agnostic of each other). I checked the documents through the quotations that WebFetch returned (I did not read the whole pages myself), and there are no section numbers. Much was shortened on the screen. Putting a message in the queue and the queue keeping it were shortened to one request and one reply, and that the reply tells only "it was kept" and not whether the notification was handled is behavior that comes from how this small queue server was made. A real product deals with acknowledgements, with which the side that handled a message tells that it has handled it, keeping the queue on disk, the case where the queue itself stops, the case where a message is lost or delivered twice, the order and distribution when there are several consumers, and redoing a failed handling, and this lab did not look at any of them. Such details are covered in the in-depth labs "Message queue basics", "Kafka partitions and consumers", and "Saga and outbox". This lab also did not judge the standard that separates the jobs a queue suits from the jobs that suit a direct call (whether the result is needed right away). I did not use a browser, and I had only one notification server and one order server.
Previous lab: Caches · Next lab: Deployment
Struck-through text was removed from the first version of the code; highlighted text was added.