Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a FIFO Qeueing system, what's the best way the to implement priority messaging

For message-oriented middleware that does not consistently support priority messages (such as AMQP) what is the best way to implement priority consumption when queues have only FIFO semantics? The general use case would be a system in which consumers receive messages of a higher priority before messages of a lower priority when a large backlog of messages exists in Queue(s).

like image 221
quaternion Avatar asked Jul 16 '09 17:07

quaternion


People also ask

How do you implement a priority queue?

Priority Queues can be implemented using common data structures like arrays, linked-lists, heaps and binary trees. The list is so created so that the highest priority element is always at the head of the list. The list is arranged in descending order of elements based on their priority.

What is priority FIFO?

FIFO. "first in, first out," a queueing discipline in which the first member to arrive is the first to be removed. priority queue. A queueing discipline in which each member has a priority determined by external factors. The member with the highest priority is the first to be removed.

Are message queues FIFO?

Queues by default operate using FIFO which means first in, first out. This approach ensures that if a backlog of messages build up, the oldest messages are processed first.

How does priority queue work?

A priority queue is a special type of queue in which each element is associated with a priority value. And, elements are served on the basis of their priority. That is, higher priority elements are served first. However, if elements with the same priority occur, they are served according to their order in the queue.


1 Answers

Given only FIFO support for a given single queue, you will of course have to introduce either multiple queues, an intermediary, or have a more complex consumer.

Multiple queues could be handled in a couple of ways. The producer and consumer could agree to have two queues between them, one for high-priority, and one for background tasks.

If your producer is constrained to a single queue, but you have control over the consumer, consider introducing a fan-out router in the path. So producer->Router is a single queue, and the router then has two queues to the consumer.

Another way to tackle it, which is likely less than ideal, would be to have your consumer spin a thread to front the queue, then dispatch the work internally. Something like the router version above, but inside a single app. This has the downside of having multiple messages in flight inside your app, which may complicate recovery in the event of a failure.

Don't forget to consider starvation of the effectively low-priority events, whatever they are, if some of them should be processed even if there are higher-priority events still hanging about.

like image 198
sdg Avatar answered Nov 16 '22 04:11

sdg