one nats cluster serves one region well. then a second place needs the same subjects: another region, a factory, a customer’s own kubernetes cluster. the first idea is usually to add the new servers to the existing cluster’s routes. a cluster is a full mesh, where every server holds a route to every other server, and a replicated stream waits for a majority of its replicas on every publish. stretched over two regions, that majority is one inter-region round trip away for every message, and a flapping link between the regions becomes a flapping cluster.
nats has two connection types built for this. a gateway joins whole clusters into a super-cluster. a leaf node is a server, or a small cluster, that dials out to another nats system and bridges one account into it. both carry subjects between clusters, and they differ in most other ways.
this builds on messaging with nats for subjects, queue groups and streams, and the store-and-forward section of patterns with nats shows a leaf node doing one specific job.
the short version
| gateways | leaf nodes | |
|---|---|---|
| shape | peer clusters, each connected to the others | spokes that dial a hub |
| who dials | every cluster dials every other cluster | the leaf dials, the hub only listens |
| default port | 7222 | 7422 |
| auth | one set of accounts across all clusters | the leaf logs in to the hub as a user of one account |
| jetstream | one system and one meta group across all clusters | a system per domain, each with its own meta group |
| link down | each side keeps its local traffic, stream changes need a meta majority | the leaf’s own streams keep working in full |
| fits | a few regions that are run by one team | edge sites, customer clusters, anything behind nat |
super-cluster: peer clusters dial each other leaf nodes: the spokes dial the hub
┌─ east ──────┐ gateway ┌─ west ──────┐ ┌─ hub ────────────────────────┐
│ e1 e2 e3 │ ◀─────────▶ │ w1 w2 w3 │ │ h1 h2 h3 │
└─────────────┘ └─────────────┘ └──────────────────────────────┘
▲ ▲ ▲ ▲ ▲
│ ┌─ south ─────┐ │ │ │ │
└─────▶ │ s1 s2 s3 │ ◀───┘ shop-041 shop-042 shop-043
└─────────────┘gateways
a gateway is a second listener on every server of a cluster, next to the client port and the route port. each cluster keeps its own cluster block and its own routes, and adds a gateway block that names itself and points at the others:
# east-1.conf
cluster {
name: east
port: 6222
routes: ["nats://east-1:6222", "nats://east-2:6222", "nats://east-3:6222"]
}
gateway {
name: east
port: 7222
gateways: [
{ name: west, urls: ["nats://west-1.example.com:7222", "nats://west-2.example.com:7222"] }
]
}the gateway name has to match the cluster name, and a server where they differ does not start: cluster name conflicts between cluster and gateway definitions. the name on each entry in gateways is the remote cluster’s name.
each server opens one gateway connection to each other cluster, to one of its servers, and not one to every remote server. gateways gossip the way routes do. point east at west and west at south, and east finds south by itself. the listed urls are seeds.
both sides open connections. east dials west and west dials east, so every cluster needs a gateway port that the other clusters can reach.
leaf nodes
a leaf node is a nats server with a remotes list. it dials each remote and keeps the connection up. the far side, the hub, only needs a listener:
# hub
leafnodes {
port: 7422
}# shop-042.conf
leafnodes {
remotes: [
{
urls: ["nats-leaf://hub-1.example.com:7422", "nats-leaf://hub-2.example.com:7422"]
credentials: "/etc/nats/shop-042.creds"
account: SHOP
}
]
}credentials is what the leaf presents to the hub, and the hub attaches the connection to the account those credentials belong to. account is the local account on the leaf whose traffic is bridged. listing several hub urls gives the leaf somewhere to reconnect when one hub server is down.
the connection is made from the leaf’s side only, so the leaf can sit behind nat or a firewall that allows nothing in. the url scheme can also be ws, for a hub that is only reachable through an http ingress.
remotes is a list. a leaf can dial several nats systems and bridge a different local account into each. a leaf can also have a leafnodes listener of its own and act as the hub for leaves further out, so the topology can be a tree.
who dials whom
a gateway needs each cluster to accept connections from the others. that is fine between regions one team runs, where a peering link or a load balancer per cluster is a normal request.
a shop, a factory or a customer’s cluster usually cannot accept anything, and nobody wants to open an inbound port in a hundred shops. a leaf node needs one outbound connection to port 7422, or to 443 over websocket, and the remotes block always goes on the side that only has egress.
interest and queue groups
neither link forwards everything. a gateway sends a message to another cluster only when that cluster has a subscriber for the subject, and a leaf connection carries only subjects that have interest on the other side. a subject nobody subscribes to in west costs east nothing.
a gateway starts out optimistic. it forwards messages for an account and the other side answers with “no interest” for the subjects nobody wants. when too many of those come back, the gateway switches that account to interest-only mode, where the remote cluster sends its full subscription list and keeps it up to date.
queue groups across gateways prefer the local cluster. a message published in east goes to an east member of the queue group when one exists. it crosses a gateway only when no local member is left, and then to one remote cluster. a service deployed in every region is therefore answered in the caller’s region and fails over to another region without any client change.
where jetstream lives
core subjects behave much the same over both links. jetstream does not.
in a super-cluster there is one jetstream system. every jetstream-enabled server in every cluster is a peer in a single meta group, the raft group that creates, moves and deletes streams and consumers. a stream’s replicas always sit inside one cluster, and replication does not stretch across a gateway. the stream is pinned to a cluster with placement, and a second cluster gets the data through a mirror:
nats stream add ORDERS --subjects "orders.>" --replicas 3 --cluster east
nats stream add ORDERS-WEST --mirror ORDERS --replicas 3 --cluster westa client connected in west can still publish to ORDERS and consume from it. the requests cross the gateway to wherever the stream lives.
a leaf node with a domain is a separate jetstream system:
# shop-042.conf
jetstream {
domain: shop-042
store_dir: /var/lib/nats
}each domain runs its own meta group, with its own peers and its own quorum. the hub reaches the leaf’s api under $JS.shop-042.API in place of $JS.API, which is what the cli’s --js-domain flag and the Domain field on a source or mirror set:
nats --js-domain shop-042 stream ls gateways: one jetstream system leaf node with a domain: two systems
┌─ east ─────────┐ ┌─ west ─────────┐ ┌─ shop-042 ─────┐ ┌─ hub ──────────┐
│ stream ORDERS │ │ ORDERS-WEST │ │ stream SALES │ │ stream SALES │
│ 3 replicas │ ────▶ │ mirrors ORDERS │ │ 1 replica │ ────▶ │ sources SALES │
└────────────────┘ └────────────────┘ └────────────────┘ │ from shop-042 │
└─────── one meta group, six peers ───────┘ its own meta group └────────────────┘
its own meta groupin both drawings the copy on the right is asynchronous. a mirror or a source is eventually consistent and its lag is in nats stream info.
accounts
a super-cluster is one system for auth as well. every cluster has to know the same accounts, from the same operator and resolver or from the same accounts block, since a message crossing a gateway stays in its account.
a leaf node keeps its own auth. clients in the shop log in to the shop’s server under the shop’s config, and the hub only sees the one user the leaf connects as. the shop’s users, and how they authenticate, never reach the hub.
the bridge is the whole account, though. every subject in the bound account can cross, in both directions, as soon as there is interest for it. a leaf that should only see its own subjects gets its own account on the hub, or a hub user whose permissions are limited to them.
when the link drops
with gateways, core messaging inside each cluster carries on, and a stream keeps taking publishes as long as its own cluster has a majority of its replicas. the jetstream api depends on the meta group, which spans all clusters, and nothing can be created, moved or restored while it has no quorum. the side of the split that holds a minority of the meta peers cannot create a stream or a consumer until the link is back.
with a leaf node in its own domain, the leaf loses nothing locally. its meta group is its own, so streams and consumers can still be created, and local publishers and consumers do not notice the outage. the subjects bridged to the hub stop flowing, and a hub stream that sources from the leaf stops advancing. when the link returns, the source resumes from the last sequence it copied.
this is why a disaster-recovery copy of a stream is normally a mirror in its own domain or in an independent cluster, and not a mirror in a second cluster of the same super-cluster. the copy is only useful if its side can still run the api alone.
using both
a common shape is a super-cluster of a few regions as the hub, with leaf nodes for everything that cannot be a peer:
shop-041 ─┐ ┌─ plant-ams
shop-042 ─┼─ leaf ─▶ ┌─ east ───┐ ◀── gateway ──▶ ┌─ west ───┐ ◀─ leaf ─┼─ plant-rtm
shop-043 ─┘ └──────────┘ └──────────┘ └─ plant-eina subscriber in west receives what a till in shop-042 publishes: the leaf carries it into east, and the gateway carries it on because west has interest.
choosing
- the clusters are run by one team, can reach each other, and should behave as one system with one set of accounts: gateways.
- one side cannot accept inbound connections: leaf node.
- the remote site has to keep its streams fully working while disconnected: leaf node with its own domain.
- the remote site belongs to someone else, or has its own users: leaf node.
- clients should be served by the nearest region and fail over to the next: gateways, with the service running in each region.
what to watch out for
two clusters are not enough for jetstream over gateways. two clusters of three servers make a meta group of six, and a majority of six is four. when the link drops each side has three, so neither can create or change a stream or a consumer. the same happens when one whole cluster is lost. a third cluster gives the meta group a majority that survives losing any one of them.
a misspelled gateway name can pass a smoke test. if east lists the remote cluster as wset, its outbound connection is refused with Failing connection to gateway "wset", remote gateway name is "west". the super-cluster still forms through the connection west opens in the other direction, so a publish across regions works while east logs that error on every retry.
geo-affinity needs workers in every region. a queue group that only has members in east sends every request from west across the gateway. the preference for local members saves the round trip only where local members exist.
a leaf with jetstream and no domain extends the hub’s jetstream. when the leaf shares the hub’s system account and has no domain, it joins the hub’s jetstream system. a stream created on the leaf may then be placed on a hub server, and the leaf has no local store to fall back on when the link drops.
a leaf bridges the account, and everything in it. two shops bound to the same hub account can subscribe to each other’s subjects through the hub. separate hub accounts, or permissions on each leaf’s hub user, keep them apart.
a loop shows up as a leaf that reconnects every 30 seconds. a leaf that reaches the same system by two paths for one account would forward its own messages back to itself. the servers detect this with a subscription on a $LDS. subject, close the connection with Loop Detected, and wait 30 seconds before trying again.
a mirror is behind by some amount. failing over to ORDERS-WEST means losing whatever ORDERS accepted and the mirror had not yet copied. the Lag field in nats stream info ORDERS-WEST is that number.
references
[1] nats documentation. “super-clusters.”
docs.nats.io/learn/topologies/super-clusters
[2] nats documentation. “leaf nodes.”
docs.nats.io/learn/topologies/leaf-nodes
[3] nats documentation. “gateway configuration reference.”
docs.nats.io/reference/config/gateway
[4] nats documentation. “leaf node remotes reference.”
docs.nats.io/reference/config/leafnodes/remotes
[5] nats documentation. “gateway protocol.”
docs.nats.io/reference/protocols/gateway
[6] nats documentation. “leaf node protocol.”
docs.nats.io/reference/protocols/leafnode
[7] nats documentation. “jetstream in a cluster.”
docs.nats.io/learn/topologies/jetstream-in-a-cluster
[8] nats documentation. “mirrors and sources.”
docs.nats.io/learn/jetstream/mirrors-and-sources
[9] nats documentation. “disaster recovery.”
docs.nats.io/running-a-nats-service/nats_admin/jetstream_admin/disaster_recovery