Shard

From seaworld008/Commonly-used-high-value-skills · View interactive version

Shard is an AI agent skill from the GitHub repository seaworld008/Commonly-used-high-value-skills. Designing multi-tenant architectures with tenant isolation strategies, RLS, routing, and scale design for SaaS. Use when designing multi-tenant SaaS systems or tenant isolation.

How to use this skill

Skill file

<!--

CAPABILITIES_SUMMARY:

COLLABORATION_PATTERNS:

BIDIRECTIONAL_PARTNERS:

PROJECT_AFFINITY: Game(L) SaaS(H) E-commerce(M) Dashboard(M) Marketing(L)

-->

Shard

Design multi-tenant architectures. Shard turns SaaS requirements into tenant isolation strategies, RLS policies, routing designs, noisy-neighbor protections, and migration plans.

Trigger Guidance

Use Shard when the user needs:

Route elsewhere when the task is primarily:

Core Contract

Boundaries

_common/ references require the separately installed upstream ecosystem. Use them only when available and selected for this task; otherwise follow host instructions and the domain workflow.

Agent role boundaries -> _common/BOUNDARIES.md

Always

Ask First When Not Already Authorized

Never

Recipes

| Recipe | Subcommand | Default? | When to Use | Read First |

|--------|-----------|---------|-------------|------------|

| Isolation Strategy | isolation | ✓ | Tenant isolation strategy design (DB / schema / row-level comparison) | reference/patterns.md |

| RLS Design | rls | | Row Level Security policy design and tenant context propagation | reference/patterns.md |

| Tenant Routing | routing | | Tenant routing design (subdomain / header / path) | reference/patterns.md |

| Scale Design | scale | | Noisy-neighbor protection, resource limits, and migration planning | reference/patterns.md |

| Tenant Migration | migration | | Cross-shard rebalancing, isolation-level upgrade, zero-downtime tenant moves | reference/tenant-migration.md |

| Tenant Provisioning | provisioning | | Tenant lifecycle, IaC-driven onboarding, idempotent re-provisioning, deprovisioning + retention | reference/tenant-provisioning.md |

| Tenant Quota | quota | | Per-tenant rate limits, fair-share scheduling, soft/hard quota, burst budgets, overage handoff | reference/tenant-quota-throttling.md |

Subcommand Dispatch

Parse the first token of user input.

Subcommand Behavior Notes

Output Routing

| Signal | Approach | Primary output | Read next |

|--------|----------|----------------|-----------|

| multi-tenant, SaaS, tenant | Full isolation strategy design | Architecture doc + RLS spec | reference/patterns.md |

| RLS, row level security | RLS policy design | Policy spec + migration SQL | reference/patterns.md |

| routing, subdomain, tenant resolution | Tenant routing design | Routing spec + middleware design | reference/patterns.md |

| noisy neighbor, rate limit, fair | Resource isolation design | Limit spec + monitoring plan | reference/patterns.md |

| migration, single to multi | Migration strategy | Migration plan + risk assessment | reference/patterns.md |

| billing, metering, usage | Billing integration design | Metering spec + event design | reference/patterns.md |

| security, data leak, isolation check | Data leakage assessment | Risk report + guardrail design | reference/patterns.md |

| unclear request | Full isolation strategy (default) | Architecture doc | reference/patterns.md |

Workflow

ASSESS -> STRATEGY -> DESIGN -> VERIFY -> DOCUMENT

| Phase | Required action | Key rule | Read |

|-------|-----------------|----------|------|

| ASSESS | Analyze scale, compliance, cost constraints, existing schema | Understand current state before designing future state | — |

| STRATEGY | Evaluate isolation levels and recommend with tradeoffs | Compare all 3 levels; include cost and complexity analysis | reference/patterns.md |

| DESIGN | Design RLS, routing, context propagation, resource limits | RLS must fail closed; context must flow end-to-end | reference/patterns.md |

| VERIFY | Assess data leakage vectors and test strategies | Every design gets a leakage checklist | reference/patterns.md |

| DOCUMENT | Produce architecture doc with migration path | Include diagrams, SQL examples, and monitoring plan | — |

Isolation Strategy Matrix

| Strategy | Tenant scale | Data isolation | Cost | Complexity | Compliance |

|----------|-------------|---------------|------|------------|------------|

| Database-per-tenant | 1-100 | Strongest | High | Medium | HIPAA/PCI-DSS/EU-AI-Act ready; use Neon project-per-tenant for serverless scale |

| Schema-per-tenant | 10-1,000 | Strong | Medium | Medium-High | SOC2 ready; Citus 13 schema-based sharding for write scale |

| Row-level (RLS) | 100-100,000+ | Moderate | Low | Low-Medium | Needs careful design; index tenant column; use security_invoker views (PG 15+) |

| Hybrid | Varies | Configurable | Medium | High | Per-tier compliance; dominant pattern in mature SaaS 2025+ |

Hybrid tenancy is the dominant pattern in mature SaaS (2025+): standard-tier tenants share pooled row-level infrastructure while enterprise tenants with compliance or heavy workload requirements get isolated schemas or dedicated databases. This optimizes unit economics for volume segments while meeting enterprise procurement requirements.

Neon project-per-tenant is now a viable database-per-tenant option for high-isolation requirements: each customer gets a dedicated Neon project (isolated Postgres instance) managed via the Neon API, with copy-on-write branching for dev/staging — Neon manages 300K+ such databases in production. Particularly suited for HIPAA-regulated SaaS. Source: Neon — Multitenancy, How Neon Solves HIPAA Compliance, Multi-Tenancy, and Scaling for B2B SaaS

Data residency and transfers: Identify the affected data, tenant regions, permitted processing and transfer locations, provider access and contractual restrictions. Select placement and isolation against customer-confirmed requirements. Verify any applicable legal dates or amounts against official sources for the target year before recording them.

Decision Factors

| Factor | Favors DB-per-tenant | Favors Schema | Favors RLS |

|--------|---------------------|---------------|------------|

| Tenant count | < 100 | 10 - 1,000 | 1,000+ |

| Data sensitivity | Regulated (HIPAA) | Moderate | Standard |

| Customization need | High per-tenant | Moderate | Low |

| Operational budget | Large | Medium | Small |

| Query complexity | Cross-tenant analytics rare | Moderate | Cross-tenant queries common |

Tenant Context Propagation

Request → [Auth Middleware] → tenant_id extracted
  → [Request Context] → tenant_id set
    → [Service Layer] → tenant_id passed
      → [Repository/ORM] → tenant_id in WHERE/RLS
        → [Database] → query scoped to tenant

Key design points:

Output Requirements

Collaboration

Receives: Schema (DB design), Gateway (API design), User (requirements), Atlas (architecture analysis)

Sends: Schema (RLS implementation), Scaffold (infra config), Builder (implementation), Sentinel (security review)

| Direction | Handoff | Purpose |

|-----------|---------|---------|

| Schema → Shard | SCHEMA_TO_SHARD_HANDOFF | DB design context for isolation |

| Gateway → Shard | GATEWAY_TO_SHARD_HANDOFF | API routing context |

| Shard → Schema | SHARD_TO_SCHEMA_HANDOFF | RLS policies for implementation |

| Shard → Sentinel | SHARD_TO_SENTINEL_HANDOFF | Data leakage assessment for review |

Reference Map

| Reference | Read this when |

|-----------|----------------|

| reference/patterns.md | You need isolation patterns, RLS examples, routing designs, or leakage checklists. |

| reference/examples.md | You need complete multi-tenant architecture examples. |

| reference/handoffs.md | You need handoff templates for collaboration with other agents. |

| reference/tenant-migration.md | You are running migration — cross-shard rebalancing, isolation-level upgrades, dual-write+cutover or offline-copy modes, verification queries, rollback playbooks. |

| reference/tenant-provisioning.md | You are running provisioning — tenant lifecycle state machine, idempotent IaC-driven onboarding, default-data seeding, deprovisioning + GDPR retention rules. |

| reference/tenant-quota-throttling.md | You are running quota — token/leaky bucket selection, fair-share scheduler choice, soft/hard quota policy, burst budget tuning, overage-billing handoff. |

| reference/autorun-schema.md | You are emitting the AUTORUN _STEP_COMPLETE block — Shard-specific Output/Next schema. |

Operational

AUTORUN Support

See _common/AUTORUN.md for the protocol (_AGENT_CONTEXT input, mode semantics, error handling). Shard-specific _STEP_COMPLETE.Output schema lives in reference/autorun-schema.md.

Nexus Hub Mode

When input contains ## NEXUS_ROUTING, return via ## NEXUS_HANDOFF (canonical schema in _common/HANDOFF.md).

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.