Prompt
I'm building a SaaS product with tenant-specific metrics, what database architecture makes sense?
Latest observation
For a SaaS product with tenant-specific metrics, the right database architecture usually depends on three things:
- How many tenants you expect
- How strict your isolation/security requirements are
- How much query/analytics volume the metrics will generate
Here’s the practical breakdown.
Common architecture options
1) Shared database, shared schema
All tenants share the same tables, with a tenant_id column on each row.
Example
users(tenant_id, ...)events(tenant_id, metric_name, value, timestamp, ...)
Pros
- Cheapest and simplest to operate
- Easy to add new tenants
- Works well early on
- Good for multi-tenant OLTP workloads
Cons
- Risk of data leaks if queries aren’t carefully scoped
- Tenant-level backups/restores are harder
- Large tenants can create noisy-neighbor issues
- Indexing and performance tuning can get harder as scale grows
Best for
- Early-stage SaaS
- Many small/medium tenants
- Rapid product iteration
2) Shared database, separate schema per tenant
Each tenant gets its own schema in the same database.
Pros
- Better logical isolation than shared schema
- Easier per-tenant exports/migrations than row-based sharing
- Still relatively simple operationally
Cons
- Can become messy with lots of tenants
- Migrations across many schemas are painful
- Not ideal for very large tenant counts
Best for
- Smaller number of tenants
- Enterprise customers needing moderate isolation
- Products with strong per-tenant customization
3) Separate database per tenant
Each tenant has their own database instance or logical DB.
Pros
- Strong isolation
- Easier tenant-specific backup/restore
- Better for compliance and “premium” enterprise tiers
- No noisy-neighbor problem between tenants
Cons
- More complex operations
- Harder to run cross-tenant analytics
- More expensive
- More schema migration overhead
Best for
- Large enterprise customers
- Regulated environments
- Tenants with very large data volumes
4) Hybrid architecture
This is often the best long-term choice.
Typical pattern
- Core app data: shared database with
tenant_id - High-volume metrics/events: separate analytics store or partitioned event tables
- Large/enterprise tenants: optionally isolated DBs
Pros
- Balances cost and isolation
- Lets you optimize metrics separately from transactional data
- Scales better than a single all-purpose database
Cons
- More architectural complexity
- Requires careful data routing and observability
Best for
- Most SaaS products that expect growth
What I’d recommend for tenant-specific metrics
If your product has metrics that are time-series-like or event-heavy, I would usually recommend:
For the application data:
- Shared relational DB with
tenant_id - Proper indexing and row-level authorization
For metrics:
Choose based on volume:
If metrics volume is moderate:
- Use the same relational DB
- Add:
- composite indexes like
(tenant_id, metric_name, timestamp) - table partitioning by time if needed
- rollup tables for aggregates
- composite indexes like
If metrics volume is high:
- Store metrics in a dedicated analytics/time-series system
- PostgreSQL + partitioning can work up to a point
- For larger scale, consider:
- ClickHouse
- TimescaleDB
- BigQuery/Snowflake if primarily analytical
- InfluxDB or Prometheus-like stack if it’s operational metrics
If tenants need strict isolation:
- Consider hybrid:
- shared control plane
- per-tenant metric store or per-tenant DB for enterprise customers
Design principles to follow
1) Always include tenant_id
Even if you later move to isolated databases, keep tenant identity explicit in your data model.
2) Enforce tenant scoping at the DB layer if possible
Examples:
- PostgreSQL Row Level Security
- Views or stored procedures
- Repository/query helpers that always inject
tenant_id
3) Partition metrics by time and tenant if large
For example:
- partition by month/day
- index by
(tenant_id, timestamp)
4) Separate OLTP from analytics
Don’t let metric aggregation queries slow down your product database.
5) Plan for per-tenant export and deletion
Especially important for compliance and data residency.
Simple recommendation by stage
MVP / early startup
- One shared PostgreSQL database
tenant_idon every table- Metrics in same DB unless volume is high
- Add partitioning later if needed
Growth stage
- Keep shared core DB
- Move metrics/events to a dedicated store
- Add background aggregation and rollups
Enterprise / regulated
- Offer hybrid deployment:
- shared for small tenants
- dedicated DB or dedicated cluster for large tenants
A good default architecture
If you want a safe default:
- PostgreSQL for app data
- Shared schema + tenant_id
- Row Level Security
- Partitioned metrics table or separate metrics service
- Optional ClickHouse/TimescaleDB for analytics
This is usually the best balance of simplicity, cost, and scalability.
If you want, I can also sketch:
- a sample schema
- a decision matrix for choosing shared vs isolated DBs
- a Postgres + metrics architecture for your exact use case