Skip to content

NATS leaf nodes — connecting edge clusters to a central hub

A walkthrough of NATS leaf-node configuration — edge-to-hub routing, auth boundaries, JetStream domain mapping, and the pitfalls of accidental subject leaks.

A retail customer wanted to land NATS at every store — point-of-sale, kitchen display, back-office printer — and aggregate the data into a central cluster in their primary region. Running a full NATS cluster at every store is overkill (each store has two devices and one printer); running plain NATS clients with a public WAN connection to the hub leaks every store’s subjects to every other client. Leaf nodes are the right tool: a single edge NATS server per store, federated upstream to the hub with strict subject filtering. This article is the install and the config knobs that matter in production.

How to verify

Confirm a hub is reachable, the version supports leaves, and the auth posture you intend:

# Hub-side: version (leaf nodes need 2.2+, JetStream domains need 2.7+)
nats server info --server nats://hub.example.com:4222 | grep -E 'version|leaf'

# Edge-side: confirm reachability to the leaf-node port (separate from client port)
nc -vz hub.example.com 7422

# Leaf accounts / users on the hub
nats account ls --server nats://admin:PASS@hub.example.com:4222

If the leaf-node port (default 7422) isn’t open from the edge to the hub, no leaf will ever connect. The client port (4222) is irrelevant for leaf-to-hub traffic.

What’s happening

A NATS leaf node is a full NATS server that connects to a remote NATS server (or cluster) as a leaf — bridging subjects between local clients and the remote hub. Unlike a regular client connection, a leaf carries an entire subject namespace, scoped by account and by the leaf-node’s own permissions.

The shape:

  • Edge clients connect to the local leaf node like any NATS client (port 4222, local credentials).
  • The leaf node holds an outbound connection to the hub’s leaf-node port (default 7422), authenticating as a leaf-specific user on the hub.
  • The hub sees the leaf as a single connection in a dedicated account; subjects flow in both directions based on what the leaf has permission to publish/subscribe.

What this buys you:

  • Local-first messaging. Edge clients can talk to each other through the local leaf with zero WAN round-trip. The kitchen display and the point-of-sale don’t need to wait for a 200 ms hop to the hub.
  • Survivable disconnects. If the WAN dies, the leaf keeps serving local clients. When the WAN comes back, the leaf reconnects and resumes subject flow. JetStream messages mirrored to the hub catch up.
  • Strict auth boundaries. The leaf’s hub credentials scope what subjects can leak upward — store.42.> can publish to the hub, but store.42.local.> stays internal.
  • JetStream domain isolation. Each leaf can run its own JetStream domain (e.g. store-42), and the hub runs hub. Streams in the leaf’s domain don’t auto-replicate to the hub unless you explicitly source or mirror them.

What this is not:

  • A NATS cluster. Cluster members must be co-located (sub-millisecond) and they exchange every subject indiscriminately within an account. Leaves are intentionally loose.
  • A gateway. Gateways are for connecting two full clusters as peers; leaves are for a small edge talking up to a hub.

The procedure

  1. Set up the hub to accept leaves. In the hub’s nats-server.conf:

    leafnodes {
      port: 7422
      # Optional TLS for the leaf-node listener
      tls {
        cert_file: /etc/nats/server-cert.pem
        key_file:  /etc/nats/server-key.pem
        ca_file:   /etc/nats/ca.pem
      }
    }
    
    accounts {
      EDGE_STORES: {
        users: [
          { user: "leaf-store-42", password: "$2a$11$..." }
        ]
        exports: [
          { stream: "broadcast.>" }
        ]
      }
    }

    The leaf-node port is separate from the client port. Don’t share. The dedicated account (EDGE_STORES) is what isolates leaf traffic from regular client traffic on the hub.

  2. Configure the leaf at the edge. A minimal nats-leaf.conf:

    server_name: leaf-store-42
    port: 4222
    
    leafnodes {
      remotes: [
        {
          url: "tls://hub.example.com:7422"
          credentials: "/etc/nats/leaf-store-42.creds"
          account: "EDGE_STORES"
          # Limit what local subjects flow up; reject everything else
          deny_export: ["store.42.local.>", "_INBOX.>"]
          deny_import: ["broadcast.confidential.>"]
        }
      ]
    }

    The deny_export/deny_import lists are the auth boundary. store.42.local.> stays on the edge; _INBOX.> (NATS request-reply scratch subjects) shouldn’t leak across the WAN; broadcast.confidential.> is filtered even if the hub publishes it.

  3. Start the leaf and verify the connection. Watch the hub’s logs for the leaf-connect event:

    nats-server -c /etc/nats/nats-leaf.conf
    
    # On the hub
    nats server report leafz --server nats://admin:PASS@hub.example.com:4222
    # name, account, in_msgs, out_msgs, rtt

    Leaves show up with their server_name and the account they joined. RTT is the WAN latency; if it’s spiking, your hub will see subscription latency match.

  4. Map JetStream domains. If both ends run JetStream, give each a distinct domain in the JetStream block:

    # On the hub
    jetstream {
      domain: hub
      store_dir: /var/lib/nats/jetstream
    }
    
    # On the leaf
    jetstream {
      domain: store-42
      store_dir: /var/lib/nats/jetstream
    }

    Now a stream ORDERS created on the leaf is store-42.ORDERS from the hub’s perspective. Mirror it explicitly when you want hub durability:

    nats stream add ORDERS-MIRROR \
      --server nats://admin:PASS@hub.example.com:4222 \
      --mirror ORDERS \
      --mirror-domain store-42
  5. Wire monitoring. The leaf exposes /varz and /leafz on its monitoring port (default 8222):

    curl -s http://leaf-store-42:8222/leafz | jq '.leafs[] | {name, rtt, in_msgs, out_msgs}'

    Alert on RTT spikes, on in_msgs/out_msgs flatlining for more than 5 minutes (probably the connection silently dropped), and on TLS-handshake errors in the leaf’s logs.

Operational notes

  • Permissions are AND, not OR, across leaf and account. A subject must be allowed by both the leaf’s deny_export/deny_import and the account-level imports/exports on the hub. Two layers of filter; both have to pass.
  • A leaf carries all subjects in its account by default. That’s the whole point. If you don’t write deny lists, every local subject ends up on the hub bus, visible to anyone with the EDGE_STORES account.
  • JetStream API calls are not subjects you want to leak. $JS.API.> is implicit in JetStream; the leaf should deny it unless you genuinely want hub clients to manage leaf streams from the hub.
  • TLS for leaves is non-optional over public WAN. The leaf-node connection is long-lived and high-value; mTLS with rotating client certs is the production posture.
  • Reconnect behavior is automatic but jittered. A leaf with a flapping WAN can spend minutes in backoff. Tune reconnect_buf_size and connect_retries if your WAN is genuinely flaky and you need faster recovery.
  • Plain client connections are still possible against the leaf’s client port. Don’t accidentally expose 4222 to the public internet — that’s a different auth boundary than the leaf-node connection.

Stack Harbor stands up NATS hub-and-leaf topologies for retail, logistics, and field-deployed customers — leaves at each site with local JetStream durability, mirrored streams in a central hub, and an auth posture that doesn’t leak per-site subjects across the bus. This is the kind of multi-site messaging design we cover under clustered environments.