2.7 KiB
2.7 KiB
SaaS Architecture Patterns
This reference outlines common architecture choices for SaaS products.
Multi-Tenant Architecture
Shared Database, Shared Schema
- Tenant isolation via
tenant_idcolumns. - Lowest operational overhead.
- Requires strict row-level authorization.
Shared Database, Separate Schema
- Per-tenant schema boundaries.
- Better logical isolation.
- Higher migration and operations complexity.
Separate Database Per Tenant
- Strongest isolation and compliance posture.
- Best for enterprise/high-regulatory environments.
- Highest cost and operational burden.
Tenant Isolation Checklist
- Enforce tenant filters in all read/write queries.
- Validate authorization at API and data layers.
- Audit logs include tenant context.
- Backups and restores preserve tenant boundaries.
Authentication Patterns
JWT-Based Session Pattern
- Stateless access tokens.
- Use short-lived access tokens + refresh tokens.
- Rotate signing keys with versioning (
kidusage).
OAuth 2.0 / OIDC Pattern
- Preferred for SSO and enterprise identity.
- Support common providers (Google, Microsoft, Okta).
- Map identity claims to internal roles and tenants.
Hybrid Auth Pattern
- Email/password for SMB self-serve.
- SSO/OAuth for enterprise accounts.
Billing Integration Patterns
Subscription Lifecycle
- Trial start
- Conversion to paid plan
- Renewal and invoice events
- Grace period / dunning
- Downgrade, cancellation, reactivation
Billing Event Handling
- Process webhook events idempotently.
- Verify provider signatures.
- Persist raw event payload for audit/debugging.
- Reconcile billing state asynchronously.
Entitlement Model
- Separate billing plans from feature entitlements.
- Resolve effective entitlements per tenant/user at request time.
API Versioning Patterns
URI Versioning
/api/v1/...,/api/v2/...- Explicit and easy to route.
Header Versioning
- Version via request header.
- Cleaner URLs, more client coordination required.
Versioning Rules
- Avoid breaking changes inside a version.
- Provide deprecation windows and migration docs.
- Track version adoption per client.
Database Schema Patterns for SaaS
Core Entities
tenantsusersmemberships(user-tenant-role mapping)planssubscriptionsinvoicesevents_audit
Recommended Relationship Pattern
tenants1:Nmembershipsusers1:Nmembershipstenants1:1 activesubscriptionssubscriptions1:Ninvoices
Data Model Guardrails
- Unique constraints on tenant-scoped natural keys.
- Soft-delete where recoverability matters.
- Created/updated timestamps on all mutable entities.
- Migration strategy supports zero-downtime changes.