Why are message queues so important in software engineering (and pizza shops)?

Aditya Rana
4 min readFeb 11, 2022

--

A message queue is a system that allows you to pool your jobs into a queue so your servers can take those unfinished tasks one by one and start processing them.

Phew, that’s a mouthful.

Here, we are going to learn about them in a non-boring way. In this article, I’ve written about how I have implemented queues in a real problem.

Queue

Why use queues?

Client’s perspective

Imagine you’re dining at a pizza shop. You choose those delightful toppings and call the waiter to place your order. The waiter doesn’t respond to you, doesn’t even seem to write down your creative selection of toppings, just bugs off and returns thirty minutes later with your pizza. Sure, you got your order but you were left salivating all this time without even knowing if your request is being taken seriously.

Now a different scenario — the waiter reads back your order to you, gives you your order number and puts your order in a queue. Now you can chill with your friends, take all those photos to post on Instagram and not worry about your order.

Here, the first system was a request-response model where your request needs to be responded to exactly with the resource you requested, nothing else, which is the pizza in this case. This means if there’s a delay in processing, you don’t know anything about the status of the request.

The second system was a queue-based model where you got your confirmation and you know the order will start processing when the chef is free and it will be delivered to you when it’s done.

Request-response model

Server’s perspective

Another advantage of the second system is from the perspective of the pizza shop. If they get famous, a single chef can only handle so many pizzas. So they can hire another one. Now, this new chef can also use the existing pinboard to take orders — no onboarding required. Both chefs happy!

When they want to take the next order, they take it from the pinboard (let’s say) on which all these orders are placed. This way, the waiter can communicate with them effectively and let the client know that the request has been sent to the relevant party. The chefs can coordinate among themselves better as they don’t end up making the same order and they share the load.

If you’re a dummy, these chefs are analogous to worker servers. The waiter is the main server you send your request to and the pinboard is the message queue.

This system allows you to easily add more workers. You just have to configure them to use your queue. This is also closely related to a solution that allows different Microservices to communicate with each other.

Queues also ensure that the CPU remains busy for the maximum amount of time as it requests for the next job as soon as it finishes the current one. If one of your servers goes down, it doesn’t crash the entire system as others are still up and running. The faulty server can start functioning normally once it’s fixed.

Queue based model

There are many more advantages of this system but it may fail too in some cases. This video explains the disadvantages with a great example.

Some services to consider

Here are some of the popular services that can do the heavy lifting for you-

  1. AWS SQS
  2. Kafka
  3. RabbitMQ

Out of these, I’ve used SQS which worked pretty well for me. It offers free service for 1 million requests. Kafka and Rabbit MQ are open source alternatives.

Conclusion

All these advantages make message queues an integral part of architectures used by fancy companies and therefore something you should learn as a software engineer.

Further reading

Amazon’s Article

Queue services

AWS SQS

RabbitMQ

Kafka

--

--

Aditya Rana
Aditya Rana

Written by Aditya Rana

Software Developer | Web 3 enthusiast

No responses yet