Feature Request: Add "Random delay after last message" mode to Delay node

Feature Request: Add "Random delay after last message" mode to Delay node

What is this idea about

Currently, the Delay node's Rate Limit mode operates with rigid, fixed intervals. For example, if the rate is set to 1 message per 5 seconds, the messages are released at precise, deterministic 5-second increments.

While this is useful for strict throttling, it doesn't allow scenarios where messages need to be passed with randomized delays between each other. Therefore, I would like to propose a new mode: "Random delay after last message".

Proposed Behavior

Instead of a fixed interval T, the interval between the current message and the previous one would be a random value R within a user-defined range [X, Y].

Logic:
Let T{last}_ be the timestamp when the previous message was released.
The next message n will be released at:
Tn = max({arrival_time}, T{last} + {random}(X, Y))

This ensures that messages are neither sent too quickly (respecting the limit) nor sent in a perfectly periodic pattern, introducing much-needed entropy into the flow.

Proposed UI Changes

In the Delay node configuration, when this mode is selected:

  1. Mode Selection: A new option in the dropdown: Random delay from last message.
  2. Configuration Parameter: A numeric input to define the minimum and maximum interval).

Use Cases

  • IoT/Sensor Simulation: Simulating real-world hardware that transmits data at irregular, non-deterministic intervals.
  • Network Traffic Emulation: Testing how downstream services handle non-deterministic message arrival patterns.
  • Load Smoothing / Thundering Herd Prevention: Preventing synchronized "waves" of messages from hitting an API or database simultaneously by introducing jitter, rather then fixed delay.

I tried to search for similar feature request from someone before me. I only found similar issue as #5136 regarding the rigidity of the current rate-limiting implementation, but the discussion in my opinion was a bit off what I am aiming for. I think my solution would not need any heavy memory store but simply the last timestamp of last sent message.

I am happy to implement this and would love to hear the maintainers' thoughts on this approach before proceeding with a Pull Request. Thanks!

Why not just use a 2nd delay node after the rate limit node?

That is a valid workaround for simply adding some jitter to a flow. As I mentioned, combining a Rate Limit node with a Random Delay node forces a minimum interval (e.g., Trate+random(0,X)T\{rate} + {random}(0, X)Trate​+random(0,X)), which prevents the user from ever achieving an interval smaller than the rate limit.

I see this as a small but meaningful tweak to the functionality already offered by the Delay node. It essentially introduces a distinction between 'delaying every message independently' (the current behavior) and 'delaying the interval between consecutive output messages' (the proposed behavior). Rather than requiring users to manage workarounds to approximate this, I believe implementing it natively within the Delay node would be a much cleaner and more robust solution.

As I said, I'm happy to propose a PR. I just don't want to dive into it without proper discussion.

And that adds state to the node ... which isn't a minor change. Adding state to a node which is stateless is a really shame since statelessness is far simpler to handle than maintaining states all the time.

Is there a alternative of creating a custom delay node in a custom package? If the node hasn't yet got any state information, then changing that is something for a custom node package perhaps.

Does it not already have state for the mode rate limit with queueing?

Me not know, if the state is already there then what I said becomes stateless, i.e. irrelevant.

I was just pointing out that the suggested behaviour would require state for each delay node.

But by the sounds of it, you're right - for rate limiting it will probably already have state.

You are absolutely right. The rate limit mode already manages state through its queue and timing mechanisms to ensure messages are released at the correct intervals.

My proposal doesn't aim to introduce a fundamentally new kind of state, but rather a different logic for how that existing timing state is calculated. Instead of using a deterministic, fixed interval for the next release, we would use a stochastic one.

Essentially, we are not adding new complexity to the node's architecture, but simply providing an alternative mathematical rule for the scheduling logic already present in the rate limit implementation. This allows users to achieve jittered arrival patterns without the need for extra nodes or manual workarounds.

Can you not use the msg.rate option to set the rate dynamically ? - If the option is set you could add a random msg.rate to each incoming message and change each one.

That unfortunately doesn't quite achieve the intended behavior.

The main difference is that the random interval needs to be determined after each message is sent, rather than when a new message arrives.

With the current implementation changing msg.rate restarts the existing setInterval() timer. If messages keep arriving more frequently than the selected interval, the timer can be repeatedly reset.
Also, if several messages are already queued and no new messages arrive, they are subsequently released at the last configured rate, without further randomization.

Each incoming message can change the rate threshold, including messages that are subsequently dropped. The intended behavior is to select a random interval once after a successful emission and retain it until the next message is accepted.

What I'm proposing is a random interval between consecutive output messages, independent of the arrival pattern.

Having looked at 89-delay.js, I think this could be implemented with a relatively small modification to the existing Rate mode: use a rescheduled setTimeout() instead of setInterval() for queued messages, and sample a new threshold after each successful emission in drop mode.

That would preserve the existing queueing, dropping, and second-output functionality, with no changes to that current code there. On the configuration side, it could simply be an additional checkbox to enable random intervals, reusing the existing minimum/maximum delay settings.

Thanks all for the feedback — in the meantime I tried a few approaches, went
through the Delay node code and finally settled on one. I've implemented it in
my fork, on the delay-node-random-rate branch:

The whole change turned out really small (two commits: the feature itself and
a matching set of tests), and it reuses as much of the existing code and
configuration as possible. Specifically:

  • It's an additional "Random interval between messages" checkbox in the
    Rate Limit → All messages mode, reusing the existing random-range settings
    (randomFirst / randomLast / randomUnits) that already exist for the
    Random delay mode — so there are no new node properties the user has to
    learn.
  • On the runtime side, as I suggested earlier, the queue mode now uses a
    rescheduled setTimeout() instead of setInterval(), and a new interval is
    sampled after each successful emission in drop mode. So the random interval
    is chosen after each message is sent, independent of the arrival pattern —
    which is exactly the behaviour msg.rate couldn't provide.
  • No new kind of state is involved: it just changes the rule for computing the
    next interval within the timing state the rate-limit mode already maintains
    (exactly as discussed above).

With the option enabled, the interval between consecutive releases is a random
value from the configured range — the first message goes out immediately, and
msg.rate is ignored in this mode since it can't sensibly combine with a range.

I added two tests covering queue mode (gaps always within the configured range)
and drop mode (messages actually dropped, first message immediate). I will try to create a PR from this and send upstream. Happy to hear maintainer feedback on
the approach (and on the label — I chose "Random interval between messages",
feel free to propose different wording).

Sounds good. Happy to look at a PR