What is a message queue?
The message queue is one of the fundamental building blocks of distributed systems and a highly used piece of technology in system design. Let’s examine a real-life analogy to simplify the component and make it easier to understand.
In a restaurant, the messaging queue would be the order wheel, where the waiter places your order and it waits for the kitchen to start preparing the food. The key is that the order waits until the kitchen has bandwidth to process it.
If the kitchen had to start preparing the food as soon as the order is received, it would get overwhelmed after a few orders. As more orders come in, the quality of the food would decrease, and eventually, the kitchen wouldn’t be able to send out correct orders.
The order wheel buffers the order from the actual cooking, and a message queue buffers requests from being processed when the “order” is entered.
By giving the applications time to complete their current workload before fetching new orders to process, it keeps the server’s resource utilization at a safe percentage and prevents crashes.
The decoupled nature of when the client sends the request and when the application processes the data makes the message queue a staple in creating a system that uses microservices rather than large monolithic applications.
When do we use a message queue?
When trying to determine if you should add a message queue to your system there are a few questions that help verify the use case:
1. Can the client wait for the data to be processed sometime in the future?
2. Does it take a long time to process the data/request?
3. Is there either a large volume of requests, or do the number of requests spike up/down?
Although not an exhaustive list, the questions cover a large number of use cases, which is why the message queue is such a vital piece of system architecture.
Quick Example
How could a message queue be used in a generic professional social media website? Social media is a read heavy application because users read more than they write to the platform. The post writing service would be a great place to insert a message queue. A user doesn’t *need* their post processed immediately (but they want it done in a reasonable time), and the volume is spiky (users are more active during the workday).
The message queue would fit nicely to temporarily store the post before it gets processed and added to the database for other users to see.


