A queue is responsible for holding messages which are produced by producers and are delivered to consumers.
RedisSMQ supports 3 types of queues: LIFO queues, FIFO queues, and Priority queues.
All RedisSMQ queue types are reliable. A queue is said to be reliable, when during a failure scenario, let it be for example a consumer crash, it can recover from such failure and the message being processed is not lost.
In a typical use case, both LIFO and FIFO queues use brpoplpush, which blocks the connection to the Redis server until a message is received. However, priority queues use pooling and lua scripting which introduce a little of overhead on the MQ and therefore priority queues are less performant than other queue types.
In the next examples for simplicity we are going to use a EQueueDeliveryModel.POINT_TO_POINT
delivery model for all the queues. But you may choose any combination of EQueueType
and EQueueDeliveryModel
that fits your application.
In a LIFO queue the last published messages are always delivered first and the first published messages are delivered last.
const { Queue, EQueueType, EQueueDeliveryModel } = require('redis-smq');
const queue = new Queue();
queue.save(
'my_queue',
EQueueType.LIFO_QUEUE,
EQueueDeliveryModel.POINT_TO_POINT,
(err) => {
if (err) console.error(err);
},
);
See Queue.save() for more details.
In a FIFO queue the first published messages are delivered first and the last published messages are delivered last.
const { Queue, EQueueType, EQueueDeliveryModel } = require('redis-smq');
const queue = new Queue();
queue.save(
'my_queue',
EQueueType.FIFO_QUEUE,
EQueueDeliveryModel.POINT_TO_POINT,
(err) => {
if (err) console.error(err);
},
);
See Queue.save() for more details.
In a priority queue, messages with higher priority are always delivered first before messages with lower priority.
const { Queue, EQueueType, EQueueDeliveryModel } = require('redis-smq');
const queue = new Queue();
queue.save(
'my_queue',
EQueueType.PRIORITY_QUEUE,
EQueueDeliveryModel.POINT_TO_POINT,
(err) => {
if (err) console.error(err);
},
);
See Queue.save() for more details.
To set up a message priority, the ProducibleMessage Class provides the following methods:
Valid message priority values that you can apply to a given message are:
EMessagePriority.LOWEST
EMessagePriority.VERY_LOW
EMessagePriority.LOW
EMessagePriority.NORMAL
EMessagePriority.ABOVE_NORMAL
EMessagePriority.HIGH
EMessagePriority.VERY_HIGH
EMessagePriority.HIGHEST
See EMessagePriority.
Queues in RedisSMQ are namespaced.
A queue namespace is used as a scope for a given set of queues to ensure unique queue names and to avoid name collisions when multiple queues share the same name.
A given queue can be either identified by a queue name like orders
or explicitly by its queue name and namespace for example { ns: 'my-app', name: 'orders' }
.
By default, when a namespace for a queue is not specified, the queue is assigned to the default namespace.
The default namespace can be configured from your configuration object. See Configuration for more details.
A queue name is required to fulfill the following requirements:
- To be composed of alphanumeric characters
[a-z0-9]
for examplequeue$
is an invalid queue name; - May include
-
and_
characters for examplemy-queue
ormy_queue
are valid queue names; - To start with an alpha character
[a-z]
and ends with an alphanumeric character[a-z0-9]
for example3queue
ormy_queue_
are invalid queue names; - Is allowed to include dots (
.
) for denoting queues hierarchy for examplesports.football
. See Topic Exchange;
To manage queues and namespaces RedisSMQ provides:
To manage queue messages use:
- QueueMessages Class
- QueuePendingMessages Class
- QueueAcknowledgedMessages Class
- QueueDeadLetteredMessages Class
- QueueScheduledMessages Class
Please note that queue acknowledged messages and dead-lettered messages are not saved by default.
To manage acknowledged and dead-lettered messages you need to enable acknowledged/dead-lettered messages storage from your RedisSMQ Configuration.