The labs on this site need JavaScript to run. The same content is written out below.
Learn › Architecture and operations › Messaging and asynchronous processing
Leave a note instead of calling directly
Why it exists — If one server calls another server directly and waits until it finishes, the response is delayed as much as the other side is slow, and if the other side is stopped, the request fails too. A message queue puts a place between the two servers where messages are stored, so the sending side responds without waiting for the result, and the receiving side, after stopping and coming back, processes the backed-up messages starting with the oldest.
Where it's used today — It is used to hand off follow-up work that the response does not immediately need, such as sending a confirmation email after an order or creating a thumbnail of an uploaded image, and tools such as RabbitMQ, Kafka, and Amazon SQS do this job. It is also used when several services must each receive and process one event. For work where you must receive the result before going to the next step, such as payment approval, a direct call is kept.
When an order comes in, you must send a confirmation email. The order server can call the email server directly and wait until it finishes, or it can put a message that says "send the email" in a queue and respond right away.
Checked against real execution · All 36 combinations were run and checked: the order server and the email server on Spring Boot 4.1.1 (the methods on the screen compiled as they are), and for the queue approach RabbitMQ 4.4.1 (Docker rabbitmq:4) with Spring AMQP 4.1.1. The response times are values measured on this computer
// Only the methods are shown (class declarations, imports, and the example Order, OrderDto, orderService, etc. omitted)
@PostMapping("/orders")
OrderDto order(@RequestBody OrderRequest req) {
Order order = orderService.save(req); // saves the order
mailClient.sendConfirm(order); // calls the email server directly and waits
return OrderDto.of(order); // responds only after the email is done
}
All 36 combinations (with 1, 3, and 5 orders) were really run and checked. The methods on the screen were left as they are, only the declarations omitted on the screen were added, the code was compiled on JDK 21.0.12.1, and the order server and the email server were started on 127.0.0.1 of this computer with Spring Boot 4.1.1 (embedded Tomcat) and Spring Framework 7.0.9. The queue approach was run with RabbitMQ 4.4.1 (the official Docker image rabbitmq:4), Spring AMQP 4.1.1 (the RabbitTemplate created by the Spring Boot auto-configuration, and @RabbitListener), and the RabbitMQ Java client 5.30.0. The added declarations are all examples we made. orderService.save saves the order in memory and gives it a number (no database is used, so saving takes almost no time), mailClient.sendConfirm calls POST /mail of the email server with RestClient, mailSender.sendConfirm waits for the time it takes to send one email (0.2s, 1s, or 3s), and in the queue approach a Queue bean creates the queue order-created. The orders were sent as POST /orders requests, one after another. The response time of one order is a value measured on this computer. It is the median of 9 orders that have the same approach, time for one email, and email server state (the runs with 1, 3, and 5 orders put together), written to 0.001s, and it changes a little on every run. The first request after the run started took longer than the median (0.624s for the first request of the direct call with a 0.2s email, and 0.349s for the first request of the queue approach). The most messages in the queue is a value read inside RabbitMQ every 1ms, the maximum of the messages not yet handed over plus the messages handed over and waiting for the confirmation. In the combinations where the email server is normal, RabbitMQ handed each message to the email server as soon as it arrived (the prefetch of the receiving side that RabbitMQ reported is 250), and the messages were removed from the queue one at a time, after each email was sent and the confirmation arrived. The first email finished only after all the orders had been answered, so the most messages in the queue was the same as the number of orders. If many more orders keep coming or emails finish much faster, this number becomes smaller than the number of orders. "Down" was run as the state where the program of the email server is not running. In that state the direct call had its connection refused at once, ResourceAccessException (cause java.net.ConnectException) occurred, the order request ended with 500, the saved orders remained, and no email went out even when the email server was started again and watched for 1s. If the email server is running but does not respond, or if the machine itself is off, the caller waits until a timeout occurs, and that case was not run. In the queue approach with the email server down, after all the orders were sent RabbitMQ reported as many messages not handed over as there were orders and 0 receiving sides, and when the email server was started they were handled in the order they were put in. With a direct call you can also catch the exception to keep the order from failing, and the case where the queue itself goes down, message loss, duplicate delivery, ordering problems, and several receiving sides are not covered here. The check is done by verify/spring/cmp_mq.js.
Previous lab: Rate limiting · Next lab: Kafka partitions and consumers
Struck-through text was removed from the first version of the code; highlighted text was added.