The NATS client is failing to connect because it cannot find any healthy NATS servers to establish a connection with.

Here’s why that’s happening and how to fix it:

1. NATS Server Not Running

Diagnosis: Check the status of the NATS server process.

systemctl status nats-server

If it’s not active, start it:

systemctl start nats-server

Why it works: The NATS server process is the core of the NATS system. If it’s not running, there are no services for clients to connect to.

2. Incorrect Server Address/Port

Diagnosis: Verify the listen address and port configured in your NATS server’s configuration file (often nats-server.conf or similar, typically in /etc/nats/ or /usr/local/etc/nats/). Ensure it matches what your client is trying to connect to. A common default is 0.0.0.0:4222 or localhost:4222. Check the client’s configuration. Most clients have a way to specify the server URL, e.g., nats://localhost:4222 or nats://your_server_ip:4222. Fix: If the server is listening on 0.0.0.0:4222 but your client is trying to connect to localhost:4222 and the server is not on the same machine, update the client’s connection string to use the server’s IP address. If the server is configured to listen on a different port, update the client to match. Why it works: The client needs to know the exact network address and port where the NATS server is actively listening for incoming connections. Mismatched addresses or ports mean the client is looking in the wrong place.

3. Firewall Blocking Connection

Diagnosis: On the server where NATS is running, check your firewall rules. For iptables, you might see something like:

sudo iptables -L -n | grep 4222

For ufw:

sudo ufw status | grep 4222

If port 4222 (or your configured NATS port) is blocked, you’ll need to allow incoming traffic. Fix: To allow traffic on port 4222 with ufw:

sudo ufw allow 4222/tcp

With iptables:

sudo iptables -A INPUT -p tcp --dport 4222 -j ACCEPT

Why it works: Firewalls act as gatekeepers, preventing unauthorized network access. If the firewall is blocking the NATS port, client connection attempts are dropped before they even reach the NATS server process.

4. Server Overloaded or Unhealthy

Diagnosis: Check the NATS server logs for any errors, high CPU, or memory usage. You can often find logs in /var/log/nats/nats-server.log or via journalctl -u nats-server. If you have Prometheus metrics enabled, check the nats_server_connections and nats_server_slow_consumers metrics. High connection counts or sustained slow consumer alerts can indicate the server is struggling. Fix: If the server is overloaded, consider increasing its resources (CPU, RAM) or scaling out by running multiple NATS server instances behind a load balancer. Restarting the server can sometimes resolve transient issues:

systemctl restart nats-server

Why it works: A NATS server that is maxed out on resources cannot accept new connections or process requests efficiently. Restarting can clear temporary bottlenecks, while scaling addresses fundamental capacity limits.

5. DNS Resolution Issues

Diagnosis: If your client is trying to connect using a hostname (e.g., nats://my-nats-cluster.example.com:4222) instead of an IP address, ensure that hostname resolves correctly to the NATS server’s IP address from the client’s network. Use dig or nslookup from the client machine:

dig my-nats-cluster.example.com
nslookup my-nats-cluster.example.com

Fix: Correct the DNS records for the hostname to point to the correct IP address of your NATS server. Alternatively, temporarily configure the client to use the direct IP address of the NATS server. Why it works: If the client cannot translate the hostname into an IP address, it doesn’t know where to send its connection requests.

6. TLS/SSL Configuration Errors

Diagnosis: If you’ve configured NATS to use TLS, verify that the certificates are valid, not expired, and correctly configured on both the server and the client. Check NATS server logs for TLS handshake errors. Client logs might also provide specific TLS errors. Fix: Ensure the tls configuration block in nats-server.conf has correct paths to cert.pem and key.pem. On the client side, ensure it’s configured to use nats-ssl://... and that it trusts the server’s certificate (or has the correct client certificate if mutual TLS is required). Why it works: TLS adds a layer of encryption and authentication. Misconfigurations here, such as invalid certificates or mismatched trust settings, will cause the secure connection handshake to fail, preventing any data from being exchanged.

7. NATS Server Configuration max_connections

Diagnosis: Check the max_connections setting in your NATS server configuration file. If the number of active client connections has reached this limit, new clients will be rejected. Fix: Increase the max_connections value in nats-server.conf and restart the server. For example, to allow up to 100,000 connections:

max_connections: 100000

Restart the server:

systemctl restart nats-server

Why it works: This is a hard limit set on the server to prevent it from being overwhelmed by too many simultaneous client connections. If this limit is hit, the server will refuse new connections until existing ones close.

The next error you’ll likely see is a "Maximum payload exceeded" error if you try to send messages larger than the server’s configured max_payload size.

Want structured learning?

Take the full Nats course →