|
| 1 | +--- |
| 2 | +name: designing-nats-subjects |
| 3 | +description: Design NATS subject hierarchies for messaging patterns (pub/sub, request/reply, streaming). Apply naming conventions, segmentation strategies, and wildcard patterns to create scalable subject architectures. Use when designing NATS messaging systems or planning multi-tenant communication. |
| 4 | +allowed-tools: AskUserQuestion, Write |
| 5 | +--- |
| 6 | + |
| 7 | +## Interview Phase (Optional) |
| 8 | + |
| 9 | +**When to Interview**: Skip if the user has specified: messaging patterns (pub/sub vs request/reply), multi-tenancy needs, and scale expectations. Interview when unclear which subjects matter most or what patterns are needed. |
| 10 | + |
| 11 | +### Critical Questions |
| 12 | + |
| 13 | +1. **Multi-Tenancy & Scale** (Impact: Determines subject segmentation strategy) |
| 14 | + - Question: "Do you need: (A) Single tenant, (B) Multi-tenant with isolation, (C) Massive scale with regions/shards?" |
| 15 | + - Why it matters: Multi-tenant systems need tenant isolation in subjects; large scale needs region/shard segmentation |
| 16 | + |
| 17 | +2. **Messaging Patterns** (Impact: Determines subject organization for different use cases) |
| 18 | + - Question: "Which patterns do you use? (A) Pub/Sub only, (B) Request/Reply, (C) Streaming/JetStream, (D) All/mix?" |
| 19 | + - Why it matters: Different patterns have different subject hierarchy needs |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Purpose |
| 24 | + |
| 25 | +Design effective NATS subject hierarchies that scale across messaging patterns (pub/sub, request/reply, JetStream streaming). Create subject architecture that's easy to navigate for subscribers while supporting multi-tenancy, security, and growth. |
| 26 | + |
| 27 | +## When to Use |
| 28 | + |
| 29 | +Apply this skill when: |
| 30 | +- Designing a new NATS messaging system |
| 31 | +- Planning multi-tenant subject isolation |
| 32 | +- Organizing device telemetry or event streams |
| 33 | +- Setting up request/reply patterns across microservices |
| 34 | +- Defining event subject structure for event-sourced systems |
| 35 | +- Building agentic AI platforms with inter-agent messaging |
| 36 | + |
| 37 | +## Core Principles |
| 38 | + |
| 39 | +### 1. The Subscriber-First Philosophy |
| 40 | + |
| 41 | +**Design subjects for subscribers, not publishers.** |
| 42 | + |
| 43 | +Publishers can publish to any subject, but subscribers determine how you organize. A good subject hierarchy groups related messages so subscribers can efficiently filter with wildcards. |
| 44 | + |
| 45 | +Bad: `publish.order.123.created` (published topic order) |
| 46 | +Good: `orders.created.region.us-west` (grouped for subscribers) |
| 47 | + |
| 48 | +### 2. Segment Ordering Strategy |
| 49 | + |
| 50 | +Order segments left-to-right from broad to specific. This enables natural wildcard filtering: |
| 51 | + |
| 52 | +``` |
| 53 | +Pattern: {domain}.{action}.{scope}.{identifier} |
| 54 | +
|
| 55 | +orders.created.us-west.order-123 |
| 56 | + ↑ ↑ ↑ ↑ |
| 57 | +domain action scope identifier |
| 58 | +(broad) (specific) |
| 59 | +``` |
| 60 | + |
| 61 | +**Subscriber paths**: |
| 62 | +- `orders.>` - All order events |
| 63 | +- `orders.created.>` - All order creation events |
| 64 | +- `orders.created.us-west.>` - Orders created in US West |
| 65 | +- `orders.created.us-west.order-123` - Specific order |
| 66 | + |
| 67 | +### 3. Cardinality Consideration |
| 68 | + |
| 69 | +**Place high-cardinality segments on the right.** High-cardinality means many unique values (IDs, timestamps). Low-cardinality means few values (regions, actions). |
| 70 | + |
| 71 | +``` |
| 72 | +✓ GOOD: orders.created.us-west.order-123 |
| 73 | + ↑ ↑ |
| 74 | + low-card high-card |
| 75 | + Subscribers: orders.created.us-west.> (1000s of specific orders) |
| 76 | +
|
| 77 | +✗ BAD: orders.order-123.us-west.created |
| 78 | + ↑ |
| 79 | + high-card early (kills hierarchy) |
| 80 | + Subscribers: orders.*.us-west.created (wildcard must match thousands of IDs) |
| 81 | +``` |
| 82 | + |
| 83 | +### 4. Consistency and Casing |
| 84 | + |
| 85 | +Use lowercase with hyphens (never underscores or mixed case): |
| 86 | +- `orders.created` ✓ |
| 87 | +- `orders_created` ✗ |
| 88 | +- `Orders.Created` ✗ |
| 89 | + |
| 90 | +## Quick Decision Matrix |
| 91 | + |
| 92 | +Choose patterns based on your use case: |
| 93 | + |
| 94 | +| Use Case | Pattern | Example | |
| 95 | +|----------|---------|---------| |
| 96 | +| **Simple Domain** | `{domain}.{action}.{scope}` | `orders.created.us-west` | |
| 97 | +| **Multi-Region** | `{domain}.{action}.{region}.{id}` | `devices.telemetry.us-east.sensor-456` | |
| 98 | +| **Multi-Tenant SaaS** | `{tenant}.{domain}.{action}.{id}` | `acme-corp.analytics.processed.report-123` | |
| 99 | +| **Multi-Tenant AI** | `agents.{action}.{tenant}.{agent-id}.{task-id}` | `agents.task-assigned.tenant-abc.agent-xyz.task-123` | |
| 100 | +| **Request/Reply** | `{service}.request` / `{service}.reply` | `orders.request` / `orders.reply` | |
| 101 | +| **Event Sourcing** | `{aggregate}.{action}.v{version}.{id}` | `orders.order.created.v1.order-123` | |
| 102 | + |
| 103 | +## Subject Design Framework |
| 104 | + |
| 105 | +### Step 1: Identify Domain Boundaries |
| 106 | + |
| 107 | +What business domains are involved? (orders, payments, inventory, etc.) |
| 108 | + |
| 109 | +### Step 2: Choose Segment Layers |
| 110 | + |
| 111 | +Determine your hierarchy depth (3-6 segments typical): |
| 112 | +- Layer 1: Domain (always) |
| 113 | +- Layer 2: Action/event type |
| 114 | +- Layer 3: Scope (region, tenant, context) |
| 115 | +- Layer 4+: Identifiers (add only as needed) |
| 116 | + |
| 117 | +### Step 3: Order Segments Strategically |
| 118 | + |
| 119 | +- Broad categories first (most subscribers filter here) |
| 120 | +- Specific identifiers last (high-cardinality) |
| 121 | +- Never put IDs or UUIDs before actions |
| 122 | + |
| 123 | +### Step 4: Plan Subscriber Paths |
| 124 | + |
| 125 | +List how subscribers will filter: |
| 126 | + |
| 127 | +``` |
| 128 | +Subscriber: orders.created.> |
| 129 | + Gets: All order creation events (any region/ID) |
| 130 | +
|
| 131 | +Subscriber: orders.created.us-west.> |
| 132 | + Gets: All order creation events in US West region |
| 133 | +
|
| 134 | +Subscriber: orders.> |
| 135 | + Gets: All order events (any action, region, ID) |
| 136 | +``` |
| 137 | + |
| 138 | +### Step 5: Validate Against Quality Checklist |
| 139 | + |
| 140 | +Run the checklist (see below) before implementation. |
| 141 | + |
| 142 | +## Key Insights |
| 143 | + |
| 144 | +**Why Segment Order Matters**: NATS wildcard matching scans left-to-right. If high-cardinality values are on the left, subscribers must use more specific filters, losing the benefit of hierarchical organization. |
| 145 | + |
| 146 | +**Wildcard Gotcha**: `orders.*.*.order-123` works but defeats the purpose (subscribers can't say "all orders from a region"). Put identifiers last. |
| 147 | + |
| 148 | +**Hierarchy Depth**: Keep to 4-6 segments. Beyond that, use properties in JetStream consumer filters instead of subject complexity. |
| 149 | + |
| 150 | +**Multi-Tenancy Pattern**: For SaaS, tenant ID as first segment enables subject-based authorization: |
| 151 | +- Tenant A users can only see: `tenant-a.>` |
| 152 | +- Tenant B users can only see: `tenant-b.>` |
| 153 | +- Admin can see: `>` |
| 154 | + |
| 155 | +## Output Format |
| 156 | + |
| 157 | +Present your subject design as: |
| 158 | + |
| 159 | +```markdown |
| 160 | +# NATS Subject Architecture: [System Name] |
| 161 | + |
| 162 | +## Domain Overview |
| 163 | +[Describe the domains and their interactions] |
| 164 | + |
| 165 | +## Subject Hierarchy |
| 166 | + |
| 167 | +### Domain: Orders |
| 168 | +- `orders.created.{region}.{order-id}` |
| 169 | +- `orders.updated.{region}.{order-id}` |
| 170 | +- `orders.cancelled.{region}.{order-id}` |
| 171 | + |
| 172 | +Subscriber paths: |
| 173 | +- `orders.>` - All order events |
| 174 | +- `orders.created.us-west.>` - Orders created in US West |
| 175 | + |
| 176 | +### Domain: Payments |
| 177 | +[Similar structure] |
| 178 | + |
| 179 | +## Multi-Tenancy Model |
| 180 | +[How tenant isolation works via subjects] |
| 181 | + |
| 182 | +## Quality Validation |
| 183 | +- [x] All segments ordered (broad to specific) |
| 184 | +- [x] No high-cardinality before action |
| 185 | +- [x] Consistent naming (lowercase, hyphens) |
| 186 | +- [x] Subscriber paths documented |
| 187 | +``` |
| 188 | + |
| 189 | +## Quality Checklist |
| 190 | + |
| 191 | +Validate your design before implementation: |
| 192 | + |
| 193 | +- [ ] All segments follow `{domain}.{action}.{scope}.{id}` order (broad to specific) |
| 194 | +- [ ] No UUID/ID fields appear before action/scope segments |
| 195 | +- [ ] Naming consistent (all lowercase, hyphens, no underscores) |
| 196 | +- [ ] Documented subscriber wildcard paths for each domain |
| 197 | +- [ ] No subjects deeper than 6 segments (consider JetStream consumer filters) |
| 198 | +- [ ] Multi-tenancy isolation is clear (tenant ID positioning documented) |
| 199 | +- [ ] Security subjects defined for admin/monitoring access |
| 200 | +- [ ] No conflicting patterns (e.g., `orders.created.123` vs `orders.123.created`) |
| 201 | +- [ ] High-cardinality decision documented (why ID placement chosen) |
| 202 | + |
| 203 | +## Reference Documentation |
| 204 | + |
| 205 | +For comprehensive guidance on specific topics: |
| 206 | + |
| 207 | +- **[Patterns](references/patterns.md)**: 6 hierarchy patterns with real-world examples for each use case |
| 208 | +- **[Anti-Patterns](references/anti-patterns.md)**: 8 common mistakes with fixes and migration strategies |
| 209 | +- **[Security & Multi-Tenancy](references/security.md)**: Authorization patterns and tenant isolation |
| 210 | +- **[JetStream Design](references/jetstream.md)**: Stream filters, consumer subjects, and event patterns |
| 211 | +- **[Use Cases](references/use-cases.md)**: Domain-specific examples (microservices, IoT, SaaS, events, AI platforms) |
| 212 | + |
| 213 | +## Version History |
| 214 | + |
| 215 | +- **v1.0** (2026-01-24): Initial release with core principles, decision matrix, quality checklist |
0 commit comments