# nats-io/nats-server architecture

> Architecture diagram of the GitHub repository nats-io/nats-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nats-io/nats-server
- Repository: https://github.com/nats-io/nats-server

## Overview

NATS Server is a communications server for services and devices. Its principal workflow accepts client connections, authenticates them, processes subscriptions and published messages, and distributes matching messages; JetStream adds persistent streaming and storage. The tree also shows clustering, monitoring, configuration, and security subsystems. The excerpts establish startup, subscription insertion, and account setup, but leave much of runtime dispatch and subsystem wiring unsampled, so uncertain relationships are shown as dashed and unverified.

Main entry points: [`server/server.go`](https://github.com/nats-io/nats-server/blob/main/server/server.go) (Server runtime), [`server/client.go`](https://github.com/nats-io/nats-server/blob/main/server/client.go) (Client protocol), [`main.go`](https://github.com/nats-io/nats-server/blob/main/main.go) (Server CLI), [`server/stream.go`](https://github.com/nats-io/nats-server/blob/main/server/stream.go) (Stream management)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_startup["Startup and configuration"]
  node_cli["Server CLI<br/>[main.go]"]
  node_config["Config parsing<br/>[parse.go]"]
  node_server["Server runtime<br/>[server.go]"]
end

subgraph group_messaging["Client messaging"]
  node_client["Client protocol<br/>[client.go]"]
  node_parser["Protocol parser<br/>[parser.go]"]
  node_auth["Authentication<br/>[auth.go]"]
  node_subscriptions["Subscription matching<br/>[sublist.go]"]
  node_subjecttree["Subject tree<br/>[stree.go]"]
  node_accounts["Accounts<br/>[accounts.go]"]
end

subgraph group_persistence["JetStream persistence"]
  node_jetstream["JetStream<br/>[jetstream.go]"]
  node_stream["Stream management<br/>[stream.go]"]
  node_store[("Message storage<br/>[store.go]")]
  node_backup["Stream backup<br/>[stream_backup.go]"]
end

subgraph group_distributed["Distributed operation"]
  node_routes["Route connections<br/>[route.go]"]
  node_gateways["Gateway connections<br/>[gateway.go]"]
  node_leafnodes["Leaf connections<br/>[leafnode.go]"]
end

subgraph group_operations["Security and operations"]
  node_authz["Authorization<br/>[auth_callout.go]"]
  node_tls["Certificate handling<br/>[certidp.go]"]
  node_monitor["Monitoring<br/>[monitor.go]"]
  node_logger["Logging<br/>[log.go]"]
end

node_operator(("Operator"))
node_external(("NATS clients"))

node_operator -.->|"configures"| node_cli
node_cli -->|"starts"| node_server
node_cli -.->|"loads config"| node_config
node_server -->|"uses logging"| node_logger
node_external -.->|"connects"| node_client
node_client -.->|"authenticates"| node_auth
node_client -.->|"processes protocol"| node_parser
node_client -->|"updates account"| node_accounts
node_client -->|"inserts subscriptions"| node_subscriptions
node_client -.->|"matches subjects"| node_subjecttree
node_server -->|"registers accounts"| node_accounts
node_server -.->|"initializes"| node_jetstream
node_jetstream -.->|"manages streams"| node_stream
node_stream -.->|"persists messages"| node_store
node_stream -.->|"backs up"| node_backup
node_server -.->|"manages routes"| node_routes
node_server -.->|"manages gateways"| node_gateways
node_server -.->|"manages leaf nodes"| node_leafnodes
node_server -.->|"starts monitoring"| node_monitor
node_server -.->|"configures authorization"| node_authz
node_server -.->|"configures certificates"| node_tls

click node_cli "https://github.com/nats-io/nats-server/blob/main/main.go"
click node_config "https://github.com/nats-io/nats-server/blob/main/conf/parse.go"
click node_server "https://github.com/nats-io/nats-server/blob/main/server/server.go"
click node_client "https://github.com/nats-io/nats-server/blob/main/server/client.go"
click node_parser "https://github.com/nats-io/nats-server/blob/main/server/parser.go"
click node_auth "https://github.com/nats-io/nats-server/blob/main/server/auth.go"
click node_subscriptions "https://github.com/nats-io/nats-server/blob/main/server/sublist.go"
click node_subjecttree "https://github.com/nats-io/nats-server/blob/main/server/stree/stree.go"
click node_accounts "https://github.com/nats-io/nats-server/blob/main/server/accounts.go"
click node_jetstream "https://github.com/nats-io/nats-server/blob/main/server/jetstream.go"
click node_stream "https://github.com/nats-io/nats-server/blob/main/server/stream.go"
click node_store "https://github.com/nats-io/nats-server/blob/main/server/store.go"
click node_backup "https://github.com/nats-io/nats-server/blob/main/server/stream_backup.go"
click node_routes "https://github.com/nats-io/nats-server/blob/main/server/route.go"
click node_gateways "https://github.com/nats-io/nats-server/blob/main/server/gateway.go"
click node_leafnodes "https://github.com/nats-io/nats-server/blob/main/server/leafnode.go"
click node_authz "https://github.com/nats-io/nats-server/blob/main/server/auth_callout.go"
click node_tls "https://github.com/nats-io/nats-server/blob/main/server/certidp/certidp.go"
click node_monitor "https://github.com/nats-io/nats-server/blob/main/server/monitor.go"
click node_logger "https://github.com/nats-io/nats-server/blob/main/logger/log.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_config,node_server,node_external toneBlue
class node_client,node_parser,node_auth,node_subscriptions,node_subjecttree,node_accounts toneAmber
class node_jetstream,node_stream,node_store,node_backup toneMint
class node_routes,node_gateways,node_leafnodes toneRose
class node_authz,node_tls,node_monitor,node_logger,node_operator toneIndigo
```

## Components

### Startup and configuration

- [Server CLI](https://github.com/nats-io/nats-server/blob/main/main.go): `main.go`
- [Config parsing](https://github.com/nats-io/nats-server/blob/main/conf/parse.go): `conf/parse.go`
- [Server runtime](https://github.com/nats-io/nats-server/blob/main/server/server.go): `server/server.go`

### Client messaging

- [Client protocol](https://github.com/nats-io/nats-server/blob/main/server/client.go): `server/client.go`
- [Protocol parser](https://github.com/nats-io/nats-server/blob/main/server/parser.go): `server/parser.go`
- [Authentication](https://github.com/nats-io/nats-server/blob/main/server/auth.go): `server/auth.go`
- [Subscription matching](https://github.com/nats-io/nats-server/blob/main/server/sublist.go): `server/sublist.go`
- [Subject tree](https://github.com/nats-io/nats-server/blob/main/server/stree/stree.go): `server/stree/stree.go`
- [Accounts](https://github.com/nats-io/nats-server/blob/main/server/accounts.go): `server/accounts.go`

### JetStream persistence

- [JetStream](https://github.com/nats-io/nats-server/blob/main/server/jetstream.go): `server/jetstream.go`
- [Stream management](https://github.com/nats-io/nats-server/blob/main/server/stream.go): `server/stream.go`
- [Message storage](https://github.com/nats-io/nats-server/blob/main/server/store.go): `server/store.go`
- [Stream backup](https://github.com/nats-io/nats-server/blob/main/server/stream_backup.go): `server/stream_backup.go`

### Distributed operation

- [Route connections](https://github.com/nats-io/nats-server/blob/main/server/route.go): `server/route.go`
- [Gateway connections](https://github.com/nats-io/nats-server/blob/main/server/gateway.go): `server/gateway.go`
- [Leaf connections](https://github.com/nats-io/nats-server/blob/main/server/leafnode.go): `server/leafnode.go`

### Security and operations

- [Authorization](https://github.com/nats-io/nats-server/blob/main/server/auth_callout.go): `server/auth_callout.go`
- [Certificate handling](https://github.com/nats-io/nats-server/blob/main/server/certidp/certidp.go): `server/certidp/certidp.go`
- [Monitoring](https://github.com/nats-io/nats-server/blob/main/server/monitor.go): `server/monitor.go`
- [Logging](https://github.com/nats-io/nats-server/blob/main/logger/log.go): `logger/log.go`

### Other components

- **Operator**
- **NATS clients**

## Connections

- Operator → Server CLI: configures
- Server CLI → Server runtime: starts (evidence: `main.go`)
- Server CLI → Config parsing: loads config
- Server runtime → Logging: uses logging (evidence: `server/server.go`)
- NATS clients → Client protocol: connects
- Client protocol → Authentication: authenticates
- Client protocol → Protocol parser: processes protocol
- Client protocol → Accounts: updates account (evidence: `server/client.go`)
- Client protocol → Subscription matching: inserts subscriptions (evidence: `server/client.go`)
- Client protocol → Subject tree: matches subjects
- Server runtime → Accounts: registers accounts (evidence: `server/server.go`)
- Server runtime → JetStream: initializes
- JetStream → Stream management: manages streams
- Stream management → Message storage: persists messages
- Stream management → Stream backup: backs up
- Server runtime → Route connections: manages routes
- Server runtime → Gateway connections: manages gateways
- Server runtime → Leaf connections: manages leaf nodes
- Server runtime → Monitoring: starts monitoring
- Server runtime → Authorization: configures authorization
- Server runtime → Certificate handling: configures certificates
