Introduction
NATS Server is a high-performance messaging system built on a simple yet powerful architecture. At its core, NATS provides subject-based publish-subscribe messaging with support for request-reply patterns, all designed for horizontal scalability and operational simplicity.Architectural Layers
NATS Server operates with two distinct architectural layers:Core NATS
Core NATS provides the foundational messaging layer with:- At-most-once delivery: Messages are delivered to connected subscribers or discarded
- Pure pub-sub: Fire-and-forget messaging with no persistence
- Subject-based routing: Messages are routed based on hierarchical subject names
- Minimal overhead: Designed for high throughput and low latency
JetStream Layer
JetStream adds persistence and streaming capabilities on top of Core NATS:- At-least-once delivery: Messages are persisted and can be replayed
- Stream storage: Messages are stored in streams for replay and recovery
- Consumer semantics: Multiple consumer types for different delivery patterns
- Horizontal scalability: Streams can be replicated across cluster members
Subject-Based Messaging Model
NATS uses a hierarchical subject naming system that forms the foundation of message routing. Subjects are UTF-8 strings composed of tokens separated by dots (.).
Subject Format
- Single token wildcard (
*): Matches exactly one tokenorders.*.laptops.shippedmatchesorders.electronics.laptops.shipped
- Multi-token wildcard (
>): Matches one or more trailing tokensorders.>matches all orders subjects
Subject Matching with Sublist
The server uses an optimized data structure called a Sublist to efficiently match published messages to subscriptions. The Sublist implements a trie-based algorithm that provides O(1) average-case lookups with support for wildcard matching. Key characteristics fromserver/sublist.go:26-29:
- Routes messages from publishers to interested subscribers
- Handles wildcard subscriptions efficiently
- Maintains a cache for frequently matched subjects
- Supports notification channels for subscription changes
Client Connection Architecture
NATS Server supports multiple connection types, each serving a different purpose:Connection Types
Fromserver/client.go:45-60:
- CLIENT: End-user applications connecting to NATS
- ROUTER: Cluster connections between NATS servers
- GATEWAY: Connections linking separate clusters into super-clusters
- LEAF: Leaf node connections for hub-and-spoke topologies
- SYSTEM: Internal system clients for server operations
- JETSTREAM: Internal clients for JetStream operations
Protocol Support
The server supports multiple client protocols:- NATS Protocol: The native text-based protocol
- WebSocket: For browser-based and firewall-friendly connections
- MQTT: MQTT 3.1.1 compatibility for IoT devices
Connection Lifecycle
Client connections follow a specific lifecycle fromserver/client.go:140-157:
- Initial connection: TCP connection established
- INFO exchange: Server sends INFO, optionally receives CONNECT
- Authentication: Credentials validated
- Active messaging: Client can publish and subscribe
- Ping/Pong: Keepalive mechanism maintains connection health
- Graceful shutdown: DRAIN protocol for clean disconnection
Scalability and Performance Characteristics
Horizontal Scalability
NATS achieves horizontal scalability through several mechanisms:- Clustering: Multiple servers form a full mesh for high availability
- Gateways: Connect clusters across regions without full mesh overhead
- Leaf nodes: Extend reach with hub-and-spoke connections
- Account isolation: Each account maintains separate subscription space
Performance Optimizations
Key performance features:- Zero-copy message routing: Messages flow through the server without unnecessary copying
- Lock-free data structures: Atomic operations minimize contention
- Adaptive buffering: Dynamic buffer sizing from
server/client.go:110-113 - Compression: Optional S2 compression for route and gateway connections
- Optimistic routing: Gateway interest-based routing reduces unnecessary traffic
Memory and Resource Management
The server implements several strategies for efficient resource utilization:- Buffer pooling: Reusable buffers reduce GC pressure
- Subscription caching: Hot paths cached for faster lookup
- Connection limits: Per-account connection and subscription limits
- Slow consumer detection: Automatic handling of backed-up consumers
Message Flow
When a message is published:- Subject parsing: The server tokenizes the subject
- Subscription matching: Sublist efficiently finds matching subscriptions
- Account isolation: Only subscriptions within the same account receive the message
- Queue distribution: For queue subscribers, one member receives the message
- Delivery: Message written to matching client connections
- Import/Export: Messages cross account boundaries only via explicit exports/imports
Multi-Tenancy with Accounts
NATS implements strong multi-tenancy through accounts, which create isolated subject spaces. Each account:- Maintains its own subscription tree
- Cannot see messages from other accounts by default
- Can selectively export streams or services to other accounts
- Can import streams or services from other accounts
Server Topologies
NATS supports various deployment topologies:Single Server
Simplest deployment for development and small-scale production.Cluster
Full mesh of servers providing high availability and load distribution. See Clustering.Super-Cluster
Multiple clusters connected via gateways for geo-distribution. See Gateways.Hub and Spoke
Leaf nodes connecting to a central hub or cluster. See Gateways.Next Steps
Clustering
Learn how NATS servers form clusters for high availability
Gateways
Explore super-clusters and multi-region connectivity
Accounts
Understand multi-tenancy and account isolation