Moleculer redis transporter issue
Prerequisites
- I am running the latest version — we hit this on 0.14.32, but the analysis below
shows the same code path exists on current
master(src/transporters/redis.js) - I checked the documentation and found no answer
- I checked to make sure that this issue has not already been filed
- I'm reporting the issue to the correct repository
Current Behavior
After a transient connection failure during RedisTransporter.connect(), a node can end
up with two subscribed sub clients on its own request topics. From that moment on,
every packet delivered to the node is processed twice with the same ctx.requestID:
action handlers run twice (duplicated side effects — double DB writes, double billing in
our case) and both executions publish a response for the same request id, so the caller
resolves with whichever finishes first. The state is permanent until the process restarts.
Two independent retry mechanisms race in connect():
When the pub client fails to connect,
connect()rejects and transit retriesconnect(), creating a brand-new sub/pub pair. The clients from the previous attempt are never disconnected — the old sub stays connected with its ownmessageBufferlistener.Meanwhile ioredis auto-retries the old pub client on its own. When it eventually connects, the
clientPub.on("ready")callback from the oldconnect()closure runs:clientPub.on("ready", () => { this.clientSub = clientSub; // re-promotes the OLD pair this.clientPub = clientPub; this.onConnected().then(resolve); // makeSubscriptions() → OLD sub subscribes too });
Both sub clients end up subscribed → duplicate delivery. (On 0.14.x the event is
"connect", on master it is "ready" — both re-fire on every ioredis auto-reconnect,
so the race is identical.)
Expected Behavior
A connect() retry should tear down the clients created by the previous attempt (or
guard against stale ready callbacks), so that a node never has more than one
subscribed sub client. Suggested fix at the top of connect():
[this.clientSub, this.clientPub, this._clientSub, this._clientPub].forEach(client => {
if (!client) return;
try {
client.removeAllListeners();
client.disconnect(false); // do not reconnect
} catch (e) { /* already destroyed */ }
});
this.clientSub = this.clientPub = this._clientSub = this._clientPub = null;We are running exactly this as a RedisTransporter subclass in production and it
eliminates the duplication.
Failure Information
Production timeline (annotated from our logs):
18:47:08.039 Setting Redis transporter → connect(): creates SUB#1
18:47:08.085 Redis-sub client is connected. → SUB#1 ok
18:47:08.085 Setting Redis transporter → creates PUB#1
18:47:18.087 Redis-pub error connect ETIMEDOUT → PUB#1 TCP connect timed out (10s)
18:47:18.087 TRANSIT: Connection is failed. → transit schedules connect() retry (+5s)
18:47:18.145 Redis-pub client is connected. ⚠ → PUB#1 connected 58ms later
(ioredis retried on its own)
→ stale callback → onConnected()
→ SUB#1 subscribes
18:47:23.088 Setting Redis transporter → transit retry: creates SUB#2
18:47:23.096 Redis-sub client is connected.
18:47:23.096 Setting Redis transporter → creates PUB#2
18:47:23.103 Redis-pub client is connected. → onConnected() again → SUB#2 subscribesAfter that, every action call is handled twice, ms apart, same requestID:
18:49:02.001 INFO ... action log { requestID: '78d7cbe98abf...' }
18:49:02.004 INFO ... action log { requestID: '78d7cbe98abf...' } ← duplicateSteps to Reproduce
- Start a broker with the Redis transporter behind a TCP proxy to Redis.
- Let the first connection (sub client) succeed, but make the second one (pub client) time out on its first attempt and succeed on retry (this mirrors a transient network blip; ioredis default connect timeout is 10s).
- Transit retries
connect()~5s after the failure and creates a second pair. - Call any action targeting this node from another node: the handler runs twice
with the same
ctx.requestID.
Context
- Moleculer version: 0.14.32 (code path unchanged on current master)
- NodeJS version: 20.x (ioredis 5.x)
- Operating System: Linux (Kubernetes; Redis standalone)
Failure Logs
1) **Service Startup Log** —
[2026-07-09T18:47:08.038Z] INFO market-service-74f97d9b6b-4cmk6-1/TRANSIT: Connecting to the transporter...
ioredis v5 does not support plugging third-party Promise library anymore. Native Promise will be used.
[2026-07-09T18:47:08.039Z] INFO market-service-74f97d9b6b-4cmk6-1/TRANSPORTER: Setting Redis transporter
[2026-07-09T18:47:08.085Z] INFO market-service-74f97d9b6b-4cmk6-1/TRANSPORTER: Redis-sub client is connected.
ioredis v5 does not support plugging third-party Promise library anymore. Native Promise will be used.
[2026-07-09T18:47:08.085Z] INFO market-service-74f97d9b6b-4cmk6-1/TRANSPORTER: Setting Redis transporter
[2026-07-09T18:47:18.048Z] ERROR market-service-74f97d9b6b-4cmk6-1/CACHER: Error: connect ETIMEDOUT
at Socket.<anonymous> (/app/node_modules/ioredis/built/Redis.js:170:41)
at Object.onceWrapper (node:events:632:28)
at Socket.emit (node:events:518:28)
at Socket.emit (node:domain:488:12)
at Socket._onTimeout (node:net:589:8)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect'
}
[2026-07-09T18:47:18.087Z] ERROR market-service-74f97d9b6b-4cmk6-1/TRANSPORTER: Redis-pub error connect ETIMEDOUT
[2026-07-09T18:47:18.087Z] WARN market-service-74f97d9b6b-4cmk6-1/TRANSIT: Connection is failed. connect ETIMEDOUT
[2026-07-09T18:47:18.087Z] WARN market-service-74f97d9b6b-4cmk6-1/TRANSPORTER: Redis-pub client is disconnected.
[2026-07-09T18:47:18.113Z] INFO market-service-74f97d9b6b-4cmk6-1/CACHER: Redis cacher connected.
[2026-07-09T18:47:18.145Z] INFO market-service-74f97d9b6b-4cmk6-1/TRANSPORTER: Redis-pub client is connected.
2) **Duplicate call to service action Log** —
[2026-07-09T18:49:02.001Z] INFO market-service-74f97d9b6b-4cmk6-1/MARKET: [revealCompanies] request received { lockKey: 'xxxxxx', requestID: '78d7cbe98abf7b3c3a7fe45b1dea0acd', caller: 'gateway', cnpjsCount: 1, clientIp: 'xxxxxxxxxx', userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36' }
[2026-07-09T18:49:02.004Z] INFO market-service-74f97d9b6b-4cmk6-1/MARKET: [revealCompanies] request received { lockKey: 'xxxxxx', requestID: '78d7cbe98abf7b3c3a7fe45b1dea0acd', caller: 'gateway', cnpjsCount: 1, clientIp: 'xxxxxxxxxx', userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36' }Source: moleculerjs/moleculer