Skip to content

Retries with backoff algorithm #223

@zaheershaik

Description

@zaheershaik

I'm trying to implement a basic multiple retry mechanism in case of processing failures in message consumer. Basically, requeue the message with some backoff time period (preferably exponential backoff) and exhaust after certain number of attempts.

For retry: I was trying to set 'x-retry-count' message header before channel.reject without success. The message header is getting reset and not propagating in the next delivery attempt.

For backoff: I was setting expiration property, which was getting reset too.

Could you please suggest some mechanism to do retries with some kind of backoff, if this is not the right way?

channel.consume(queue, function (msg) { //initiate consume
  work(msg, function (ok) { //process message
    try {
      if (ok)
        channel.ack(msg); //ack message
      else {
        var retryCount = msg.properties.headers["x-retry-count"] || 0;
        msg.properties.expiration = ++retryCount * 2000 * (Math.pow(3, (retryCount - 1)));
        msg.properties.headers["x-retry-count"] = retryCount;
        console.debug("Set retry count on msg", retryCount);
        channel.reject(msg, true); //nack message
      }
    } catch (e) {
      console.error("Message processing error", e);
    }
  });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions