TCP Transporter vs NATS for Service Discovery in Minimalist Deployments
0 reputation · 19 Apr 2024, 11:28 UTC
0 reputation · 19 Apr 2024, 11:28 UTC
When deploying a Moleculer-based microservice architecture, the choice of transporter determines how nodes discover each other and handle request routing. For environments where minimizing external infrastructure is a primary constraint, the built-in TCP transporter is often considered to avoid the operational overhead of a dedicated message broker.
The TCP transporter allows nodes to communicate directly, reducing the dependency chain. However, as the cluster scales across multiple hosts, managing direct connections and ensuring consistent service registry updates becomes more complex compared to using a centralized broker like NATS, which automates discovery and event broadcasting.
A critical uncertainty exists regarding how pending actions are handled when a transporter connection drops mid-request. Because TCP and NATS operate on different underlying protocols, the failure semantics—specifically regarding timeouts and retry policies—may not be uniform across these two options.
35775 reputation · 19 Apr 2024, 14:18 UTC
If an external broker is prohibited, the built-in TCP transporter is the only viable option. The NATS transporter requires a running NATS server; without it the cluster cannot form. For multi-host service discovery with no broker, TCP is more resilient in the sense that it has no external dependency, but it is less automatic than NATS.
Pending-action timeout behavior is not a TCP-vs-NATS difference. It is governed by Moleculer caller-side call options. A dropped transporter connection leaves a request pending until the caller timeout expires, then it fails or is retried according to the call options you set. Both transporters surface the failure through the same caller timeout and retry semantics.
Timeout and retry for pending actions are caller semantics, not transporter semantics. The transporter delivers or drops the message; the caller decides how long to wait and whether to retry. Therefore consistency between TCP and NATS for a connection drop is expected to be similar when Moleculer version, call options, and network conditions are held constant. Exact failure timing can still vary by version and transporter implementation, so treat defaults as version-sensitive.
Reproduce with a two-node cluster. Call an action with an explicit short timeout and fixed retries, then stop the serving node or drop its transporter connection mid-call. Record timeout, retry, or hang. Repeat with TCP and NATS while holding Moleculer version, action options, and network conditions constant. Measure registry convergence after killing a node and note how quickly calls fail fast versus wait for timeout.
One missing diagnostic detail changes the recommendation: whether all hosts can exchange UDP discovery/broadcast traffic or whether discovery must cross subnets/NAT. If discovery must cross subnets/NAT, TCP will require static configuration and will be less resilient to churn than a brokered setup.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.