contextos-agents 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/AGENTS.md +421 -0
- package/.agents/adapters/aider/export.js +137 -0
- package/.agents/adapters/claude/export.js +55 -0
- package/.agents/adapters/copilot/export.js +107 -0
- package/.agents/adapters/cursor/export.js +210 -0
- package/.agents/adapters/gemini/export.js +115 -0
- package/.agents/adapters/shared.js +119 -0
- package/.agents/adapters/zed/export.js +111 -0
- package/.agents/core/profiles/backend.yaml +47 -0
- package/.agents/core/profiles/enterprise.yaml +46 -0
- package/.agents/core/profiles/frontend.yaml +46 -0
- package/.agents/core/profiles/hackathon.yaml +45 -0
- package/.agents/core/profiles/mvp.yaml +43 -0
- package/.agents/core/profiles/startup.yaml +48 -0
- package/.agents/core/skills/adapters/EXAMPLES.md +19 -0
- package/.agents/core/skills/adapters/SKILL.md +105 -0
- package/.agents/core/skills/adapters/TROUBLESHOOTING.md +7 -0
- package/.agents/core/skills/adapters/VALIDATION.json +12 -0
- package/.agents/core/skills/adapters/skill.yaml +10 -0
- package/.agents/core/skills/architecture-diagrams/SKILL.md +108 -0
- package/.agents/core/skills/architecture-diagrams/VALIDATION.json +12 -0
- package/.agents/core/skills/architecture-diagrams/skill.yaml +8 -0
- package/.agents/core/skills/brutalist-design/SKILL.md +150 -0
- package/.agents/core/skills/brutalist-design/VALIDATION.json +12 -0
- package/.agents/core/skills/brutalist-design/skill.yaml +8 -0
- package/.agents/core/skills/context-manager/EXAMPLES.md +19 -0
- package/.agents/core/skills/context-manager/SKILL.md +126 -0
- package/.agents/core/skills/context-manager/TROUBLESHOOTING.md +7 -0
- package/.agents/core/skills/context-manager/VALIDATION.json +12 -0
- package/.agents/core/skills/context-manager/skill.yaml +10 -0
- package/.agents/core/skills/context-os/EXAMPLES.md +21 -0
- package/.agents/core/skills/context-os/SKILL.md +169 -0
- package/.agents/core/skills/context-os/TROUBLESHOOTING.md +7 -0
- package/.agents/core/skills/context-os/VALIDATION.json +12 -0
- package/.agents/core/skills/context-os/packs.yaml +59 -0
- package/.agents/core/skills/context-os/references/context-rules.md +68 -0
- package/.agents/core/skills/context-os/references/pipeline.md +119 -0
- package/.agents/core/skills/context-os/references/project-graph.md +103 -0
- package/.agents/core/skills/context-os/rules.yaml +135 -0
- package/.agents/core/skills/context-os/skill.yaml +10 -0
- package/.agents/core/skills/database/EXAMPLES.md +74 -0
- package/.agents/core/skills/database/SKILL.md +101 -0
- package/.agents/core/skills/database/TROUBLESHOOTING.md +18 -0
- package/.agents/core/skills/database/VALIDATION.json +11 -0
- package/.agents/core/skills/database/skill.yaml +25 -0
- package/.agents/core/skills/ddd/EXAMPLES.md +42 -0
- package/.agents/core/skills/ddd/SKILL.md +247 -0
- package/.agents/core/skills/ddd/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ddd/VALIDATION.json +12 -0
- package/.agents/core/skills/ddd/ddd.md +178 -0
- package/.agents/core/skills/ddd/skill.yaml +10 -0
- package/.agents/core/skills/decisions/EXAMPLES.md +35 -0
- package/.agents/core/skills/decisions/SKILL.md +90 -0
- package/.agents/core/skills/decisions/TROUBLESHOOTING.md +13 -0
- package/.agents/core/skills/decisions/VALIDATION.json +12 -0
- package/.agents/core/skills/decisions/skill.yaml +10 -0
- package/.agents/core/skills/docker/EXAMPLES.md +56 -0
- package/.agents/core/skills/docker/SKILL.md +63 -0
- package/.agents/core/skills/docker/TROUBLESHOOTING.md +18 -0
- package/.agents/core/skills/docker/VALIDATION.json +11 -0
- package/.agents/core/skills/docker/skill.yaml +23 -0
- package/.agents/core/skills/engineering-workflow/EXAMPLES.md +57 -0
- package/.agents/core/skills/engineering-workflow/SKILL.md +343 -0
- package/.agents/core/skills/engineering-workflow/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/engineering-workflow/VALIDATION.json +12 -0
- package/.agents/core/skills/engineering-workflow/skill.yaml +13 -0
- package/.agents/core/skills/fastapi/EXAMPLES.md +36 -0
- package/.agents/core/skills/fastapi/SKILL.md +148 -0
- package/.agents/core/skills/fastapi/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/fastapi/VALIDATION.json +12 -0
- package/.agents/core/skills/fastapi/fastapi.md +112 -0
- package/.agents/core/skills/fastapi/skill.yaml +10 -0
- package/.agents/core/skills/gemini-precision/SKILL.md +169 -0
- package/.agents/core/skills/gemini-precision/VALIDATION.json +12 -0
- package/.agents/core/skills/gemini-precision/skill.yaml +9 -0
- package/.agents/core/skills/generators/EXAMPLES.md +19 -0
- package/.agents/core/skills/generators/SKILL.md +112 -0
- package/.agents/core/skills/generators/TROUBLESHOOTING.md +7 -0
- package/.agents/core/skills/generators/VALIDATION.json +12 -0
- package/.agents/core/skills/generators/skill.yaml +10 -0
- package/.agents/core/skills/generators/templates/API.md +77 -0
- package/.agents/core/skills/generators/templates/ARCHITECTURE.md +70 -0
- package/.agents/core/skills/generators/templates/DATABASE.md +42 -0
- package/.agents/core/skills/generators/templates/DECISION.md +46 -0
- package/.agents/core/skills/generators/templates/PRD.md +67 -0
- package/.agents/core/skills/generators/templates/PROJECT_GRAPH.md +56 -0
- package/.agents/core/skills/generators/templates/ROADMAP.md +51 -0
- package/.agents/core/skills/generators/templates/TASKS.md +43 -0
- package/.agents/core/skills/generators/templates/UI.md +73 -0
- package/.agents/core/skills/graphify/EXAMPLES.md +73 -0
- package/.agents/core/skills/graphify/SKILL.md +130 -0
- package/.agents/core/skills/graphify/VALIDATION.json +12 -0
- package/.agents/core/skills/graphify/skill.yaml +13 -0
- package/.agents/core/skills/gstack-roles/EXAMPLES.md +23 -0
- package/.agents/core/skills/gstack-roles/SKILL.md +152 -0
- package/.agents/core/skills/gstack-roles/TROUBLESHOOTING.md +13 -0
- package/.agents/core/skills/gstack-roles/VALIDATION.json +12 -0
- package/.agents/core/skills/gstack-roles/skill.yaml +12 -0
- package/.agents/core/skills/impeccable-design/EXAMPLES.md +26 -0
- package/.agents/core/skills/impeccable-design/SKILL.md +201 -0
- package/.agents/core/skills/impeccable-design/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/impeccable-design/VALIDATION.json +12 -0
- package/.agents/core/skills/impeccable-design/skill.yaml +14 -0
- package/.agents/core/skills/interview-me/SKILL.md +97 -0
- package/.agents/core/skills/interview-me/VALIDATION.json +12 -0
- package/.agents/core/skills/interview-me/skill.yaml +8 -0
- package/.agents/core/skills/microservices/EXAMPLES.md +38 -0
- package/.agents/core/skills/microservices/SKILL.md +164 -0
- package/.agents/core/skills/microservices/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/microservices/VALIDATION.json +12 -0
- package/.agents/core/skills/microservices/microservices.md +119 -0
- package/.agents/core/skills/microservices/skill.yaml +10 -0
- package/.agents/core/skills/minimalist-design/SKILL.md +113 -0
- package/.agents/core/skills/minimalist-design/VALIDATION.json +12 -0
- package/.agents/core/skills/minimalist-design/skill.yaml +8 -0
- package/.agents/core/skills/nestjs/EXAMPLES.md +40 -0
- package/.agents/core/skills/nestjs/SKILL.md +139 -0
- package/.agents/core/skills/nestjs/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/nestjs/VALIDATION.json +12 -0
- package/.agents/core/skills/nestjs/nestjs.md +103 -0
- package/.agents/core/skills/nestjs/skill.yaml +10 -0
- package/.agents/core/skills/nextjs/EXAMPLES.md +40 -0
- package/.agents/core/skills/nextjs/SKILL.md +163 -0
- package/.agents/core/skills/nextjs/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/nextjs/VALIDATION.json +12 -0
- package/.agents/core/skills/nextjs/nextjs.md +67 -0
- package/.agents/core/skills/nextjs/skill.yaml +10 -0
- package/.agents/core/skills/node/EXAMPLES.md +80 -0
- package/.agents/core/skills/node/SKILL.md +128 -0
- package/.agents/core/skills/node/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/node/VALIDATION.json +12 -0
- package/.agents/core/skills/node/node.md +87 -0
- package/.agents/core/skills/node/skill.yaml +10 -0
- package/.agents/core/skills/performance/EXAMPLES.md +30 -0
- package/.agents/core/skills/performance/SKILL.md +75 -0
- package/.agents/core/skills/performance/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/performance/VALIDATION.json +12 -0
- package/.agents/core/skills/performance/performance.md +52 -0
- package/.agents/core/skills/performance/skill.yaml +10 -0
- package/.agents/core/skills/ponytail-mindset/EXAMPLES.md +45 -0
- package/.agents/core/skills/ponytail-mindset/SKILL.md +193 -0
- package/.agents/core/skills/ponytail-mindset/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ponytail-mindset/VALIDATION.json +12 -0
- package/.agents/core/skills/ponytail-mindset/skill.yaml +14 -0
- package/.agents/core/skills/react/EXAMPLES.md +79 -0
- package/.agents/core/skills/react/SKILL.md +132 -0
- package/.agents/core/skills/react/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/react/VALIDATION.json +12 -0
- package/.agents/core/skills/react/react.md +93 -0
- package/.agents/core/skills/react/skill.yaml +10 -0
- package/.agents/core/skills/react-best-practices/SKILL.md +155 -0
- package/.agents/core/skills/react-best-practices/VALIDATION.json +12 -0
- package/.agents/core/skills/react-best-practices/skill.yaml +10 -0
- package/.agents/core/skills/redesign-audit/SKILL.md +117 -0
- package/.agents/core/skills/redesign-audit/VALIDATION.json +12 -0
- package/.agents/core/skills/redesign-audit/skill.yaml +8 -0
- package/.agents/core/skills/security/EXAMPLES.md +64 -0
- package/.agents/core/skills/security/SKILL.md +158 -0
- package/.agents/core/skills/security/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/security/VALIDATION.json +12 -0
- package/.agents/core/skills/security/security.md +106 -0
- package/.agents/core/skills/security/skill.yaml +10 -0
- package/.agents/core/skills/soft-design/SKILL.md +108 -0
- package/.agents/core/skills/soft-design/VALIDATION.json +12 -0
- package/.agents/core/skills/soft-design/skill.yaml +8 -0
- package/.agents/core/skills/state-management/EXAMPLES.md +56 -0
- package/.agents/core/skills/state-management/SKILL.md +48 -0
- package/.agents/core/skills/state-management/TROUBLESHOOTING.md +18 -0
- package/.agents/core/skills/state-management/VALIDATION.json +11 -0
- package/.agents/core/skills/state-management/skill.yaml +22 -0
- package/.agents/core/skills/subagent-orchestrator/SKILL.md +100 -0
- package/.agents/core/skills/subagent-orchestrator/VALIDATION.json +12 -0
- package/.agents/core/skills/subagent-orchestrator/skill.yaml +8 -0
- package/.agents/core/skills/system-design/EXAMPLES.md +75 -0
- package/.agents/core/skills/system-design/SKILL.md +419 -0
- package/.agents/core/skills/system-design/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/system-design/VALIDATION.json +12 -0
- package/.agents/core/skills/system-design/skill.yaml +13 -0
- package/.agents/core/skills/system-design/system-design.md +112 -0
- package/.agents/core/skills/testing/EXAMPLES.md +71 -0
- package/.agents/core/skills/testing/SKILL.md +70 -0
- package/.agents/core/skills/testing/TROUBLESHOOTING.md +18 -0
- package/.agents/core/skills/testing/VALIDATION.json +11 -0
- package/.agents/core/skills/testing/skill.yaml +26 -0
- package/.agents/core/skills/typescript/EXAMPLES.md +64 -0
- package/.agents/core/skills/typescript/SKILL.md +112 -0
- package/.agents/core/skills/typescript/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/typescript/VALIDATION.json +12 -0
- package/.agents/core/skills/typescript/skill.yaml +10 -0
- package/.agents/core/skills/typescript/typescript.md +71 -0
- package/.agents/core/skills/ui-design/EXAMPLES.md +21 -0
- package/.agents/core/skills/ui-design/SKILL.md +124 -0
- package/.agents/core/skills/ui-design/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ui-design/VALIDATION.json +12 -0
- package/.agents/core/skills/ui-design/skill.yaml +10 -0
- package/.agents/core/skills/ui-design/ui.md +88 -0
- package/.agents/core/skills/ui-ux-pro/EXAMPLES.md +62 -0
- package/.agents/core/skills/ui-ux-pro/SKILL.md +375 -0
- package/.agents/core/skills/ui-ux-pro/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ui-ux-pro/VALIDATION.json +12 -0
- package/.agents/core/skills/ui-ux-pro/skill.yaml +13 -0
- package/.agents/core/skills/ux-design/EXAMPLES.md +36 -0
- package/.agents/core/skills/ux-design/SKILL.md +116 -0
- package/.agents/core/skills/ux-design/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/ux-design/VALIDATION.json +12 -0
- package/.agents/core/skills/ux-design/skill.yaml +10 -0
- package/.agents/core/skills/ux-design/ux.md +80 -0
- package/.agents/core/skills/vercel-optimize/SKILL.md +83 -0
- package/.agents/core/skills/vercel-optimize/VALIDATION.json +12 -0
- package/.agents/core/skills/vercel-optimize/skill.yaml +10 -0
- package/.agents/core/skills/web-accessibility/EXAMPLES.md +39 -0
- package/.agents/core/skills/web-accessibility/SKILL.md +170 -0
- package/.agents/core/skills/web-accessibility/TROUBLESHOOTING.md +19 -0
- package/.agents/core/skills/web-accessibility/VALIDATION.json +12 -0
- package/.agents/core/skills/web-accessibility/accessibility.md +63 -0
- package/.agents/core/skills/web-accessibility/skill.yaml +10 -0
- package/.agents/ctx.js +370 -0
- package/.agents/generated/claude/skills/adapters/SKILL.md +126 -0
- package/.agents/generated/claude/skills/architecture-diagrams/SKILL.md +101 -0
- package/.agents/generated/claude/skills/brutalist-design/SKILL.md +145 -0
- package/.agents/generated/claude/skills/context-manager/SKILL.md +147 -0
- package/.agents/generated/claude/skills/context-os/SKILL.md +191 -0
- package/.agents/generated/claude/skills/database/SKILL.md +191 -0
- package/.agents/generated/claude/skills/ddd/SKILL.md +305 -0
- package/.agents/generated/claude/skills/decisions/SKILL.md +134 -0
- package/.agents/generated/claude/skills/docker/SKILL.md +135 -0
- package/.agents/generated/claude/skills/engineering-workflow/SKILL.md +414 -0
- package/.agents/generated/claude/skills/fastapi/SKILL.md +200 -0
- package/.agents/generated/claude/skills/gemini-precision/SKILL.md +161 -0
- package/.agents/generated/claude/skills/generators/SKILL.md +133 -0
- package/.agents/generated/claude/skills/graphify/SKILL.md +198 -0
- package/.agents/generated/claude/skills/gstack-roles/SKILL.md +184 -0
- package/.agents/generated/claude/skills/impeccable-design/SKILL.md +241 -0
- package/.agents/generated/claude/skills/interview-me/SKILL.md +90 -0
- package/.agents/generated/claude/skills/microservices/SKILL.md +218 -0
- package/.agents/generated/claude/skills/minimalist-design/SKILL.md +108 -0
- package/.agents/generated/claude/skills/nestjs/SKILL.md +195 -0
- package/.agents/generated/claude/skills/nextjs/SKILL.md +219 -0
- package/.agents/generated/claude/skills/node/SKILL.md +224 -0
- package/.agents/generated/claude/skills/performance/SKILL.md +121 -0
- package/.agents/generated/claude/skills/ponytail-mindset/SKILL.md +252 -0
- package/.agents/generated/claude/skills/react/SKILL.md +227 -0
- package/.agents/generated/claude/skills/react-best-practices/SKILL.md +146 -0
- package/.agents/generated/claude/skills/redesign-audit/SKILL.md +112 -0
- package/.agents/generated/claude/skills/security/SKILL.md +237 -0
- package/.agents/generated/claude/skills/soft-design/SKILL.md +103 -0
- package/.agents/generated/claude/skills/state-management/SKILL.md +120 -0
- package/.agents/generated/claude/skills/subagent-orchestrator/SKILL.md +93 -0
- package/.agents/generated/claude/skills/system-design/SKILL.md +507 -0
- package/.agents/generated/claude/skills/testing/SKILL.md +157 -0
- package/.agents/generated/claude/skills/typescript/SKILL.md +192 -0
- package/.agents/generated/claude/skills/ui-design/SKILL.md +161 -0
- package/.agents/generated/claude/skills/ui-ux-pro/SKILL.md +451 -0
- package/.agents/generated/claude/skills/ux-design/SKILL.md +168 -0
- package/.agents/generated/claude/skills/vercel-optimize/SKILL.md +76 -0
- package/.agents/generated/claude/skills/web-accessibility/SKILL.md +225 -0
- package/.agents/generated/gemini/skills/adapters/SKILL.md +135 -0
- package/.agents/generated/gemini/skills/architecture-diagrams/SKILL.md +107 -0
- package/.agents/generated/gemini/skills/brutalist-design/SKILL.md +151 -0
- package/.agents/generated/gemini/skills/context-manager/SKILL.md +156 -0
- package/.agents/generated/gemini/skills/context-os/SKILL.md +200 -0
- package/.agents/generated/gemini/skills/database/SKILL.md +200 -0
- package/.agents/generated/gemini/skills/ddd/SKILL.md +314 -0
- package/.agents/generated/gemini/skills/decisions/SKILL.md +143 -0
- package/.agents/generated/gemini/skills/docker/SKILL.md +144 -0
- package/.agents/generated/gemini/skills/engineering-workflow/SKILL.md +423 -0
- package/.agents/generated/gemini/skills/fastapi/SKILL.md +209 -0
- package/.agents/generated/gemini/skills/gemini-precision/SKILL.md +167 -0
- package/.agents/generated/gemini/skills/generators/SKILL.md +142 -0
- package/.agents/generated/gemini/skills/graphify/SKILL.md +205 -0
- package/.agents/generated/gemini/skills/gstack-roles/SKILL.md +193 -0
- package/.agents/generated/gemini/skills/impeccable-design/SKILL.md +250 -0
- package/.agents/generated/gemini/skills/interview-me/SKILL.md +96 -0
- package/.agents/generated/gemini/skills/microservices/SKILL.md +227 -0
- package/.agents/generated/gemini/skills/minimalist-design/SKILL.md +114 -0
- package/.agents/generated/gemini/skills/nestjs/SKILL.md +204 -0
- package/.agents/generated/gemini/skills/nextjs/SKILL.md +298 -0
- package/.agents/generated/gemini/skills/node/SKILL.md +323 -0
- package/.agents/generated/gemini/skills/performance/SKILL.md +185 -0
- package/.agents/generated/gemini/skills/ponytail-mindset/SKILL.md +261 -0
- package/.agents/generated/gemini/skills/react/SKILL.md +332 -0
- package/.agents/generated/gemini/skills/react-best-practices/SKILL.md +152 -0
- package/.agents/generated/gemini/skills/redesign-audit/SKILL.md +118 -0
- package/.agents/generated/gemini/skills/security/SKILL.md +355 -0
- package/.agents/generated/gemini/skills/soft-design/SKILL.md +109 -0
- package/.agents/generated/gemini/skills/state-management/SKILL.md +129 -0
- package/.agents/generated/gemini/skills/subagent-orchestrator/SKILL.md +99 -0
- package/.agents/generated/gemini/skills/system-design/SKILL.md +631 -0
- package/.agents/generated/gemini/skills/testing/SKILL.md +166 -0
- package/.agents/generated/gemini/skills/typescript/SKILL.md +275 -0
- package/.agents/generated/gemini/skills/ui-design/SKILL.md +170 -0
- package/.agents/generated/gemini/skills/ui-ux-pro/SKILL.md +460 -0
- package/.agents/generated/gemini/skills/ux-design/SKILL.md +177 -0
- package/.agents/generated/gemini/skills/vercel-optimize/SKILL.md +82 -0
- package/.agents/generated/gemini/skills/web-accessibility/SKILL.md +300 -0
- package/.agents/mcp/runtime.py +454 -0
- package/.agents/mcp/server.mjs +189373 -0
- package/.agents/plugins.js +574 -0
- package/.agents/profiles.js +267 -0
- package/.agents/resolver.js +336 -0
- package/.agents/skills-index.json +200 -0
- package/.agents/skills.json +6 -0
- package/.agents/validate.js +500 -0
- package/LICENSE +21 -0
- package/README.md +415 -0
- package/benchmarks/gemini-issues.js +533 -0
- package/bin/index.js +458 -0
- package/package.json +70 -0
- package/registry.json +53 -0
- package/registry.schema.json +32 -0
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: system-design
|
|
3
|
+
description: >
|
|
4
|
+
Architecture and system design skill based on donnemartin's System Design Primer. Teaches scalable architecture thinking: load balancers, caching, DB partitioning, microservices, CAP theorem, and trade-off analysis before writing backend code.
|
|
5
|
+
---
|
|
6
|
+
# system-design
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
Scalable system architecture blueprint based on the System Design Primer and DDIA. Enforces load balancing, multi-tier caching (Redis, CDN), database partitioning, CAP theorem tradeoffs, and rate limiting before code is written.
|
|
11
|
+
|
|
12
|
+
## When to Use
|
|
13
|
+
|
|
14
|
+
Activate during the PLAN phase of any backend service, API design, database schema creation, or scalability optimization.
|
|
15
|
+
|
|
16
|
+
## Rules & Patterns
|
|
17
|
+
|
|
18
|
+
Based on [donnemartin/system-design-primer](https://github.com/donnemartin/system-design-primer) — the most starred system design resource on GitHub.
|
|
19
|
+
|
|
20
|
+
## Core Principle
|
|
21
|
+
|
|
22
|
+
> **Everything is a trade-off.** Before writing a single line of backend code, reason through the system at scale. A flat monolith that works now fails at 10× load.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Mandatory Pre-Design Checklist
|
|
27
|
+
|
|
28
|
+
Before architecting any backend system, answer these questions:
|
|
29
|
+
|
|
30
|
+
1. **Scale**: What is the expected QPS (queries per second)? Peak vs average?
|
|
31
|
+
2. **Data volume**: How much data? Growth rate? 1GB? 1TB? 1PB?
|
|
32
|
+
3. **Consistency vs Availability**: Can we tolerate eventual consistency? (CAP theorem)
|
|
33
|
+
4. **Read/Write ratio**: Is it read-heavy (cache it!) or write-heavy (shard it!)?
|
|
34
|
+
5. **Latency requirements**: Real-time (<100ms)? Near-real-time (<1s)? Batch?
|
|
35
|
+
6. **Global distribution**: Single region or multi-region?
|
|
36
|
+
7. **Deployment model**: Traditional servers, Serverless, or Edge functions?
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Core Architecture Patterns
|
|
41
|
+
|
|
42
|
+
### Load Balancing
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
Clients → Load Balancer → [App Server 1, App Server 2, App Server N]
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
- Use **Round Robin** for stateless services
|
|
49
|
+
- Use **Least Connections** for varying request times
|
|
50
|
+
- Use **IP Hash** for session affinity (or move sessions to Redis)
|
|
51
|
+
- Always add **health checks** — remove unhealthy nodes automatically
|
|
52
|
+
|
|
53
|
+
**Rule**: Any service expecting > 1000 RPS needs a load balancer. No exceptions.
|
|
54
|
+
|
|
55
|
+
### Caching Strategy
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
App → [Cache Layer: Redis/Memcached] → Database
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Cache decision ladder (check in order):
|
|
62
|
+
|
|
63
|
+
1. Is it read > write? → Cache it
|
|
64
|
+
2. Is it expensive to compute? → Cache it
|
|
65
|
+
3. Is it user-specific? → Cache with user key
|
|
66
|
+
4. Is it global? → Shared cache, shorter TTL
|
|
67
|
+
|
|
68
|
+
**Cache patterns**:
|
|
69
|
+
|
|
70
|
+
- **Cache-aside** (lazy loading): check cache → miss → load DB → write cache
|
|
71
|
+
- **Write-through**: write to DB AND cache simultaneously (consistency > performance)
|
|
72
|
+
- **Write-behind**: write to cache → async flush to DB (performance > consistency)
|
|
73
|
+
|
|
74
|
+
**Invalidation**: Use TTL + event-driven invalidation. Never stale-forever.
|
|
75
|
+
|
|
76
|
+
**Modern framework-native caching (Next.js App Router)**:
|
|
77
|
+
Before spinning up a dedicated Redis instance for caching API responses, check if Next.js built-in mechanisms are sufficient:
|
|
78
|
+
|
|
79
|
+
- `revalidatePath('/dashboard')` — invalidate all cache for a route
|
|
80
|
+
- `revalidateTag('user-profile')` — fine-grained tagged cache invalidation
|
|
81
|
+
- `unstable_cache()` — server-side data caching with TTL
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
// [GOOD] Use Next.js native caching first
|
|
85
|
+
import { revalidateTag } from 'next/cache'
|
|
86
|
+
|
|
87
|
+
const getUser = unstable_cache(
|
|
88
|
+
async (id: string) => db.users.findById(id),
|
|
89
|
+
['user'],
|
|
90
|
+
{ tags: ['user-profile'], revalidate: 3600 }
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
// Invalidate on mutation:
|
|
94
|
+
await db.users.update(id, data)
|
|
95
|
+
revalidateTag('user-profile')
|
|
96
|
+
|
|
97
|
+
// [BAD] Don't add Redis for simple SSR caching when Next.js handles it
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Database Architecture
|
|
101
|
+
|
|
102
|
+
#### When to use SQL vs NoSQL
|
|
103
|
+
|
|
104
|
+
| Scenario | Use SQL | Use NoSQL |
|
|
105
|
+
| ---------- | --------- | ----------- |
|
|
106
|
+
| Complex joins, ACID transactions | [PASS] | [FAIL] |
|
|
107
|
+
| Flexible/evolving schema | [FAIL] | [PASS] |
|
|
108
|
+
| Horizontal scaling needed | Careful | [PASS] |
|
|
109
|
+
| Simple key-value lookup | Overkill | [PASS] |
|
|
110
|
+
| Full-text search | Use Elasticsearch | Use Elasticsearch |
|
|
111
|
+
| Time-series data | TimescaleDB | InfluxDB |
|
|
112
|
+
|
|
113
|
+
#### Scaling Databases
|
|
114
|
+
|
|
115
|
+
**Vertical scaling**: Bigger machine. Easy but has ceiling.
|
|
116
|
+
**Read replicas**: Route SELECT to replicas, writes to primary.
|
|
117
|
+
**Sharding (horizontal partitioning)**:
|
|
118
|
+
|
|
119
|
+
- Hash sharding: `user_id % N` — even distribution, hard to rebalance
|
|
120
|
+
- Range sharding: user_id 1-1M on shard 1 — easy range queries, hotspots risk
|
|
121
|
+
- Directory-based: lookup table maps key → shard — flexible, but lookup is overhead
|
|
122
|
+
|
|
123
|
+
**Denormalization**: For read-heavy systems, duplicate data to avoid joins.
|
|
124
|
+
**Rule**: Don't shard until you've maxed out read replicas.
|
|
125
|
+
|
|
126
|
+
### Message Queues & Async Processing
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
Producer → [Queue: Redis/RabbitMQ/Kafka] → Consumer Workers
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Use queues when:
|
|
133
|
+
|
|
134
|
+
- Operation takes > 200ms (email, PDF generation, ML inference)
|
|
135
|
+
- You need retry logic on failure
|
|
136
|
+
- You need to decouple services
|
|
137
|
+
- Traffic spikes need to be absorbed
|
|
138
|
+
|
|
139
|
+
**Kafka** = durability + replay + high throughput (events/analytics)
|
|
140
|
+
**Redis Queue** = simplicity + low latency (jobs/tasks)
|
|
141
|
+
**RabbitMQ** = complex routing + acknowledgements
|
|
142
|
+
|
|
143
|
+
### Microservices vs Monolith
|
|
144
|
+
|
|
145
|
+
**Start with a monolith** unless you have > 10 engineers or proven scale need.
|
|
146
|
+
|
|
147
|
+
When to split into microservices:
|
|
148
|
+
|
|
149
|
+
- Independent deployment cycles needed
|
|
150
|
+
- Different scaling requirements per service
|
|
151
|
+
- Team autonomy (Conway's Law)
|
|
152
|
+
- Clear service boundaries (DDD bounded contexts)
|
|
153
|
+
|
|
154
|
+
**Rule**: A microservice should be able to be rewritten in 2 weeks by 2 engineers.
|
|
155
|
+
|
|
156
|
+
Service communication:
|
|
157
|
+
|
|
158
|
+
- **Sync (REST/gRPC)**: when caller needs immediate response
|
|
159
|
+
- **Async (events/queue)**: when caller can tolerate delay, or decoupling is needed
|
|
160
|
+
- **BFF / Server Actions**: for web apps, prefer typed client-server contracts (see below)
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Modern Stack Patterns (Serverless, Edge, Next.js)
|
|
165
|
+
|
|
166
|
+
### Serverless & Edge Architecture
|
|
167
|
+
|
|
168
|
+
When deploying to serverless (Vercel Functions, AWS Lambda) or edge (Vercel Edge, Cloudflare Workers), the classical "App Server + Load Balancer" model changes:
|
|
169
|
+
|
|
170
|
+
**Cold Start Problem**:
|
|
171
|
+
|
|
172
|
+
- Serverless functions spin up from zero on first request — this can add 100–1000ms
|
|
173
|
+
- **Never** do heavy initialization at module level (DB connections, config loading, crypto keys)
|
|
174
|
+
- **Always** initialize lazily inside the handler, or use a connection pooling service
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// [BAD] Wrong: Module-level initialization (runs on cold start, hangs the function)
|
|
178
|
+
const db = new DatabaseClient({ ... }) // top of file
|
|
179
|
+
|
|
180
|
+
// [GOOD] Correct: Lazy initialization with caching
|
|
181
|
+
let db: DatabaseClient | null = null
|
|
182
|
+
function getDb() {
|
|
183
|
+
if (!db) db = new DatabaseClient({ ... })
|
|
184
|
+
return db
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
**DB Connection Pooling in Serverless**:
|
|
189
|
+
|
|
190
|
+
- Traditional in-process pools (pg-pool, knex) do NOT work in serverless — each invocation is ephemeral
|
|
191
|
+
- Use **Prisma Accelerate**, **PlanetScale**, **Neon** pooling, or **Supabase** — they handle pooling at the infrastructure level
|
|
192
|
+
- Rule: If deploying to Vercel/serverless, NEVER assume `max_connections` is managed by your app process
|
|
193
|
+
|
|
194
|
+
**Edge Functions limitations**:
|
|
195
|
+
|
|
196
|
+
- No Node.js APIs (no `fs`, no `crypto.randomBytes`, limited DNS)
|
|
197
|
+
- Latency must be < 50ms — no heavy DB queries
|
|
198
|
+
- Use edge for: auth token verification, A/B testing, geo-routing, lightweight transformations
|
|
199
|
+
|
|
200
|
+
### BFF Pattern & Server Actions (Type-Safe Client-Server)
|
|
201
|
+
|
|
202
|
+
When building web apps, prefer typed client-server communication over generic REST endpoints:
|
|
203
|
+
|
|
204
|
+
**Option 1: Server Actions (Next.js App Router)**
|
|
205
|
+
For mutations that touch the database directly, skip the API route entirely:
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
// [GOOD] Server Action: No API route needed, fully type-safe
|
|
209
|
+
"use server"
|
|
210
|
+
export async function updateUser(id: string, data: UpdateUserInput) {
|
|
211
|
+
// Input validation (always!)
|
|
212
|
+
const validated = UpdateUserSchema.parse(data)
|
|
213
|
+
|
|
214
|
+
// Auth check (always before data access!)
|
|
215
|
+
const session = await getSession()
|
|
216
|
+
if (session.userId !== id && !session.isAdmin) {
|
|
217
|
+
throw new Error("Forbidden")
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return db.users.update(id, validated)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// [BAD] Over-engineering: Don't create /api/users/[id] + fetch wrapper for simple mutations
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
**Option 2: tRPC (Full-stack type safety)**
|
|
227
|
+
For complex APIs with many routes, use tRPC to get end-to-end type safety from DB to UI without code generation.
|
|
228
|
+
|
|
229
|
+
**Option 3: REST (When appropriate)**
|
|
230
|
+
When building a public API consumed by external clients or mobile apps — use REST with OpenAPI spec.
|
|
231
|
+
|
|
232
|
+
**Decision rule**:
|
|
233
|
+
|
|
234
|
+
- Internal web-to-DB mutation → **Server Action**
|
|
235
|
+
- Internal complex API → **tRPC**
|
|
236
|
+
- Public/mobile API → **REST + OpenAPI**
|
|
237
|
+
|
|
238
|
+
### Domain-Driven Design (DDD) — Business Logic Isolation
|
|
239
|
+
|
|
240
|
+
**Rule**: NEVER write business logic inside API route handlers, Server Actions, or controllers. Always extract to dedicated services/use-cases.
|
|
241
|
+
|
|
242
|
+
```
|
|
243
|
+
[FAIL] Wrong structure:
|
|
244
|
+
app/api/orders/route.ts ← contains: validation + auth + business logic + DB query
|
|
245
|
+
|
|
246
|
+
[PASS] Correct structure:
|
|
247
|
+
app/api/orders/route.ts ← only: parse request, call service, return response
|
|
248
|
+
src/services/order.service.ts ← all business logic, testable without HTTP context
|
|
249
|
+
src/repositories/order.repo.ts ← all DB queries
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
Example:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
// [BAD] Business logic in route (untestable, bloated)
|
|
256
|
+
export async function POST(req: Request) {
|
|
257
|
+
const data = await req.json()
|
|
258
|
+
if (data.quantity <= 0) return new Response("Invalid", { status: 400 })
|
|
259
|
+
const inventory = await db.inventory.findById(data.productId)
|
|
260
|
+
if (inventory.stock < data.quantity) return new Response("Out of stock", { status: 400 })
|
|
261
|
+
const total = inventory.price * data.quantity
|
|
262
|
+
// ... 40 more lines
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// [GOOD] Thin route, fat service
|
|
266
|
+
export async function POST(req: Request) {
|
|
267
|
+
const data = await req.json()
|
|
268
|
+
const result = await orderService.createOrder(data)
|
|
269
|
+
return Response.json(result)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// orderService.createOrder() — pure function, fully unit-testable without HTTP
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
## Scalability Design Patterns
|
|
278
|
+
|
|
279
|
+
### CDN (Content Delivery Network)
|
|
280
|
+
|
|
281
|
+
- Serve static assets (JS, CSS, images) from CDN edge nodes
|
|
282
|
+
- Cache API responses that don't change per-user
|
|
283
|
+
- Reduce origin server load by 80%+
|
|
284
|
+
|
|
285
|
+
### Rate Limiting
|
|
286
|
+
|
|
287
|
+
Always implement for public APIs:
|
|
288
|
+
|
|
289
|
+
```
|
|
290
|
+
- Token bucket: smooth bursts, allows brief spikes
|
|
291
|
+
- Leaky bucket: strict rate, no bursts
|
|
292
|
+
- Fixed window: simple, vulnerable to boundary spikes
|
|
293
|
+
- Sliding window: most accurate, slightly more complex
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
Store rate limit state in Redis (not in-process — it doesn't survive restarts).
|
|
297
|
+
|
|
298
|
+
### Circuit Breaker
|
|
299
|
+
|
|
300
|
+
Prevent cascade failures:
|
|
301
|
+
|
|
302
|
+
```
|
|
303
|
+
CLOSED (normal) → [failures > threshold] → OPEN (fail fast)
|
|
304
|
+
↑ ↓
|
|
305
|
+
└────── [timeout] ← HALF-OPEN (test request) ──┘
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### Database Connection Pooling
|
|
309
|
+
|
|
310
|
+
- **Traditional servers**: Use pg-pool, knex, Prisma connection pool
|
|
311
|
+
- **Serverless**: Use Prisma Accelerate, PlanetScale, Neon, or Supabase pooling — NOT in-process pools
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
## CAP Theorem in Practice
|
|
316
|
+
|
|
317
|
+
**You can only guarantee 2 of 3**: Consistency, Availability, Partition Tolerance
|
|
318
|
+
|
|
319
|
+
| System | Chooses | Example |
|
|
320
|
+
| -------- | --------- | --------- |
|
|
321
|
+
| Traditional SQL | CP | PostgreSQL |
|
|
322
|
+
| Distributed NoSQL | AP | DynamoDB, Cassandra |
|
|
323
|
+
| Cache | AP (tunable) | Redis with replication |
|
|
324
|
+
|
|
325
|
+
**For most apps**: Choose AP. Accept eventual consistency. Use optimistic locking for critical writes.
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## Designing Data-Intensive Applications (DDIA) Patterns
|
|
330
|
+
|
|
331
|
+
Based on _Designing Data-Intensive Applications_ (Martin Kleppmann) and [ciembor/agent-rules-books](https://github.com/ciembor/agent-rules-books).
|
|
332
|
+
|
|
333
|
+
### 1. The Dual-Write Problem & Transactional Outbox
|
|
334
|
+
|
|
335
|
+
**The Anti-Pattern**: Updating the database and sending a message to a broker (Kafka, RabbitMQ, SQS) in two separate operations. If one fails, the system enters an inconsistent state.
|
|
336
|
+
|
|
337
|
+
**The Solution**: Write the business entity AND an event record to an `outbox` table in the SAME database transaction:
|
|
338
|
+
|
|
339
|
+
```sql
|
|
340
|
+
BEGIN TRANSACTION;
|
|
341
|
+
UPDATE orders SET status = 'PAID' WHERE id = 'ord_123';
|
|
342
|
+
INSERT INTO outbox_events (id, aggregate_type, aggregate_id, event_type, payload, created_at)
|
|
343
|
+
VALUES ('evt_456', 'Order', 'ord_123', 'OrderPaid', '{"amount": 99.00}', NOW());
|
|
344
|
+
COMMIT;
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
A background process (polling worker or Debezium CDC) reads `outbox_events`, delivers them to the message broker, and marks them as published.
|
|
348
|
+
|
|
349
|
+
### 2. Idempotency Invariant for Mutations
|
|
350
|
+
|
|
351
|
+
All write operations exposed over HTTP or queues MUST support deduplication:
|
|
352
|
+
|
|
353
|
+
- Accept an `Idempotency-Key` header (UUID or client-generated hash).
|
|
354
|
+
- Store key with status in Redis or DB with a TTL (e.g., 24 hours).
|
|
355
|
+
- If the key is already `COMPLETED`, return the cached response immediately without re-executing.
|
|
356
|
+
- If `IN_PROGRESS`, return HTTP `409 Conflict` or queue retry.
|
|
357
|
+
|
|
358
|
+
### 3. Read-Your-Own-Writes Consistency
|
|
359
|
+
|
|
360
|
+
When using read replicas, replication lag (even 50ms) causes users to not see their own changes immediately after saving:
|
|
361
|
+
|
|
362
|
+
- **Rule**: Route user reads to the primary database for `N` seconds (e.g., 5s) following any mutation by that user.
|
|
363
|
+
- Route all other queries and background jobs to read replicas.
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
367
|
+
## Architecture Decision Template
|
|
368
|
+
|
|
369
|
+
When proposing any backend architecture, include:
|
|
370
|
+
|
|
371
|
+
```markdown
|
|
372
|
+
## System Design Decision
|
|
373
|
+
|
|
374
|
+
**Scale Target**: [X RPS, Y GB data, Z users]
|
|
375
|
+
**Deployment Model**: [Traditional servers | Serverless | Edge]
|
|
376
|
+
**CAP Choice**: [CP/AP] because [reason]
|
|
377
|
+
**Read/Write Ratio**: [X:Y]
|
|
378
|
+
|
|
379
|
+
### Components
|
|
380
|
+
- **API Layer**: [REST/tRPC/Server Actions] — [why this choice]
|
|
381
|
+
- **Cache**: [Next.js native | Redis] for [what] with [TTL/tags strategy]
|
|
382
|
+
- **Database**: [SQL/NoSQL] - [pooling solution for serverless if applicable]
|
|
383
|
+
- **Async**: [Queue tech] for [what operations]
|
|
384
|
+
|
|
385
|
+
### Business Logic Isolation
|
|
386
|
+
- Services: [list key service files]
|
|
387
|
+
- Repositories: [list key repo files]
|
|
388
|
+
- Routes/Actions: [thin handlers only]
|
|
389
|
+
|
|
390
|
+
### Trade-offs Accepted
|
|
391
|
+
- [Trade-off 1]: [Why acceptable]
|
|
392
|
+
- [Trade-off 2]: [Why acceptable]
|
|
393
|
+
|
|
394
|
+
### Scaling Path
|
|
395
|
+
1. Now (MVP): [simple setup]
|
|
396
|
+
2. At 10× load: [first scaling step]
|
|
397
|
+
3. At 100× load: [next scaling step]
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
## Code Examples
|
|
402
|
+
|
|
403
|
+
See `EXAMPLES.md` for detailed code examples.
|
|
404
|
+
|
|
405
|
+
## Validation Checklist
|
|
406
|
+
|
|
407
|
+
What to verify during the review phase before completing the task.
|
|
408
|
+
|
|
409
|
+
## Common Mistakes
|
|
410
|
+
|
|
411
|
+
Anti-patterns and things to explicitly avoid. See `TROUBLESHOOTING.md`.
|
|
412
|
+
|
|
413
|
+
## Integration Notes
|
|
414
|
+
|
|
415
|
+
How this skill interacts with other skills.
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
<!-- Source: system-design.md -->
|
|
419
|
+
|
|
420
|
+
# System Design — Patterns & Principles
|
|
421
|
+
|
|
422
|
+
## Architecture Patterns
|
|
423
|
+
|
|
424
|
+
### Monolith (Start Here)
|
|
425
|
+
|
|
426
|
+
- **When**: MVP, small team, < 100K users
|
|
427
|
+
- **Structure**: Modular monolith with clear boundaries
|
|
428
|
+
- **Rule**: You can always extract microservices later. You can't easily merge them back.
|
|
429
|
+
|
|
430
|
+
### Microservices
|
|
431
|
+
|
|
432
|
+
- **When**: Team > 10 engineers, independent deployment needed
|
|
433
|
+
- **Communication**: REST (sync), Message Queue (async)
|
|
434
|
+
- **Data**: Each service owns its database
|
|
435
|
+
- **Pitfalls**: Network complexity, distributed transactions, debugging difficulty
|
|
436
|
+
|
|
437
|
+
### Event-Driven
|
|
438
|
+
|
|
439
|
+
- **When**: Loose coupling between services, async processing
|
|
440
|
+
- **Patterns**: Event Sourcing, CQRS, Pub/Sub
|
|
441
|
+
- **Tools**: Kafka, RabbitMQ, AWS SQS, Redis Streams
|
|
442
|
+
|
|
443
|
+
## Scalability
|
|
444
|
+
|
|
445
|
+
### Horizontal vs Vertical
|
|
446
|
+
|
|
447
|
+
- **Vertical first** — upgrade your server before distributing
|
|
448
|
+
- **Horizontal when** — you hit single-machine limits or need redundancy
|
|
449
|
+
|
|
450
|
+
### Database Scaling
|
|
451
|
+
|
|
452
|
+
1. **Indexes** — most common fix for slow queries
|
|
453
|
+
2. **Read replicas** — for read-heavy workloads
|
|
454
|
+
3. **Connection pooling** — PgBouncer, connection limits
|
|
455
|
+
4. **Caching** — Redis for hot data
|
|
456
|
+
5. **Sharding** — last resort, adds massive complexity
|
|
457
|
+
|
|
458
|
+
### Caching Layers
|
|
459
|
+
|
|
460
|
+
```
|
|
461
|
+
Client Cache (browser) → CDN → API Cache (Redis) → Database
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
| Strategy | Description |
|
|
465
|
+
| --- | --- |
|
|
466
|
+
| Cache-Aside | App checks cache first, fetches from DB on miss |
|
|
467
|
+
| Write-Through | App writes to cache AND DB simultaneously |
|
|
468
|
+
| Write-Behind | App writes to cache, async write to DB |
|
|
469
|
+
| TTL-based | Set expiry, accept stale data |
|
|
470
|
+
|
|
471
|
+
## Load Balancing
|
|
472
|
+
|
|
473
|
+
- **Round Robin** — simple, default
|
|
474
|
+
- **Least Connections** — for varying request duration
|
|
475
|
+
- **IP Hash** — for sticky sessions (avoid if possible)
|
|
476
|
+
- **Health checks** — remove unhealthy instances
|
|
477
|
+
|
|
478
|
+
## API Design
|
|
479
|
+
|
|
480
|
+
### REST Conventions
|
|
481
|
+
|
|
482
|
+
```
|
|
483
|
+
GET /users → List users
|
|
484
|
+
GET /users/:id → Get user
|
|
485
|
+
POST /users → Create user
|
|
486
|
+
PUT /users/:id → Full update
|
|
487
|
+
PATCH /users/:id → Partial update
|
|
488
|
+
DELETE /users/:id → Delete user
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
### Pagination
|
|
492
|
+
|
|
493
|
+
- Cursor-based for real-time data (recommended)
|
|
494
|
+
- Offset-based for static data
|
|
495
|
+
|
|
496
|
+
### Versioning
|
|
497
|
+
|
|
498
|
+
- URL-based (`/v1/`, `/v2/`) — simplest
|
|
499
|
+
- Header-based — more flexible
|
|
500
|
+
|
|
501
|
+
## Reliability
|
|
502
|
+
|
|
503
|
+
- **Circuit breaker** — stop calling failing services
|
|
504
|
+
- **Retry with exponential backoff** — for transient failures
|
|
505
|
+
- **Timeout** — every external call needs a timeout
|
|
506
|
+
- **Health checks** — `/health` endpoint for load balancers
|
|
507
|
+
- **Graceful degradation** — work with reduced functionality
|
|
508
|
+
|
|
509
|
+
## Data Storage
|
|
510
|
+
|
|
511
|
+
| Use Case | Technology |
|
|
512
|
+
| --- | --- |
|
|
513
|
+
| Relational data, ACID | PostgreSQL |
|
|
514
|
+
| Key-value, caching | Redis |
|
|
515
|
+
| Full-text search | Elasticsearch, Meilisearch |
|
|
516
|
+
| Document store | MongoDB (if truly schemaless) |
|
|
517
|
+
| Time series | TimescaleDB, InfluxDB |
|
|
518
|
+
| Blob storage | S3, R2 |
|
|
519
|
+
| Queue | Redis Streams, RabbitMQ, SQS |
|
|
520
|
+
|
|
521
|
+
## Decision Framework
|
|
522
|
+
|
|
523
|
+
Before choosing an architecture:
|
|
524
|
+
|
|
525
|
+
1. What's the expected load? (users, requests/sec)
|
|
526
|
+
2. What's the team size?
|
|
527
|
+
3. What's the timeline?
|
|
528
|
+
4. What are the consistency requirements?
|
|
529
|
+
5. What's the budget?
|
|
530
|
+
|
|
531
|
+
**Default answer**: Start with a monolith, PostgreSQL, Redis cache. Extract services only when you have data showing you need to.
|
|
532
|
+
|
|
533
|
+
<!-- Source: EXAMPLES.md -->
|
|
534
|
+
|
|
535
|
+
# system-design Examples — Anti-patterns vs ContextOS Standard
|
|
536
|
+
|
|
537
|
+
## Example 1: Database Caching Strategy
|
|
538
|
+
|
|
539
|
+
### Anti-pattern: Cache-Aside with Unbounded Thundering Herd
|
|
540
|
+
|
|
541
|
+
```typescript
|
|
542
|
+
// BAD: When cache expires, 10,000 concurrent requests hit PostgreSQL simultaneously
|
|
543
|
+
async function getUserProfile(id: string) {
|
|
544
|
+
const cached = await redis.get(`user:${id}`);
|
|
545
|
+
if (cached) return JSON.parse(cached);
|
|
546
|
+
const user = await db.user.findUnique({ where: { id } });
|
|
547
|
+
await redis.set(`user:${id}`, JSON.stringify(user), 'EX', 300);
|
|
548
|
+
return user;
|
|
549
|
+
}
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
### Best practice: ContextOS Standard (Mutex Lock / Single-Flight Pattern)
|
|
553
|
+
|
|
554
|
+
```typescript
|
|
555
|
+
// GOOD: Only one worker fetches from DB on cache miss; others wait
|
|
556
|
+
import { singleflight } from './singleflight';
|
|
557
|
+
|
|
558
|
+
async function getUserProfile(id: string) {
|
|
559
|
+
const cached = await redis.get(`user:${id}`);
|
|
560
|
+
if (cached) return JSON.parse(cached);
|
|
561
|
+
|
|
562
|
+
return singleflight.do(`user:${id}`, async () => {
|
|
563
|
+
const fresh = await redis.get(`user:${id}`);
|
|
564
|
+
if (fresh) return JSON.parse(fresh);
|
|
565
|
+
|
|
566
|
+
const user = await db.user.findUnique({ where: { id } });
|
|
567
|
+
if (user) {
|
|
568
|
+
await redis.set(`user:${id}`, JSON.stringify(user), 'EX', 300);
|
|
569
|
+
}
|
|
570
|
+
return user;
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
---
|
|
576
|
+
|
|
577
|
+
## Example 2: Outbox Pattern for Distributed Consistency
|
|
578
|
+
|
|
579
|
+
### Anti-pattern: Dual-Write Anti-pattern (Direct DB write + Kafka publish)
|
|
580
|
+
|
|
581
|
+
```typescript
|
|
582
|
+
// BAD: If Kafka publish fails, DB change is committed but event is lost forever
|
|
583
|
+
async function createOrder(data: OrderInput) {
|
|
584
|
+
const order = await db.order.create({ data });
|
|
585
|
+
await kafkaProducer.send({ topic: 'orders', messages: [{ value: JSON.stringify(order) }] });
|
|
586
|
+
return order;
|
|
587
|
+
}
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
### Best practice: ContextOS Standard (Transactional Outbox)
|
|
591
|
+
|
|
592
|
+
```typescript
|
|
593
|
+
// GOOD: Order and Outbox record committed in a single atomic DB transaction
|
|
594
|
+
async function createOrder(data: OrderInput) {
|
|
595
|
+
return await db.$transaction(async (tx) => {
|
|
596
|
+
const order = await tx.order.create({ data });
|
|
597
|
+
await tx.outbox.create({
|
|
598
|
+
data: {
|
|
599
|
+
aggregateType: 'Order',
|
|
600
|
+
aggregateId: order.id,
|
|
601
|
+
eventType: 'OrderCreated',
|
|
602
|
+
payload: JSON.stringify(order),
|
|
603
|
+
status: 'PENDING',
|
|
604
|
+
},
|
|
605
|
+
});
|
|
606
|
+
return order;
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
<!-- Source: TROUBLESHOOTING.md -->
|
|
612
|
+
|
|
613
|
+
# system-design Troubleshooting & Common Mistakes
|
|
614
|
+
|
|
615
|
+
## 1. Serverless Connection Exhaustion
|
|
616
|
+
|
|
617
|
+
- **Symptom**: "FATAL: remaining connection slots are reserved for non-replication superuser connections" under modest traffic.
|
|
618
|
+
- **Root Cause**: Serverless/Edge functions opening new DB connection pools per invoked instance.
|
|
619
|
+
- **Fix**: Use a connection pooler like PgBouncer or managed pooling (Supabase connection pool, AWS RDS Proxy, Prisma Accelerate).
|
|
620
|
+
|
|
621
|
+
## 2. Cache Invalidation Drift
|
|
622
|
+
|
|
623
|
+
- **Symptom**: Users see stale, outdated data after making updates.
|
|
624
|
+
- **Root Cause**: Updates to database do not invalidate related cache keys, or TTLs are set to infinite.
|
|
625
|
+
- **Fix**: Invalidate cache keys explicitly on write in the same transactional flow, and always set defensive TTLs.
|
|
626
|
+
|
|
627
|
+
## 3. Lack of Rate Limiting and Backpressure
|
|
628
|
+
|
|
629
|
+
- **Symptom**: Backend crashes or slows to a crawl during traffic spikes or bot scraping.
|
|
630
|
+
- **Root Cause**: Unthrottled public endpoints without token-bucket or sliding-window rate limiting.
|
|
631
|
+
- **Fix**: Add rate-limiting middleware (Redis-backed sliding window) at the API gateway / Edge layer.
|