Overview
JetStream is NATS’ built-in persistence layer that adds streaming, message replay, and at-least-once delivery semantics to the core NATS messaging system. It transforms NATS from a pure pub/sub system into a powerful distributed streaming platform.JetStream was introduced in NATS Server 2.2.0 and provides message persistence, replay, and guaranteed delivery without requiring external dependencies.
Why Use JetStream?
JetStream extends NATS with critical enterprise features:- Message Persistence: Store messages in memory or on disk for reliable delivery
- Message Replay: Replay historical messages from any point in the stream
- At-Least-Once Delivery: Guaranteed message delivery with acknowledgments
- Exactly-Once Semantics: Message deduplication prevents duplicate processing
- Stream Replication: Cluster replication for high availability (up to 3 replicas)
- Message TTL and Limits: Automatic message expiration and stream size management
Core Concepts
Streams
Streams are message stores that capture and persist messages published to specific subjects. Each stream defines:- Subjects: Which message subjects to capture (supports wildcards)
- Storage: Memory or file-based persistence
- Limits: Maximum messages, bytes, message age
- Retention: How long to keep messages (limits, interest, work queue)
- Replication: Number of replicas in a cluster
Consumers
Consumers are views into a stream that track which messages have been delivered and acknowledged. They provide:- Durable Consumers: Named consumers that persist delivery state
- Ephemeral Consumers: Temporary consumers that exist only while connected
- Delivery Modes: Push (server sends) or Pull (client requests)
- Acknowledgment: Track message delivery and redelivery
- Filtering: Subscribe to a subset of stream subjects
Message Deduplication
JetStream provides exactly-once semantics through message deduplication using message IDs. Publishers can set a unique message ID, and JetStream will reject duplicates within a configurable window.Deduplication Example
Persistence and Replication
Storage Types
File Storage
File Storage
File storage persists messages to disk for durability across server restarts.
- Uses memory-mapped files for performance
- Configurable sync intervals (
SyncInterval,SyncAlways) - Stored in
StoreDir(default:/tmp/jetstreamor configured path) - Survives server restarts
Memory Storage
Memory Storage
Memory storage keeps messages in RAM for maximum performance.
- Faster than file storage
- Limited by
MaxMemoryconfiguration - Messages lost on server restart
- Ideal for transient data or caching
Replication
JetStream supports stream replication in clustered deployments:- Replicas: 1, 2, or 3 copies across cluster nodes
- Raft Consensus: Uses Raft protocol for consistency
- Automatic Failover: Leader election on node failure
- Maximum 3 Replicas: Limited to R3 for performance
Replicated Stream
Enabling JetStream
Command Line Flag
The simplest way to enable JetStream is with the-js flag:
Enable JetStream
Configuration File
For production deployments, use a configuration file:jetstream.conf
If
max_memory_store and max_file_store are not specified, NATS dynamically calculates limits based on available system resources (see jetstream.go:199-206).Account Limits
JetStream supports per-account resource limits:Account Limits
Configuration Reference
Key JetStream configuration options (from jetstream.go:40-52):Maximum size of memory storage type streams in bytes
Maximum size of file storage type streams in bytes
Directory where storage files are stored
How frequently to sync to disk by calling fsync
Flush to disk after every write (impacts performance)
JetStream domain for multi-cluster hierarchies
Unique tag assigned to this server instance
JetStream API
JetStream exposes a comprehensive management API over NATS subjects (see jetstream_api.go:36-200):Stream Management
API Subjects
Consumer Management
Consumer API
Monitoring
Monitor JetStream statistics via the/jsz endpoint:
JetStream Monitoring
Statistics
JetStream tracks detailed usage statistics (jetstream.go:54-63):- Memory Usage: Current memory storage used
- Store Usage: Current file storage used
- Reserved Resources: Pre-allocated account limits
- API Statistics: Total requests, errors, inflight
- Accounts: Number of JetStream-enabled accounts
- HA Assets: Replicated streams and consumers
Best Practices
Use File Storage
Use file storage for streams that must survive restarts. Reserve memory storage for caching and transient data.
Configure Limits
Always set appropriate limits (
max_msgs, max_bytes, max_age) to prevent unbounded growth.Enable Replication
Use R3 replication for critical streams in production clusters.
Message Deduplication
Use message IDs for exactly-once semantics with configurable duplicate windows.
Next Steps
Configuration
Configure JetStream for your deployment
Monitoring
Monitor JetStream metrics and health
Architecture
Understand NATS Server architecture
MQTT
MQTT implementation uses JetStream