NATS
High-performance messaging system.
Overview
NATS is a lightweight, high-performance messaging system designed for cloud-native applications, microservices, and IoT environments. Originally developed by Derek Collison at Apcera, NATS has evolved into one of the fastest and simplest messaging platforms available, capable of handling millions of messages per second with minimal resource overhead. Unlike traditional message brokers that rely on complex routing mechanisms, NATS uses a simple subject-based pub/sub model that makes it incredibly fast and easy to understand.
This Docker stack deploys a single NATS server with JetStream persistence enabled, providing both traditional fire-and-forget messaging and durable message streaming capabilities. The configuration includes the built-in monitoring interface, allowing you to observe message flow, connection statistics, and system health in real-time. JetStream adds persistent messaging, key-value stores, and object storage functionality on top of NATS' core pub/sub system, making it suitable for both high-throughput real-time scenarios and applications requiring message durability.
This setup is ideal for developers building microservices architectures, IoT systems, or real-time applications who need reliable messaging without the complexity of heavier brokers like RabbitMQ or Kafka. NATS excels in scenarios requiring low latency, high throughput, and simple deployment, making it perfect for edge computing, gaming backends, financial trading systems, and distributed application coordination.
Key Features
- Subject-based publish/subscribe messaging with wildcard support
- JetStream persistent messaging with configurable retention policies
- Request/reply pattern for synchronous communication between services
- Queue groups for load balancing messages across multiple consumers
- Built-in key-value store for distributed configuration and state management
- Real-time monitoring dashboard accessible via web interface
- Extremely low memory footprint starting at 64MB RAM usage
- Native clustering support for high availability deployments
Common Use Cases
- 1Microservices communication in containerized applications
- 2IoT device messaging and telemetry data collection
- 3Real-time gaming backends for player state synchronization
- 4Financial trading systems requiring ultra-low latency messaging
- 5Edge computing scenarios with resource-constrained environments
- 6Event-driven architectures for distributed application coordination
- 7Chat applications and real-time collaboration tools
Prerequisites
- Minimum 256MB RAM available for optimal NATS performance
- Port 4222 available for NATS client connections
- Port 8222 available for monitoring dashboard access
- Basic understanding of publish/subscribe messaging patterns
- NATS CLI client installed for testing and administration
- Docker Compose 3.8+ for volume management support
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 nats: 3 image: nats:latest4 container_name: nats5 restart: unless-stopped6 command: "-js -m 8222"7 ports: 8 - "4222:4222"9 - "8222:8222"10 volumes: 11 - nats_data:/data1213volumes: 14 nats_data: .env Template
.env
1# No additional config neededUsage Notes
- 1Docs: https://docs.nats.io/
- 2Client connections on port 4222, monitoring at http://localhost:8222
- 3JetStream enabled (-js flag) for persistent messaging
- 4Pub/sub: nats pub subject 'message', nats sub subject
- 5Key-value store: nats kv add <bucket>, nats kv put <bucket> <key> <value>
- 6Extremely lightweight - single binary, minimal resource usage
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 nats:5 image: nats:latest6 container_name: nats7 restart: unless-stopped8 command: "-js -m 8222"9 ports:10 - "4222:4222"11 - "8222:8222"12 volumes:13 - nats_data:/data1415volumes:16 nats_data:17EOF1819# 2. Create the .env file20cat > .env << 'EOF'21# No additional config needed22EOF2324# 3. Start the services25docker compose up -d2627# 4. View logs28docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/nats/run | bashTroubleshooting
- Connection refused on port 4222: Ensure the NATS container is fully started and port mapping is correct in docker-compose.yml
- JetStream not available error: Verify the -js flag is present in the command configuration to enable JetStream functionality
- Monitoring dashboard not accessible: Check that port 8222 is properly mapped and the -m 8222 flag is included in the startup command
- Messages not persisting after restart: Confirm the nats_data volume is properly mounted to /data directory in the container
- High memory usage with JetStream: Configure stream limits and retention policies to prevent unbounded message accumulation
- Slow message delivery: Check subject naming patterns and avoid excessive wildcard subscriptions that can impact performance
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download