Design a Distributed Messaging Queue
Learn to design a distributed messaging queue.
Messaging queue
A messaging queue is an intermediate component between the interacting entities known as producers and consumers. The producer produces messages and places them in the queue, while the consumer retrieves the messages from the queue and processes them. There might be multiple producers and consumers interacting with the queue at the same time.
Here is an illustration of two applications interacting via a single messaging queue:
Requirements
Let’s define the functional and non-functional requirements of the system.
Functional requirements
Listed below are the actions that a client should be able to perform:
-
Queue creation: The client should be able to create a queue and set some parameters—for example, queue name,
, andqueue size A queue size is the maximum number of messages a queue can contain. For example, Amazon’s standard queue size is unlimited. However, for inflight messages, the size is limited to 120,000. .maximum message size MaxMessageSize -
Send message: Producer entities should be able to send messages to a queue that’s intended for them.
-
Receive message: Consumer entities should be able to receive messages from their respective queues.
-
Delete message: The consumer processes should be able to delete a message from the queue after the successful processing of the message.
-
Queue deletion: Clients should be able to delete a specific queue.
Non-functional requirements
Listed below are the non-functional requirements:
-
Durability: The data received by the system should be durable and shouldn’t be lost. Producers and consumers can fail independently, and a queue with data durability is critical to make the whole system work because other entities are relying on the queue.
-
Scalability: The system needs to be scalable and capable of handling the increased load, queues, producers, consumers, and the number of messages. Similarly, when the load reduces, the system should be able to shrink the resources accordingly.
-
Availability: The system should be highly available for receiving and sending messages. It should continue operating uninterrupted, even after one or more of its components fail.
-
Performance: The system should provide high throughput and low latency.
Considerations of a distributed messaging queue’s design
Let’s go through the factors that affect the design of a messaging queue.
Ordering of messages
A messaging queue is used to receive messages from producers. These messages are consumed by the consumers at their own pace. Some operations are critical in that they require strict ordering of the execution of the tasks, driven by the messages in the queue. For example, while chatting over a messenger application with a friend, the messages should be delivered in ...