RedisSMQ / Docs / Queue Delivery Models
RedisSMQ offers two reliable message delivery models: Point-to-Point and Pub/Sub. Below, we delve into each model, providing detailed explanations, sample code snippets, and best practices for implementation.
In the Point-to-Point model, a message is produced to a queue and delivered to a single consumer at a time. This model ensures that each message is processed only once by one consumer.
To create a Point-to-Point queue, use the following code snippet:
const { Queue, EQueueDeliveryModel, EQueueType } = require('redis-smq');
const queue = new Queue();
queue.save(
'my-queue',
EQueueType.LIFO_QUEUE,
EQueueDeliveryModel.POINT_TO_POINT,
(err, reply) => {
if (err) {
console.error('Error creating queue:', err);
} else {
console.log('Successfully created queue:', reply);
}
}
);
Refer to Queue.save() for additional details.
To publish a message to a Point-to-Point queue, use the following:
const { Producer, ProducibleMessage } = require('redis-smq');
const message = new ProducibleMessage();
message.setBody('hello world').setQueue('my-queue');
const producer = new Producer();
producer.run((err) => {
if (err) {
console.error('Error running producer:', err);
} else {
producer.produce(message, (err, reply) => {
if (err) {
console.error('Error producing message:', err);
} else {
console.log('Successfully produced message:', reply);
}
});
}
});
Refer to Producer.produce() for more details.
To consume a message from a Point-to-Point queue, you can use the following snippet:
const { Consumer } = require('redis-smq');
const consumer = new Consumer();
const messageHandler = (msg, cb) => {
// Acknowledge the message
cb();
};
consumer.consume('my-queue', messageHandler, (err) => {
if (err) {
console.error('Error adding message handler:', err);
} else {
console.log('Message handler added successfully');
}
});
consumer.run((err) => {
if (err) {
console.error('Error running consumer:', err);
}
});
In the Pub/Sub model, messages are delivered to all consumers of a queue. Every consumer receives and processes a copy of the produced message.
To consume messages from a Pub/Sub queue, a consumer group is required.
- When publishing a message to a Pub/Sub queue, it is sent to all consumer groups associated with that queue.
- Within each consumer group, only one consumer will receive the message.
- If a message remains unacknowledged for a given time, it will be retried in the same manner as within a Point-to-Point queue.
- If the retry threshold is exceeded, failed messages can be stored, if configured, in the dead-letter queue for that Pub/Sub queue.
To create a Pub/Sub queue, use the following:
const { Queue, EQueueDeliveryModel, EQueueType } = require('redis-smq');
const queue = new Queue();
queue.save(
'my-pubsub-queue',
EQueueType.LIFO_QUEUE,
EQueueDeliveryModel.PUB_SUB,
(err, reply) => {
if (err) {
console.error('Error creating Pub/Sub queue:', err);
} else {
console.log('Successfully created Pub/Sub queue:', reply);
}
}
);
Refer to Queue.save() for additional details.
A consumer group is automatically created when consuming messages from the queue if it does not already exist.
You can also manually create consumer groups using the ConsumerGroups.saveConsumerGroup() method.
Refer to the ConsumerGroups Class for managing consumer groups.
Use the following code to publish a message to a Pub/Sub queue:
const { ProducibleMessage, Producer } = require('redis-smq');
const message = new ProducibleMessage();
message.setBody('hello world').setQueue('my-pubsub-queue');
const producer = new Producer();
producer.run((err) => {
if (err) {
console.error('Error running producer:', err);
} else {
producer.produce(message, (err, reply) => {
if (err) {
console.error('Error producing message:', err);
} else {
console.log('Successfully produced message:', reply);
}
});
}
});
Note: When producing a message to a Pub/Sub queue, if no consumer groups exist, an error will be returned. Ensure that at least one consumer group is created prior to publishing messages.
Refer to Producer.produce() for more details.
To consume a message from a Pub/Sub queue, ensure to specify the consumer group ID:
const { Consumer } = require('redis-smq');
const consumer = new Consumer();
const messageHandler = (msg, cb) => {
// Acknowledge the message
cb();
};
consumer.consume(
{ queue: 'my-pubsub-queue', groupId: 'my-app-group-1' },
messageHandler,
(err) => {
if (err) {
console.error('Error adding message handler:', err);
} else {
console.log('Message handler added successfully');
}
}
);
consumer.run((err) => {
if (err) {
console.error('Error running consumer:', err);
}
});
Remember to provide the consumer group ID when consuming messages from a Pub/Sub queue.