smrchy/rsmq

Receiving, recommended way & id generating

Kamil93 opened this issue · 1 comments

setTimeout or nextTick? Which callback registering is better for recv messages by rsmq?
Also there is an option for receiving N messages at once?

Is there posibility for overriding generate id function that generates id for messages?
I think in some cases the random part of id may be shorter for better performance & still unique: https://zelark.github.io/nano-id-cc/

erdii commented

setTimeout or nextTick

I would use setTimeout, because waiting some time (at least 1 second) will definitely decrease cpu and network usage in your node and redis processes. nextTick is essentially a "restless" lopp that only alternates between fetching a message from rsmq and working on that message.

How about something like this?

const interval = 10000; // try to start a worker iteration each 10s

function workerTick() {
  const start = Date.now();
  // fetch message
  // ...
  // finished message processing
  const elapsedTime = Date.now() - start;
  const waitTime = interval - elapsedTime;
  setTimeout(workerTick, Math.max(0, waitTime);
}

// start loop
workerTick()

Also there is an option for receiving N messages at once?

In RSMQ, there is no such thing as receiving multiple messages at once.

Is there posibility for overriding generate id function that generates id for messages?

The id generation has to use the current format, because the ordering of messages relies on it. Also you would need to introduce the new id algorithm in all other RSMQ implementiations (like PhpRSMQ and jRSMQ), to ensure protocol interoperability.