Moleculer Service Broker and NATS Streaming Transporter: Handling In‑Flight Requests During a Failed Upgrade
0 reputation · 19 Nov 2021, 12:11 UTC
0 reputation · 19 Nov 2021, 12:11 UTC
Ensure that a failed service upgrade can be rolled back without losing messages or leaving in‑flight requests unresolved, while respecting Moleculer’s lack of automatic draining and the transporter’s delivery guarantees.
When the new version is stopped via broker.stopService, ongoing calls are aborted unless custom logic waits for them to finish, and the transporter may not persist unacknowledged messages if it does not support acknowledgements.
35775 reputation · 19 Nov 2021, 19:43 UTC
1. Drain before stopping: Implement a custom shutdown routine that stops accepting new calls, tracks the number of active action promises, and waits (with a hard timeout) for that counter to reach zero before invoking broker.stop().
2. Pre‑stop hook vs. transporter acks: A pre‑stop hook is required; transporter acknowledgements only protect the message queue, not the already‑executed local calls. Both mechanisms should coexist.
3. Verify redelivery: After restarting the previous version, check the NATS/JetStream consumer’s pending and redelivered metrics. A non‑zero redelivered count confirms that unacknowledged messages were re‑queued and processed.
broker.stop() terminates the node immediately; any action that has already entered the handler is cancelled.this.accepting = true) and expose an activeRequests counter via a health endpoint.async action(ctx) {
this.activeRequests++;
try {
return await ctx.call(this.actionName, ctx.params);
} finally {
this.activeRequests--;
}
}preStop method), set this.accepting = false, then wait until this.activeRequests becomes zero or a timeout (e.g., 30 s) elapses.await broker.stop() (or broker.stopService() for a single service).js.ConsumerInfo()) and confirm:
pending > 0 during the stop.redelivered has increased.RequestTimeoutError or retry entries – they indicate that the request was retried or redelivered, not silently dropped.To tailor the timeout and idempotency strategy, please share: Which NATS transporter (core, STAN, or JetStream) and version are you using?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.