|
| 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 or planning multi-tenant communication. Do not use for NATS |
| 8 | + server configuration, cluster setup, or client library implementation — this |
| 9 | + skill covers subject architecture only. |
| 10 | +allowed-tools: AskUserQuestion, Write |
| 11 | +--- |
| 12 | + |
| 13 | +# Design NATS Subject Hierarchy |
| 14 | + |
| 15 | +Design a subject architecture that subscribers can efficiently navigate using wildcards, with proper segment ordering, tenant isolation, and growth path. |
| 16 | + |
| 17 | +## Interview Phase |
| 18 | + |
| 19 | +**Skip if** the user has already specified: messaging patterns, multi-tenancy needs, security requirements, and persistence needs. |
| 20 | + |
| 21 | +### Questions |
| 22 | + |
| 23 | +1. **Scope** — "Is this greenfield design or migrating existing subjects?" |
| 24 | + - Impact: Migration needs anti-pattern audit first (see [references/anti-patterns.md](references/anti-patterns.md)) |
| 25 | + |
| 26 | +2. **Multi-Tenancy & Scale** — "Do you need: (A) Single tenant, (B) Multi-tenant with isolation, (C) Massive scale with regions/shards?" |
| 27 | + - Impact: Determines whether tenant prefix is needed and segmentation depth |
| 28 | + |
| 29 | +3. **Messaging Patterns** — "Which patterns do you use? (A) Pub/Sub only, (B) Request/Reply, (C) Streaming/JetStream, (D) All/mix?" |
| 30 | + - Impact: JetStream needs stream-aware subject design; request/reply has its own conventions |
| 31 | + |
| 32 | +4. **Security** — "Do you need subject-based authorization or tenant isolation?" |
| 33 | + - Impact: Determines whether tenant/role prefixes are needed and permission boundaries |
| 34 | + |
| 35 | +5. **Persistence** — "Do you need JetStream persistence or core NATS only?" |
| 36 | + - Impact: Determines stream/consumer subject design and retention considerations |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## When to Use |
| 41 | + |
| 42 | +- Designing a new NATS messaging system |
| 43 | +- Planning multi-tenant subject isolation |
| 44 | +- Organizing device telemetry or event streams |
| 45 | +- Setting up request/reply patterns across microservices |
| 46 | +- Defining event subject structure for event-sourced systems |
| 47 | +- Building agentic AI platforms with inter-agent messaging |
| 48 | + |
| 49 | +## When NOT to Use |
| 50 | + |
| 51 | +- Configuring NATS server or cluster settings (infrastructure, not subject design) |
| 52 | +- Writing NATS client code or connection logic (implementation, not architecture) |
| 53 | +- Choosing between NATS and other messaging systems (technology evaluation) |
| 54 | +- Debugging existing NATS connectivity or performance issues |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Workflow |
| 59 | + |
| 60 | +### 1. Identify Domain Boundaries |
| 61 | + |
| 62 | +List all business domains involved (orders, payments, inventory, etc.). Each domain becomes a top-level subject segment. |
| 63 | + |
| 64 | +### 2. Choose a Pattern |
| 65 | + |
| 66 | +Match the user's scenario to a pattern: |
| 67 | + |
| 68 | +| Use Case | Pattern | Example | |
| 69 | +|----------|---------|---------| |
| 70 | +| **Simple Domain** | `{domain}.{action}.{scope}` | `orders.created.us-west` | |
| 71 | +| **Multi-Region** | `{domain}.{action}.{region}.{id}` | `devices.telemetry.us-east.sensor-456` | |
| 72 | +| **Multi-Tenant SaaS** | `{tenant}.{domain}.{action}.{id}` | `acme-corp.analytics.processed.report-123` | |
| 73 | +| **Multi-Tenant AI** | `agents.{action}.{tenant}.{agent-id}.{task-id}` | `agents.task-assigned.tenant-abc.agent-xyz.task-123` | |
| 74 | +| **Request/Reply** | `{service}.request` / `{service}.reply` | `orders.request` / `orders.reply` | |
| 75 | +| **Event Sourcing** | `{aggregate}.{action}.v{version}.{id}` | `orders.order.created.v1.order-123` | |
| 76 | + |
| 77 | +For full pattern details with subscriber paths and scaling guidance, read [references/patterns.md](references/patterns.md). |
| 78 | + |
| 79 | +### 3. Order Segments Strategically |
| 80 | + |
| 81 | +Apply these rules when ordering segments left-to-right: |
| 82 | + |
| 83 | +- **Broad to specific**: Domain → Action → Scope → Identifier |
| 84 | +- **Low-cardinality left, high-cardinality right**: Regions (few values) before IDs (millions of values) |
| 85 | +- **Never put IDs or UUIDs before actions** |
| 86 | + |
| 87 | +``` |
| 88 | +✓ GOOD: orders.created.us-west.order-123 |
| 89 | + ↑ ↑ |
| 90 | + low-card high-card |
| 91 | +
|
| 92 | +✗ BAD: orders.order-123.us-west.created |
| 93 | + ↑ |
| 94 | + high-card early (kills wildcard filtering) |
| 95 | +``` |
| 96 | + |
| 97 | +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. |
| 98 | + |
| 99 | +For common ordering mistakes and migration strategies, read [references/anti-patterns.md](references/anti-patterns.md). |
| 100 | + |
| 101 | +### 4. Plan Subscriber Paths |
| 102 | + |
| 103 | +For each domain, document how subscribers will filter: |
| 104 | + |
| 105 | +``` |
| 106 | +orders.> → All order events |
| 107 | +orders.created.> → All order creation events |
| 108 | +orders.created.us-west.> → Orders created in US West |
| 109 | +orders.created.us-west.order-123 → Specific order |
| 110 | +``` |
| 111 | + |
| 112 | +Design subjects for subscribers, not publishers. Subscribers determine how you organize — a good hierarchy lets them efficiently filter with wildcards. |
| 113 | + |
| 114 | +### 5. Design Security Model (if multi-tenant) |
| 115 | + |
| 116 | +If the user needs tenant isolation or role-based access: |
| 117 | + |
| 118 | +- Tenant ID as first segment enables subject-based authorization (`acme-corp.>`) |
| 119 | +- Separate admin subjects from user operations (`_admin.>` for platform ops) |
| 120 | +- Apply least-privilege permissions per service |
| 121 | + |
| 122 | +For authorization patterns and tenant isolation examples, read [references/security.md](references/security.md). |
| 123 | + |
| 124 | +### 6. Design JetStream Streams (if persistence needed) |
| 125 | + |
| 126 | +If the user needs JetStream: |
| 127 | + |
| 128 | +- One stream per domain (or per tenant for isolation) |
| 129 | +- Consumer filters for fine-grained routing |
| 130 | +- Retention policies per domain (financial: years, telemetry: days) |
| 131 | +- Keep to 4-6 subject segments — use consumer filters instead of deeper hierarchies |
| 132 | + |
| 133 | +For stream design, consumer patterns, and migration from core NATS, read [references/jetstream.md](references/jetstream.md). |
| 134 | + |
| 135 | +### 7. Validate and Write Output |
| 136 | + |
| 137 | +Present the design using this template: |
| 138 | + |
| 139 | +```markdown |
| 140 | +# NATS Subject Architecture: [System Name] |
| 141 | + |
| 142 | +## Domain Overview |
| 143 | +[Describe the domains and their interactions] |
| 144 | + |
| 145 | +## Subject Hierarchy |
| 146 | + |
| 147 | +### Domain: [Name] |
| 148 | +- `domain.action.{scope}.{id}` |
| 149 | +- `domain.action.{scope}.{id}` |
| 150 | + |
| 151 | +Subscriber paths: |
| 152 | +- `domain.>` — All events |
| 153 | +- `domain.action.>` — Specific action |
| 154 | + |
| 155 | +[Repeat for each domain] |
| 156 | + |
| 157 | +## Multi-Tenancy Model |
| 158 | +[How tenant isolation works via subjects, if applicable] |
| 159 | + |
| 160 | +## Security Model |
| 161 | +[Authorization rules per role/service, if applicable] |
| 162 | + |
| 163 | +## JetStream Streams |
| 164 | +[Stream definitions and consumer filters, if applicable] |
| 165 | + |
| 166 | +## Quality Validation |
| 167 | +[Run checklist below] |
| 168 | +``` |
| 169 | + |
| 170 | +For complete real-world examples across microservices, IoT, SaaS, event sourcing, and agentic AI platforms, read [references/use-cases.md](references/use-cases.md). |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +## Naming Rules |
| 175 | + |
| 176 | +- All lowercase with hyphens: `orders.created` ✓ |
| 177 | +- Never underscores: `orders_created` ✗ |
| 178 | +- Never mixed case: `Orders.Created` ✗ |
| 179 | +- Keep to 4-6 segments maximum |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## Quality Checklist |
| 184 | + |
| 185 | +- [ ] All segments follow broad-to-specific order (`{domain}.{action}.{scope}.{id}`) |
| 186 | +- [ ] No UUID/ID fields appear before action/scope segments |
| 187 | +- [ ] Naming consistent (all lowercase, hyphens, no underscores) |
| 188 | +- [ ] Documented subscriber wildcard paths for each domain |
| 189 | +- [ ] No subjects deeper than 6 segments |
| 190 | +- [ ] Multi-tenancy isolation is clear (tenant ID positioning documented) |
| 191 | +- [ ] Security subjects defined for admin/monitoring access |
| 192 | +- [ ] No conflicting patterns (e.g., `orders.created.123` vs `orders.123.created`) |
| 193 | +- [ ] High-cardinality decision documented (why ID placement chosen) |
| 194 | + |
| 195 | +## Reference Documentation |
| 196 | + |
| 197 | +- **[Patterns](references/patterns.md)**: 6 hierarchy patterns with subscriber paths and scaling guidance |
| 198 | +- **[Anti-Patterns](references/anti-patterns.md)**: 8 common mistakes with detection, fixes, and migration strategies |
| 199 | +- **[Security & Multi-Tenancy](references/security.md)**: Authorization patterns and tenant isolation |
| 200 | +- **[JetStream Design](references/jetstream.md)**: Stream filters, consumer subjects, and retention policies |
| 201 | +- **[Use Cases](references/use-cases.md)**: Complete examples for microservices, IoT, SaaS, event sourcing, agentic AI |
0 commit comments