Prompt
How do I handle tenant-level config in a multi-tenant SaaS app?
Latest observation
A good pattern is to treat tenant-level config as a first-class domain object with clear precedence, validation, and safe defaults.
1) Define config layers and precedence
Use a predictable hierarchy, for example:
- System defaults
- Plan-level defaults (e.g. Free / Pro / Enterprise)
- Tenant-level overrides
- User-level preferences if needed
Make the resolution rule explicit, e.g. “most specific wins.”
2) Store config per tenant, not in code
For dynamic settings, keep them in a database table or config service keyed by tenant_id.
Typical shape:
tenant_idkeyvalueupdated_atupdated_by- optionally
version,scope,description
Examples:
billing.currency = "USD"features.beta_dashboard = truelimits.max_projects = 50
3) Separate “feature flags” from “business config”
They solve different problems:
- Feature flags: enable/disable behavior, gradual rollout, experiments
- Business config: tenant-specific rules, thresholds, branding, integrations
You can keep them in the same platform, but model them separately.
4) Validate config with schemas
Don’t let arbitrary JSON drift into production without guardrails.
Use:
- JSON Schema, Pydantic, Zod, Joi, etc.
- Type validation
- Allowed ranges/enums
- Required/optional fields
This avoids bad values like max_users = -1 or invalid URLs.
5) Cache carefully
Tenant config is often read on every request, so cache it:
- in-memory per app instance
- Redis/shared cache
- with TTL or version-based invalidation
But ensure updates propagate quickly enough for your use case.
6) Make config loading cheap
Common pattern:
- Load tenant config once at request start
- Attach it to request context
- Reuse throughout the request
Avoid repeated DB calls inside every service method.
7) Support auditability
Because config changes can affect behavior:
- track who changed what and when
- keep history/versioning
- allow rollback if possible
This is especially important for billing, permissions, and workflow settings.
8) Design for defaults and missing values
Always define:
- what happens if a key is absent
- what the safe fallback is
- whether a missing config is an error
Don’t assume every tenant has fully populated settings.
9) Consider tenant isolation
If tenants can have sensitive config:
- ensure strict tenant scoping in queries
- avoid cross-tenant cache leakage
- encrypt secrets at rest
- never expose raw secrets in logs or APIs
10) A practical implementation approach
A common setup:
tenant_settingstable for structured configfeature_flagstable/service for rollout flags- a config resolver that merges defaults + overrides
- schema validation at write time
- cache on read
- audit log on change
Example resolution flow
If a tenant requests email_from_address:
- Check tenant override
- If absent, check plan default
- If absent, use system default
- Validate the final value before use
If you want, I can also give you:
- a database schema
- a code example in Node/Python/Java
- or a recommended architecture for small vs large SaaS apps.