weyoss / redis-smq

A simple high-performance Redis message queue for Node.js.
MIT License
588 stars 64 forks source link

Hash naming convention question #68

Closed PhantomRay closed 2 years ago

PhantomRay commented 2 years ago

I noticed that the hash name of a queue looks like redis-smq-v600rc12.live.2.queue-12345. My points are

Also, for namespace, it's a one time configuration. Is it possible to dynamically change when using producer and consumer? I think this is very useful.

weyoss commented 2 years ago

Good question.

A Redis key starts with a prefix, and at this time it is redis-smq-v600rc12. This prefix is also the latest compatible version of Redis keys.

Actually, the key prefix contains the version of the redis-smq at which changes to Redis keys where made (new keys could be added, existing keys could be deleted, renamed, or maybe the schema of the stored data could be updated).

Currently, the latest RedisSMQ release is v6, and it is compatible with the latest updates of Redis keys which were made when redis-smq were at v6.0.0-rc.12.

In other words, if you were using redis-smq@6.0.0-rc.12 and then upgraded your installation to redis-smq@6.1.0, your existing Redis keys and data will be used.

This way when a new release is published, it is guaranteed that it will work correctly using the Redis keys from the latest compatible version. Incompatible keys from previous RedisSMQ releases will be kept untouched so your existing data would not be lost or corrupted due to inconsistency issues.

weyoss commented 2 years ago

Also, for namespace, it's a one time configuration. Is it possible to dynamically change when using producer and consumer? I think this is very useful.

No. The namespace is not a one time configuration. The namespace from the configuration is the default namespace.

Let me make it clear. For a given message, the Message API allows you to specify either a queue name or a queue name plus a namespace. When you specify only a queue name, the default namespace is used.

See https://github.com/weyoss/redis-smq/blob/master/docs/api/message.md#messageprototypesetqueue for more details.

Example:

Given the current configuration:

const config = {
   namespace: 'my-awesome-app'
}

When publishing a message, you can set its queue as follows:

const msg = new Message();
msg.setQueue('my-queue');
msg.getQueue(); // { name: ''my-queue', ns: 'my-awesome-app' }

const msg2 = new Message();
msg2.setQueue({ name: 'my-queue', ns: 'live' });
msg2.getQueue(); // { name: ''my-queue', ns: 'live' }

The Consumer API also allows you to consume messages from different namespaces. See https://github.com/weyoss/redis-smq/blob/master/docs/api/consumer.md#consumerprototypeconsume.

So to consume messages from my-queue@live:

consumer.consume({ name: 'my-queue', ns: 'live' }, false, messageHandler, (err, isRunning) => {
   // ...
});
PhantomRay commented 2 years ago

Thank you as always @weyoss msg.setQueue({ name: 'my-queue', ns: 'live' }); - problem solved. ^_^