Overview
Gateways and leaf nodes solve different connectivity challenges in NATS deployments:- Gateways: Connect multiple NATS clusters into a super-cluster for geo-distribution
- Leaf nodes: Extend NATS clusters with hub-and-spoke connections for edge computing
Gateways
Gateways connect separate NATS clusters across regions or data centers without the overhead of full mesh routes.Gateway Concept
A gateway connection:- Links two named clusters
- Exchanges subscription interest between clusters
- Forwards messages only when interest exists
- Operates at the cluster level, not server level
- Optimizes for wide-area network efficiency
Super-Cluster Architecture
Fromserver/gateway.go:35-59, gateways implement:
- Interest-based routing: Messages only cross gateways when subscribers exist
- Optimistic mode: Initially assumes interest until told otherwise
- Interest-only mode: Explicit interest tracking for efficiency
- Transitioning mode: Automatic mode switching based on traffic patterns
Gateway Interest Modes
Fromserver/gateway.go:93-111:
- Cluster sends messages across gateway unless explicitly told no interest
- Efficient when most subjects have remote subscribers
- Remote sends “no interest” messages to suppress traffic
- Cluster only sends messages with explicit remote interest
- Efficient when few subjects have remote subscribers
- Requires full interest propagation
- Triggered after too many “no interest” messages (default: 1000)
- Cluster switches to interest-only mode
- Prevents excessive unnecessary traffic
Gateway Configuration
Basic gateway setup:Gateway TLS
Secure gateway connections:Gateway Protocol Details
Fromserver/gateway.go:87-91, gateway commands:
gatewayCmdGossip: Share gateway topologygatewayCmdAllSubsStart: Begin full interest syncgatewayCmdAllSubsComplete: Finish interest sync
- Gateway name and URLs
- Cluster information
- Interest mode settings
- Protocol capabilities
Reply Subject Handling
Gateways use special reply subject prefixes fromserver/gateway.go:43-54:
- Old format:
$GR.<hash>.<subject> - New format:
_GR_.<cluster_hash>.<server_hash>.<subject>
Use Cases for Gateways
Geographic Distribution
Deploy clusters in multiple regions:- Each region has a local cluster (low latency)
- Gateways connect regions (high latency tolerant)
- Clients connect to local cluster
- Messages reach subscribers in any region
Failover Between Regions
Gateways enable cross-region failover:- Client connects to local cluster
- Local cluster fails
- Client reconnects to remote cluster via gateway
- Application continues without message loss
Data Sovereignty
Control message flow between regions:- Use accounts to isolate sensitive data
- Configure imports/exports to control cross-region flow
- Messages only cross gateways when explicitly allowed
Cost Optimization
Reduce cross-region traffic costs:- Interest-only mode minimizes unnecessary traffic
- Messages only traverse expensive WAN links when needed
- Local clusters handle regional traffic locally
Leaf Nodes
Leaf nodes extend NATS clusters with lightweight, hub-and-spoke connections.Leaf Node Concept
A leaf node connection:- Connects a server (spoke) to a cluster (hub)
- Appears as a single client to the hub
- Supports bidirectional messaging
- Automatically reconnects to hub cluster
- Can operate in isolated mode
Hub and Spoke vs Spoke
Fromserver/leafnode.go:121-133:
- Hub: The server/cluster accepting leaf connections
- Spoke: The server initiating the leaf connection
- Solicited: Spoke-initiated outbound connection
- Unsolicited: Hub-accepted inbound connection
Leaf Node Architecture
Leaf nodes support several patterns: Edge Computing:Leaf Node Configuration
Hub Configuration
Accept leaf node connections:Spoke Configuration
Connect to hub:Leaf Node Features
Account Binding
Leaf nodes bind local accounts to remote accounts:EDGE_USERS appear in the hub account.
TLS for Leaf Nodes
Secure leaf connections:WebSocket Leaf Nodes
Leaf nodes can connect via WebSocket:server/leafnode.go:64, the path /leafnode identifies WebSocket connections as leaf nodes.
Compression
Fromserver/leafnode.go:101, leaf nodes support S2 compression:
Leaf Node Isolation
Fromserver/leafnode.go:86, isolated leaf nodes:
- Don’t receive subscriptions from other leaf nodes
- Only see traffic from hub cluster
- Prevents east-west traffic between leaf nodes
- Useful for security and performance
Loop Detection
Fromserver/leafnode.go:49-60, leaf nodes prevent loops:
- Loop detection subject:
$LDS.<random> - If leaf receives its own loop detection message, loop detected
- Connection closed with 30-second reconnect delay
- Prevents cluster connecting to itself via leaf
Leaf Node Reconnection
Leaf nodes automatically reconnect with backoff:- Multiple hub URLs supported for failover
- Automatic reconnection on disconnect
- Subscription state restored on reconnect
- From
server/leafnode.go:50-57, special delays for error conditions:- Loop detected: 30 seconds
- Permission violation: 30 seconds
- Same cluster name: 30 seconds
Multi-Region Connectivity Patterns
Hybrid: Clusters + Gateways + Leaf Nodes
Combine all three for complex topologies:- Regional clusters for low latency
- Gateways for cross-region
- Leaf nodes for edge locations
Active-Active Multi-Region
Hub with Regional Spokes
Configuration Examples from Source
Gateway with Multiple Remotes
From source code patterns:Leaf Node with Credentials
Performance Considerations
Gateway Performance
- Interest propagation overhead: Interest-only mode trades setup cost for reduced traffic
- RTT sensitivity: High RTT impacts mode transition decisions
- Connection pooling: From
server/gateway.go:140, gateways maintain ordered connections - Ping interval: Max 15 seconds from
server/gateway.go:58
Leaf Node Performance
- Hub cluster impact: Each leaf appears as one client
- Subject interest: All leaf subscriptions propagated to hub
- Message amplification: Hub messages reach all leaf subscribers
- Isolation benefit: Isolated leafs reduce cross-leaf traffic
Best Practices
Gateway Deployment
- Name gateways consistently: Use geographic or functional names
- Configure all connections: Each cluster should list all other clusters
- Use TLS: Encrypt cross-region traffic
- Monitor interest modes: Track mode transitions
- Set reject_unknown: Prevent unauthorized gateway connections
Leaf Node Deployment
- Use credentials: JWT-based authentication preferred
- Multiple hub URLs: Configure failover URLs
- Account mapping: Plan account topology carefully
- Consider isolation: Use for edge security
- Compression for WAN: Enable for bandwidth-limited connections
Capacity Planning
- Gateway bandwidth: Monitor cross-region traffic
- Hub capacity: Each leaf consumes hub resources
- Interest churn: Frequent subscriptions trigger interest updates
- Subject cardinality: Many unique subjects increase interest state
Troubleshooting
Gateway Issues
Check/gatewayz endpoint:
- Connection state per gateway
- Interest mode per account
- Message and byte counts
- Number of “no interest” suppressions
Leaf Node Issues
Check/leafz endpoint:
- Leaf connection status
- Account bindings
- Subscription counts
- RTT and compression stats
- Loop detection: Check cluster names don’t match
- Permission errors: Verify account permissions
- Connection failures: Check network and authentication
Next Steps
Clustering
Understand cluster fundamentals
Accounts
Configure multi-tenancy across gateways and leaf nodes