|
| 1 | +--- |
| 2 | +name: nats-design-subject |
| 3 | +description: >- |
| 4 | + Design NATS subject hierarchies for messaging patterns (pub/sub, request/reply, |
| 5 | + streaming). Apply naming conventions, segmentation strategies, and wildcard |
| 6 | + patterns to create scalable subject architectures. Use when designing NATS |
| 7 | + messaging systems, planning multi-tenant communication, or auditing existing |
| 8 | + subject hierarchies. Do not use for: (1) NATS server configuration or cluster |
| 9 | + setup, (2) client library implementation or connection code, (3) debugging |
| 10 | + connectivity or performance issues, (4) choosing between NATS and other |
| 11 | + messaging systems. |
| 12 | +allowed-tools: AskUserQuestion, Write, Read, Shell |
| 13 | +--- |
| 14 | + |
| 15 | +# Design NATS Subject Hierarchy |
| 16 | + |
| 17 | +Design a subject architecture that subscribers can efficiently navigate using wildcards, with proper segment ordering, tenant isolation, and growth path. |
| 18 | + |
| 19 | +## Interview Phase |
| 20 | + |
| 21 | +**Skip interview if ALL of these are already specified:** |
| 22 | +- Messaging patterns (pub/sub, request/reply, streaming) |
| 23 | +- Multi-tenancy needs (single/multi-tenant, scale requirements) |
| 24 | +- Security requirements (authorization, tenant isolation) |
| 25 | +- Persistence needs (JetStream vs core NATS) |
| 26 | + |
| 27 | +**Always interview if**: Migrating existing subjects (needs anti-pattern audit first) |
| 28 | + |
| 29 | +### Questions |
| 30 | + |
| 31 | +1. **Scope** — "Is this greenfield design or migrating existing subjects?" |
| 32 | + - Impact: Migration needs anti-pattern audit first (see [references/anti-patterns.md](references/anti-patterns.md)) |
| 33 | + |
| 34 | +2. **Multi-Tenancy & Scale** — "Do you need: (A) Single tenant, (B) Multi-tenant with isolation, (C) Massive scale with regions/shards?" |
| 35 | + - Impact: Determines whether tenant prefix is needed and segmentation depth |
| 36 | + |
| 37 | +3. **Messaging Patterns** — "Which patterns do you use? (A) Pub/Sub only, (B) Request/Reply, (C) Streaming/JetStream, (D) All/mix?" |
| 38 | + - Impact: JetStream needs stream-aware subject design; request/reply has its own conventions |
| 39 | + |
| 40 | +4. **Security** — "Do you need subject-based authorization or tenant isolation?" |
| 41 | + - Impact: Determines whether tenant/role prefixes are needed and permission boundaries |
| 42 | + |
| 43 | +5. **Persistence** — "Do you need JetStream persistence or core NATS only?" |
| 44 | + - Impact: Determines stream/consumer subject design and retention considerations |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## When to Use |
| 49 | + |
| 50 | +- Designing a new NATS messaging system |
| 51 | +- Planning multi-tenant subject isolation |
| 52 | +- Organizing device telemetry or event streams |
| 53 | +- Setting up request/reply patterns across microservices |
| 54 | +- Defining event subject structure for event-sourced systems |
| 55 | +- Building agentic AI platforms with inter-agent messaging |
| 56 | + |
| 57 | +## When NOT to Use |
| 58 | + |
| 59 | +- Configuring NATS server or cluster settings (infrastructure, not subject design) |
| 60 | +- Writing NATS client code or connection logic (implementation, not architecture) |
| 61 | +- Choosing between NATS and other messaging systems (technology evaluation) |
| 62 | +- Debugging existing NATS connectivity or performance issues |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## Workflow |
| 67 | + |
| 68 | +### 1. Identify Domain Boundaries |
| 69 | + |
| 70 | +List all business domains involved (orders, payments, inventory, etc.). Each domain becomes a top-level subject segment. |
| 71 | + |
| 72 | +### 2. Choose a Pattern |
| 73 | + |
| 74 | +Match the user's scenario to a pattern: |
| 75 | + |
| 76 | +| Use Case | Pattern | Example | |
| 77 | +|----------|---------|---------| |
| 78 | +| **Simple Domain** | `{domain}.{action}.{scope}` | `orders.created.us-west` | |
| 79 | +| **Multi-Region** | `{domain}.{action}.{region}.{id}` | `devices.telemetry.us-east.sensor-456` | |
| 80 | +| **Multi-Tenant SaaS** | `{tenant}.{domain}.{action}.{id}` | `acme-corp.analytics.processed.report-123` | |
| 81 | +| **Multi-Tenant AI** | `agents.{action}.{tenant}.{agent-id}.{task-id}` | `agents.task-assigned.tenant-abc.agent-xyz.task-123` | |
| 82 | +| **Request/Reply** | `{service}.request` / `{service}.reply` | `orders.request` / `orders.reply` | |
| 83 | +| **Event Sourcing** | `{aggregate}.{action}.v{version}.{id}` | `orders.order.created.v1.order-123` | |
| 84 | + |
| 85 | +For full pattern details with subscriber paths and scaling guidance, read [references/patterns.md](references/patterns.md). |
| 86 | + |
| 87 | +### 3. Order Segments Strategically |
| 88 | + |
| 89 | +Apply these rules when ordering segments left-to-right: |
| 90 | + |
| 91 | +- **Broad to specific**: Domain → Action → Scope → Identifier |
| 92 | +- **Low-cardinality left, high-cardinality right**: Regions (few values) before IDs (millions of values) |
| 93 | +- **Never put IDs or UUIDs before actions** |
| 94 | + |
| 95 | +``` |
| 96 | +✓ GOOD: orders.created.us-west.order-123 |
| 97 | + ↑ ↑ |
| 98 | + low-card high-card |
| 99 | +
|
| 100 | +✗ BAD: orders.order-123.us-west.created |
| 101 | + ↑ |
| 102 | + high-card early (kills wildcard filtering) |
| 103 | +``` |
| 104 | + |
| 105 | +Why this matters: NATS wildcard matching scans left-to-right. High-cardinality values on the left force subscribers into inefficient `orders.*.us-west.created` patterns that must match thousands of IDs. |
| 106 | + |
| 107 | +For common ordering mistakes and migration strategies, read [references/anti-patterns.md](references/anti-patterns.md). |
| 108 | + |
| 109 | +### 4. Plan Subscriber Paths |
| 110 | + |
| 111 | +For each domain, document how subscribers will filter: |
| 112 | + |
| 113 | +``` |
| 114 | +orders.> → All order events |
| 115 | +orders.created.> → All order creation events |
| 116 | +orders.created.us-west.> → Orders created in US West |
| 117 | +orders.created.us-west.order-123 → Specific order |
| 118 | +``` |
| 119 | + |
| 120 | +Design subjects for subscribers, not publishers. Subscribers determine how you organize — a good hierarchy lets them efficiently filter with wildcards. |
| 121 | + |
| 122 | +### 5. Design Security Model (if multi-tenant) |
| 123 | + |
| 124 | +If the user needs tenant isolation or role-based access: |
| 125 | + |
| 126 | +- Tenant ID as first segment enables subject-based authorization (`acme-corp.>`) |
| 127 | +- Separate admin subjects from user operations (`_admin.>` for platform ops) |
| 128 | +- Apply least-privilege permissions per service |
| 129 | + |
| 130 | +For authorization patterns and tenant isolation examples, read [references/security.md](references/security.md). |
| 131 | + |
| 132 | +### 6. Design JetStream Streams (if persistence needed) |
| 133 | + |
| 134 | +If the user needs JetStream: |
| 135 | + |
| 136 | +- One stream per domain (or per tenant for isolation) |
| 137 | +- Consumer filters for fine-grained routing |
| 138 | +- Retention policies per domain (financial: years, telemetry: days) |
| 139 | +- Keep to 4-6 subject segments — use consumer filters instead of deeper hierarchies |
| 140 | + |
| 141 | +For stream design, consumer patterns, and migration from core NATS, read [references/jetstream.md](references/jetstream.md). |
| 142 | + |
| 143 | +### 7. Validate and Write Output |
| 144 | + |
| 145 | +Present the design using this template: |
| 146 | + |
| 147 | +```markdown |
| 148 | +# NATS Subject Architecture: [System Name] |
| 149 | + |
| 150 | +## Domain Overview |
| 151 | +[Describe the domains and their interactions] |
| 152 | + |
| 153 | +## Subject Hierarchy |
| 154 | + |
| 155 | +### Domain: [Name] |
| 156 | +- `domain.action.{scope}.{id}` |
| 157 | +- `domain.action.{scope}.{id}` |
| 158 | + |
| 159 | +Subscriber paths: |
| 160 | +- `domain.>` — All events |
| 161 | +- `domain.action.>` — Specific action |
| 162 | + |
| 163 | +[Repeat for each domain] |
| 164 | + |
| 165 | +## Multi-Tenancy Model |
| 166 | +[How tenant isolation works via subjects, if applicable] |
| 167 | + |
| 168 | +## Security Model |
| 169 | +[Authorization rules per role/service, if applicable] |
| 170 | + |
| 171 | +## JetStream Streams |
| 172 | +[Stream definitions and consumer filters, if applicable] |
| 173 | + |
| 174 | +## Quality Validation |
| 175 | +[Run checklist below] |
| 176 | +``` |
| 177 | + |
| 178 | +### Example Output |
| 179 | + |
| 180 | +```markdown |
| 181 | +# NATS Subject Architecture: IoT Smart Building Platform |
| 182 | + |
| 183 | +## Domain Overview |
| 184 | + |
| 185 | +Smart building system with 10,000+ sensors across multiple regions sending temperature, humidity, and occupancy data. Needs real-time monitoring, regional aggregation, and alerting. |
| 186 | + |
| 187 | +## Subject Hierarchy |
| 188 | + |
| 189 | +### Domain: Devices |
| 190 | +- `devices.telemetry.{region}.{device-id}.{metric}` |
| 191 | +- `devices.telemetry.us-west.sensor-456.temperature` |
| 192 | +- `devices.telemetry.us-west.sensor-456.humidity` |
| 193 | +- `devices.telemetry.eu-central.sensor-789.occupancy` |
| 194 | + |
| 195 | +Subscriber paths: |
| 196 | +- `devices.telemetry.>` — All telemetry (global monitoring) |
| 197 | +- `devices.telemetry.us-west.>` — Regional dashboard (US West) |
| 198 | +- `devices.telemetry.>.>.temperature` — All temperature readings |
| 199 | + |
| 200 | +### Domain: Alerts |
| 201 | +- `alerts.triggered.{severity}.{region}.{device-id}` |
| 202 | +- `alerts.triggered.critical.us-west.sensor-456` |
| 203 | + |
| 204 | +Subscriber paths: |
| 205 | +- `alerts.triggered.critical.>` — Critical alerts only |
| 206 | +- `alerts.triggered.>.us-west.>` — Regional alert dashboard |
| 207 | + |
| 208 | +## Multi-Tenancy Model |
| 209 | + |
| 210 | +Not applicable (single organization) |
| 211 | + |
| 212 | +## Security Model |
| 213 | + |
| 214 | +- Building operators: `devices.telemetry.>`, `alerts.>` (subscribe only) |
| 215 | +- Alert service: `alerts.>` (publish + subscribe) |
| 216 | +- Admin: `>` (full access) |
| 217 | + |
| 218 | +## JetStream Streams |
| 219 | + |
| 220 | +Stream: `telemetry-us-west` |
| 221 | + Subjects: `devices.telemetry.us-west.>` |
| 222 | + Retention: 24h (high volume) |
| 223 | + |
| 224 | +Stream: `alerts` |
| 225 | + Subjects: `alerts.>` |
| 226 | + Retention: 30d |
| 227 | + |
| 228 | +## Quality Validation |
| 229 | + |
| 230 | +✓ All segments follow broad-to-specific order |
| 231 | +✓ Device IDs at rightmost position |
| 232 | +✓ Naming consistent (lowercase, hyphens) |
| 233 | +✓ Regional filtering efficient |
| 234 | +✓ 4 segments (within 4-6 limit) |
| 235 | +``` |
| 236 | + |
| 237 | +For complete real-world examples across microservices, IoT, SaaS, event sourcing, and agentic AI platforms, read [references/use-cases.md](references/use-cases.md). |
| 238 | + |
| 239 | +--- |
| 240 | + |
| 241 | +## Quick Start (Simple Cases) |
| 242 | + |
| 243 | +If you're designing a simple single-domain system without multi-tenancy: |
| 244 | + |
| 245 | +1. **Use Pattern 1** (Simple Domain): `{domain}.{action}.{id}` |
| 246 | +2. **Skip references** — follow the workflow above |
| 247 | +3. **Example**: `orders.created.order-123`, `payments.authorized.payment-456` |
| 248 | + |
| 249 | +For multi-region, multi-tenant, or event-sourcing needs, continue with full workflow and read references as needed. |
| 250 | + |
| 251 | +--- |
| 252 | + |
| 253 | +## Reference Navigation |
| 254 | + |
| 255 | +The skill includes 5 detailed reference documents — read them as needed during workflow steps: |
| 256 | + |
| 257 | +- **[patterns.md](references/patterns.md)**: Read when choosing initial pattern (step 2) |
| 258 | +- **[anti-patterns.md](references/anti-patterns.md)**: Read when migrating or auditing existing subjects |
| 259 | +- **[security.md](references/security.md)**: Read when multi-tenancy/authorization needed (step 5) |
| 260 | +- **[jetstream.md](references/jetstream.md)**: Read when persistence needed (step 6) |
| 261 | +- **[use-cases.md](references/use-cases.md)**: Read for complete worked examples per domain |
| 262 | + |
| 263 | +Don't read all references upfront — use them progressively as the workflow requires. |
| 264 | + |
| 265 | +--- |
| 266 | + |
| 267 | +## Naming Rules |
| 268 | + |
| 269 | +- All lowercase with hyphens: `orders.created` ✓ |
| 270 | +- Never underscores: `orders_created` ✗ |
| 271 | +- Never mixed case: `Orders.Created` ✗ |
| 272 | +- Keep to 4-6 segments maximum |
| 273 | + |
| 274 | +--- |
| 275 | + |
| 276 | +## Quality Checklist |
| 277 | + |
| 278 | +- [ ] All segments follow broad-to-specific order (`{domain}.{action}.{scope}.{id}`) |
| 279 | +- [ ] No UUID/ID fields appear before action/scope segments |
| 280 | +- [ ] Naming consistent (all lowercase, hyphens, no underscores) |
| 281 | +- [ ] Documented subscriber wildcard paths for each domain |
| 282 | +- [ ] No subjects deeper than 6 segments |
| 283 | +- [ ] Multi-tenancy isolation is clear (tenant ID positioning documented) |
| 284 | +- [ ] Security subjects defined for admin/monitoring access |
| 285 | +- [ ] No conflicting patterns (e.g., `orders.created.123` vs `orders.123.created`) |
| 286 | +- [ ] High-cardinality decision documented (why ID placement chosen) |
| 287 | + |
| 288 | +## Testing Your Design |
| 289 | + |
| 290 | +Validate your subject hierarchy before deployment: |
| 291 | + |
| 292 | +```bash |
| 293 | +# Start local NATS server |
| 294 | +nats-server -D |
| 295 | + |
| 296 | +# Test subscriber wildcards |
| 297 | +nats sub "orders.>" # Should match all order subjects |
| 298 | +nats sub "orders.created.>" # Should match only creations |
| 299 | +nats sub "orders.created.us-west.>" # Should match region-specific |
| 300 | + |
| 301 | +# Test publishing |
| 302 | +nats pub "orders.created.us-west.order-123" "test message" |
| 303 | + |
| 304 | +# Verify JetStream streams (if applicable) |
| 305 | +nats stream info orders-stream |
| 306 | +nats consumer info orders-stream order-consumer |
| 307 | +``` |
| 308 | + |
| 309 | +For existing deployments, audit current subjects: |
| 310 | + |
| 311 | +```bash |
| 312 | +# List active subjects (requires monitoring enabled) |
| 313 | +nats server report jetstream |
| 314 | + |
| 315 | +# Check subject permissions |
| 316 | +nats server check connection --account <account-name> |
| 317 | +``` |
| 318 | + |
| 319 | +## Reference Documentation |
| 320 | + |
| 321 | +- **[Patterns](references/patterns.md)**: 6 hierarchy patterns with subscriber paths and scaling guidance |
| 322 | +- **[Anti-Patterns](references/anti-patterns.md)**: 8 common mistakes with detection, fixes, and migration strategies |
| 323 | +- **[Security & Multi-Tenancy](references/security.md)**: Authorization patterns and tenant isolation |
| 324 | +- **[JetStream Design](references/jetstream.md)**: Stream filters, consumer subjects, and retention policies |
| 325 | +- **[Use Cases](references/use-cases.md)**: Complete examples for microservices, IoT, SaaS, event sourcing, agentic AI |
0 commit comments