Multi Tenancy Saas
Skill Profile
(Select at least one profile to enable specific modules)
Overview
Multi-tenancy lets one product serve many customers safely. The hard parts are enforcing isolation everywhere (API, DB, cache, jobs), preventing noisy-neighbor issues, and keeping operations (migrations, billing, support) tenant-aware.
Why This Matters
- Security: prevent cross-tenant data leaks (the #1 existential risk for SaaS)
- Scalability: serve thousands of tenants without exploding ops overhead
- Cost efficiency: shared infra with fair resource allocation
- Velocity: one codebase and deployment model, still customizable per tenant
Core Concepts & Rules
1. Core Principles
- Follow established patterns and conventions
- Maintain consistency across codebase
- Document decisions and trade-offs
2. Implementation Guidelines
- Start with the simplest viable solution
- Iterate based on feedback and requirements
- Test thoroughly before deployment
Inputs / Outputs / Contracts
- Inputs:
- <e.g., env vars, request payload, file paths, schema>
- Entry Conditions:
- <Pre-requisites: e.g., Repo initialized, DB running, specific branch checked out>
- Outputs:
- <e.g., artifacts (PR diff, docs, tests, dashboard JSON)>
- Artifacts Required (Deliverables):
- <e.g., Code Diff, Unit Tests, Migration Script, API Docs>
- Acceptance Evidence:
- <e.g., Test Report (screenshot/log), Benchmark Result, Security Scan Report>
- Success Criteria:
- <e.g., p95 < 300ms, coverage ≥ 80%>
Skill Composition
- Depends on: None
- Compatible with: None
- Conflicts with: None
- Related Skills: None
Quick Start
Assumptions
- Multi-tenant SaaS application architecture
- Relational database with support for row-level security (PostgreSQL recommended)
- Authentication system that can validate tenant membership
- Billing/subscription system for entitlement management
- Cache layer (Redis) for tenant-aware caching
Compatibility
- PostgreSQL: 12+ (for RLS features)
- Node.js: 16+ (for TypeScript examples)
- Redis: 6+ (for tenant-aware caching)
- Stripe: Latest API version
- ORMs: Prisma 4+, TypeORM 0.3+, Sequelize 6+
Test Scenario Matrix
| Scenario |
Input |
Expected Output |
Verification |
| Tenant isolation |
Query with tenant_id |
Returns only tenant data |
DB query inspection |
| Cross-tenant access |
Query with wrong tenant_id |
Empty result |
API response check |
| Rate limit enforcement |
Exceed tenant quota |
429 Too Many Requests |
Load test |
| Billing event |
Usage event |
Idempotent ledger entry |
Event deduplication |
| Tenant provisioning |
New tenant data |
Tenant created + admin user |
Smoke test |
| Tenant offboarding |
Offboard tenant |
Data deleted/exported |
Retention check |
Technical Guardrails
Agent Directives & Error Recovery
(ข้อกำหนดสำหรับ AI Agent ในการคิดและแก้ปัญหาเมื่อเกิดข้อผิดพลาด)
- Thinking Process: Analyze root cause before fixing. Do not brute-force.
- Fallback Strategy: Stop after 3 failed test attempts. Output root cause and ask for human intervention/clarification.
- Self-Review: Check against Guardrails & Anti-patterns before finalizing.
- Output Constraints: Output ONLY the modified code block. Do not explain unless asked.
Definition of Done
A multi-tenancy feature is complete when:
Anti-patterns
- "Filter in app only": no DB enforcement; one missed
tenant_id filter becomes a breach
- Shared resources without limits: one tenant degrades everyone
- Global caches: cached objects without tenant namespace
- Tenant-unaware jobs: background workers processing cross-tenant data accidentally
- Admin bypass: admin tools that skip tenant scoping
- Mixed logs: logs without tenant context making debugging impossible
- Global migrations: schema changes that don't consider per-tenant impact
- Customization as code: divergent code paths instead of configuration
Reference Links
Versioning & Changelog
- Version: 1.0.0
- Changelog:
- 2026-02-22: Initial version with complete template structure
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: multi-tenancy-saas3description: Multi-tenancy lets one product serve many customers safely. The hard Use when this capability is needed.4---56# Multi Tenancy Saas78## Skill Profile9*(Select at least one profile to enable specific modules)*10- [ ] **DevOps**11- [x] **Backend**12- [ ] **Frontend**13- [ ] **AI-RAG**14- [ ] **Security Critical**1516## Overview17Multi-tenancy lets one product serve many customers safely. The hard parts are enforcing isolation everywhere (API, DB, cache, jobs), preventing noisy-neighbor issues, and keeping operations (migrations, billing, support) tenant-aware.1819## Why This Matters20- **Security**: prevent cross-tenant data leaks (the #1 existential risk for SaaS)21- **Scalability**: serve thousands of tenants without exploding ops overhead22- **Cost efficiency**: shared infra with fair resource allocation23- **Velocity**: one codebase and deployment model, still customizable per tenant2425## Core Concepts & Rules2627### 1. Core Principles28- Follow established patterns and conventions29- Maintain consistency across codebase30- Document decisions and trade-offs3132### 2. Implementation Guidelines33- Start with the simplest viable solution34- Iterate based on feedback and requirements35- Test thoroughly before deployment363738## Inputs / Outputs / Contracts39* **Inputs**:40 - <e.g., env vars, request payload, file paths, schema>41* **Entry Conditions**:42 - <Pre-requisites: e.g., Repo initialized, DB running, specific branch checked out>43* **Outputs**:44 - <e.g., artifacts (PR diff, docs, tests, dashboard JSON)>45* **Artifacts Required (Deliverables)**:46 - <e.g., Code Diff, Unit Tests, Migration Script, API Docs>47* **Acceptance Evidence**:48 - <e.g., Test Report (screenshot/log), Benchmark Result, Security Scan Report>49* **Success Criteria**:50 - <e.g., p95 < 300ms, coverage ≥ 80%>5152## Skill Composition53* **Depends on**: None54* **Compatible with**: None55* **Conflicts with**: None56* **Related Skills**: None5758## Quick Start59#6061## Assumptions62- Multi-tenant SaaS application architecture63- Relational database with support for row-level security (PostgreSQL recommended)64- Authentication system that can validate tenant membership65- Billing/subscription system for entitlement management66- Cache layer (Redis) for tenant-aware caching6768## Compatibility69- **PostgreSQL**: 12+ (for RLS features)70- **Node.js**: 16+ (for TypeScript examples)71- **Redis**: 6+ (for tenant-aware caching)72- **Stripe**: Latest API version73- **ORMs**: Prisma 4+, TypeORM 0.3+, Sequelize 6+7475## Test Scenario Matrix76| Scenario | Input | Expected Output | Verification |77|----------|-------|-----------------|--------------|78| Tenant isolation | Query with tenant_id | Returns only tenant data | DB query inspection |79| Cross-tenant access | Query with wrong tenant_id | Empty result | API response check |80| Rate limit enforcement | Exceed tenant quota | 429 Too Many Requests | Load test |81| Billing event | Usage event | Idempotent ledger entry | Event deduplication |82| Tenant provisioning | New tenant data | Tenant created + admin user | Smoke test |83| Tenant offboarding | Offboard tenant | Data deleted/exported | Retention check |8485## Technical Guardrails86#8788## Agent Directives & Error Recovery89*(ข้อกำหนดสำหรับ AI Agent ในการคิดและแก้ปัญหาเมื่อเกิดข้อผิดพลาด)*9091- **Thinking Process**: Analyze root cause before fixing. Do not brute-force.92- **Fallback Strategy**: Stop after 3 failed test attempts. Output root cause and ask for human intervention/clarification.93- **Self-Review**: Check against Guardrails & Anti-patterns before finalizing.94- **Output Constraints**: Output ONLY the modified code block. Do not explain unless asked.959697## Definition of Done98A multi-tenancy feature is complete when:99100- [ ] Tenant isolation enforced at database layer (RLS or equivalent)101- [ ] All repositories require tenant context102- [ ] Caches are namespaced by tenant ID103- [ ] Queues include tenant context104- [ ] Rate limits and quotas are per-tenant105- [ ] Billing events are tenant-scoped and idempotent106- [ ] Onboarding/offboarding are automated107- [ ] Logs include tenant ID for debugging108- [ ] Admin tools require explicit tenant context109- [ ] Security review passed for isolation patterns110111## Anti-patterns1121. **"Filter in app only"**: no DB enforcement; one missed `tenant_id` filter becomes a breach1132. **Shared resources without limits**: one tenant degrades everyone1143. **Global caches**: cached objects without tenant namespace1154. **Tenant-unaware jobs**: background workers processing cross-tenant data accidentally1165. **Admin bypass**: admin tools that skip tenant scoping1176. **Mixed logs**: logs without tenant context making debugging impossible1187. **Global migrations**: schema changes that don't consider per-tenant impact1198. **Customization as code**: divergent code paths instead of configuration120121## Reference Links122- [Multi-Tenant SaaS Patterns (AWS)](https://aws.amazon.com/partners/programs/saas/)123- [Azure Multi-Tenant Guidance](https://learn.microsoft.com/azure/architecture/guide/multitenant/)124- [PostgreSQL Row Level Security](https://www.postgresql.org/docs/current/ddl-rowsecurity.html)125- [Stripe Billing Best Practices](https://stripe.com/docs/billing/best-practices)126- [SaaS Architecture Patterns](https://martinfowler.com/articles/multi-tenancy.html)127128## Versioning & Changelog129130* **Version**: 1.0.0131* **Changelog**:132 - 2026-02-22: Initial version with complete template structure133134---135> Converted and distributed by [TomeVault](https://tomevault.io/claim/amnadtaowsoam) — claim your Tome and manage your conversions.136<!-- tomevault:4.0:skill_md:2026-04-13 -->