Overview
Accounts provide strong multi-tenancy in NATS by creating isolated subject namespaces. Each account maintains complete isolation from other accounts unless explicitly configured to share specific streams or services.Multi-Tenancy with Accounts
Fromserver/accounts.go:50-52, an Account represents a subject namespace definition where:
- Messages published in one account cannot be seen by other accounts
- Subscriptions exist within account boundaries
- Sharing occurs only through explicit exports and imports
- Each account has independent limits and quotas
The Global Account
Fromserver/accounts.go:42-44, for backward compatibility with NATS < 2.0:
Account Structure
Fromserver/accounts.go:52-119, each account maintains:
- Sublist (
sl): Subscription matching tree - Client tracking: Maps of connected clients
- Statistics: Message and byte counters for different connection types
- Import/Export maps: Cross-account sharing configuration
- JetStream context: Optional per-account JetStream resources
- Limits: Connection, subscription, and payload limits
- Permissions: Default permissions for account users
Account Isolation and Security Boundaries
Subject Space Isolation
Each account has its own subject space:orders.created in Account A will never reach subscribers in Account B (unless explicitly exported/imported).
Resource Isolation
Fromserver/accounts.go:53-60, accounts track separate statistics:
- Messages in/out
- Bytes in/out
- Slow consumers
- Per-connection-type breakdowns
Connection Isolation
Clients connect to specific accounts:- User credentials determine account assignment
- Each client belongs to exactly one account
- Client can only publish/subscribe within their account
- Cross-account access only via explicit imports/exports
System Account
The system account is a special privileged account used for monitoring and management.System Account Role
Fromserver/server.go:192, the server maintains a reference to the system account:
- Receives server events and metrics
- Provides monitoring endpoints
- Handles server-to-server communication
- Cannot be used for regular application messaging
System Account Configuration
System Account Events
The system account receives:- Connection events:
$SYS.ACCOUNT.*.CONNECT - Disconnect events:
$SYS.ACCOUNT.*.DISCONNECT - Server stats:
$SYS.SERVER.*.STATSZ - JetStream advisories:
$JS.EVENT.ADVISORY.>
JWT-Based Authentication
NATS supports decentralized authentication using JSON Web Tokens (JWTs).JWT Architecture
Fromserver/accounts.go:36 and server/auth.go:33:
- Operator JWT: Root of trust, signs accounts
- Account JWT: Defines account properties and limits
- User JWT: Credentials for individual users
- NKeys: Ed25519 key pairs for signing and verification
JWT Components
Fromserver/accounts.go:64-66:
- Nkey: Account’s public key
- Issuer: Operator’s public key that signed this account
- claimJWT: The full JWT claim
Signing Keys
Fromserver/accounts.go:98-99:
- Multiple signing keys: Rotate keys without downtime
- Scoped keys: Limit what each signing key can authorize
- External authorization: Delegate authentication to external services
JWT Configuration
Enable JWT mode:User Credentials
Users authenticate with credential files:Account Limits and Quotas
Fromserver/accounts.go:126-133, accounts support limits:
Connection Limits
Limit the number of connections per account:server/accounts.go:77-80, the server tracks:
Subscription Limits
Limit subscriptions per account:Payload Size Limits
Limit maximum message size:server/server.go:123, the server advertises max payload:
JetStream Limits
Fromserver/accounts.go:93:
Exports and Imports
Accounts can selectively share streams or services.Stream Exports
Fromserver/accounts.go:142-150, stream imports:
orders.shipped in ORDERS appear as external.orders.shipped in FULFILLMENT.
Service Exports
Export request-reply services:auth.validate which are handled by AUTH account services.
Subject Transforms
Fromserver/accounts.go:146-147, imports support transformations:
Account Management
Static Configuration
Define accounts in configuration file:JWT-Based Management
Use JWT resolver for dynamic account management:- Create operator and signing key
- Issue account JWTs signed by operator
- Issue user JWTs signed by account
- Configure server with operator JWT and resolver
- Server fetches account JWTs from resolver as needed
- Adding accounts without server restart
- Updating account limits dynamically
- Revoking user credentials
- Account delegation
Account Revocation
Fromserver/accounts.go:84:
Account Updates
Fromserver/accounts.go:67:
- Server periodically checks for account updates
- Updated JWTs apply to new connections immediately
- Existing connections use permissions from connection time
- Can force disconnect for immediate updates
Account Best Practices
Account Design
- One account per tenant: Isolate customers completely
- System account separation: Never mix application and monitoring
- Service accounts: Dedicated accounts for shared services
- Environment separation: Separate dev/staging/prod accounts
Security
- Use JWT authentication: Centralized, revocable credentials
- Set appropriate limits: Prevent resource exhaustion
- Minimal exports: Only share what’s necessary
- Audit imports: Know what external data you consume
- Rotate signing keys: Regular key rotation for security
Performance
- Account-level optimization: Some routes can be account-dedicated
- Limit account count: Many accounts increase overhead
- Monitor account metrics: Track per-account resource usage
- Subject planning: Design subjects for efficient routing
Operations
- Use JWT resolver: Enable dynamic account management
- Monitor system account: Subscribe to server events
- Set up alerts: Track limit violations
- Document exports: Maintain export/import relationships
- Test failover: Verify account isolation during failures
Monitoring Accounts
Account Statistics
From server monitoring endpoints:/accountz: List all accounts and basic stats/connz?acc=ACCOUNT: Connections for specific account/subsz?acc=ACCOUNT: Subscriptions for specific account
System Account Monitoring
Subscribe in system account:JetStream Account Monitoring
For accounts with JetStream:Advanced Account Features
Account Trace Sampling
Fromserver/accounts.go:113-115:
- Route trace messages to specific destination
- Sample percentage of traced messages
- Useful for debugging message flow
Account Tags
Fromserver/accounts.go:106-107:
External Authorization
Fromserver/accounts.go:99:
- Server calls external service for auth decisions
- Enables integration with existing auth systems
- Supports custom authorization logic
- Can use for publish/subscribe permission checks
Next Steps
Architecture Overview
Review overall NATS architecture
Clustering
Deploy accounts across clusters
Gateways
Share accounts across regions with gateways