Worked design: multi-tenant SaaS analytics
Multi-tenant SaaS analytics
A B2B SaaS product (about 4,000 customer companies, from 5-seat startups to 50,000-seat enterprises) wants an in-product analytics tab. Each customer sees dashboards about their own usage, with sub-second loads, fresh to within 15 minutes. A few enterprises want a daily export of their raw data to their own warehouse. One customer must never see another customer's data.
Design the data model, storage, serving layer and isolation.
Hint 1
Where is tenant isolation enforced, and what happens if one query forgets a filter?
Hint 2
Small and large tenants have very different volumes. Does one design fit both?
Show solution
Ingestion and model: product events land per tenant (tenant_id on every record) through the usual event pipeline. Models are built once with tenant_id as the leading key of every table, plus pre-aggregated tables at the grains the dashboards use (tenant × day × feature).
Serving: in-product dashboards need predictable sub-second latency and high concurrency, which is a poor fit for a general warehouse. Serve from a real-time OLAP store (ClickHouse, Druid, Pinot) or a cache of pre-aggregates, partitioned or sorted by tenant_id. An API layer between the product and the store injects the tenant from the authenticated session.
Isolation in depth: the tenant comes from the auth token, never from the request; the API adds the tenant filter itself; row-level security in the store enforces it again; and automated tests try cross-tenant access. Very large or regulated tenants can get dedicated tables or clusters (the "noisy neighbor" and compliance answer).
Exports: a per-tenant daily export to the customer's cloud storage, or warehouse data sharing, filtered by tenant, with its own audit log.
What a strong answer covers
- tenant_id as a first-class key in every table and pre-aggregates at dashboard grain (weight 2)
- Chooses a low-latency, high-concurrency serving store (OLAP/cache) behind an API (weight 2)
- Isolation in depth: tenant from auth, enforced by API and row-level security, tested (weight 3)
- Handles noisy neighbors and big tenants (dedicated resources, partitioning) (weight 2)
- Per-tenant exports or sharing with auditing (weight 1)
Follow-ups
- The product team wants each customer to define custom metrics. What changes?
Store metric definitions as data (a metric spec per tenant: measure, filters, dimensions), validated against a fixed set of allowed fields, and compile them to SQL with a semantic layer. Add query limits and caching, since custom metrics make query cost less predictable.
- How do you charge back serving cost per tenant?
Measure resource usage per tenant: tag queries with the tenant id, record credits or slot time and bytes scanned, add storage per tenant, and allocate shared costs proportionally. Use the result for pricing tiers or usage-based billing.
1. Requirements
- Tenants: about 4,000, with sizes spanning four orders of magnitude.
- Experience: dashboards load in under 1 second, data under 15 minutes old.
- Concurrency: many tenants' users at once. This is product traffic, not internal analysts.
- Isolation: zero tolerance for cross-tenant leaks.
- Exports: daily raw data to some enterprise customers' warehouses.
2. Data contracts
Every event and every row carries tenant_id, set by the product backend from the authenticated session, never from client input. Schemas are versioned, because customer-facing metrics must not change meaning silently.
3. Ingestion
Product events go through the standard event pipeline (Kafka → raw lake), and application database tables arrive via CDC. Micro-batches every few minutes keep the 15-minute freshness target.
4. Storage and format
- Modeled layer (lakehouse or warehouse): facts and dimensions with
tenant_idas the leading key in partitioning or clustering, so per-tenant reads are cheap. - Serving layer: pre-aggregated tables at the dashboard grains (tenant × day × feature, tenant × user × week) loaded into a real-time OLAP store (ClickHouse, Apache Druid or Apache Pinot), sorted or partitioned by
tenant_idthen time.
Why not query the warehouse directly? Warehouses are optimized for large scans by few users. In-product analytics needs predictable latency at high concurrency, which is what OLAP serving stores and caches are built for.
5. Transform
dbt models build the per-tenant aggregates incrementally. Metric definitions live in one place (a semantic layer or shared SQL), because customers compare these numbers with their own records and will notice inconsistencies.
6. Serving
product UI → analytics API (auth → tenant_id) → OLAP store (row policy: tenant_id = ?)
- The API derives
tenant_idfrom the auth token and adds it to every query. Clients never send it. - The store enforces it again with row-level security or per-tenant views, so a bug in one query can't leak data.
- Caching per tenant and query for hot dashboards.
7. Quality and security
- Automated cross-tenant tests: a test tenant tries to read another tenant's data through every endpoint.
- Audit logs for queries and exports.
- Reconciliation of key metrics against the application database per tenant (counts of users, projects).
8. Orchestration and backfill
Per-tenant backfills (for example after a bug for one customer) must be possible, so partitioning by tenant and date makes targeted rebuilds cheap. Exports run per tenant on a schedule, writing to the customer's storage (or through warehouse data sharing) with their own credentials and an audit record.
9. Failure modes
| Failure | Mitigation |
|---|---|
| Missing tenant filter in a new query | Enforced in the API and again by row-level security; cross-tenant tests in CI |
| Noisy neighbor (huge tenant slows others) | Quotas, query limits, dedicated tables/cluster for the largest tenants |
| Stale dashboards | Freshness shown in the UI; alerts on pipeline lag |
| Export sent to the wrong destination | Per-tenant destination config, verification, audit log |
10. Cost
Serving cost scales with concurrency and data per tenant. Pre-aggregation keeps it small. Measure cost per tenant (storage, queries, exports). It informs pricing tiers, and the largest tenants may justify a premium "dedicated" tier.
11. What changes at 10×
With 40,000 tenants or much larger ones, the "pool" model (shared tables) remains the default, with a "silo" tier (dedicated tables or clusters) for the biggest and regulated customers. Adding self-serve custom metrics would need a governed semantic layer and query cost limits per tenant.