Back to BlogEngineering

Designing a Multi-Tenant Database Schema for a SaaS Survey Platform

Alex RiveraApril 12, 202611 min read
Designing a Multi-Tenant Database Schema for a SaaS Survey Platform

Multi-tenancy is one of the foundational architectural decisions in any SaaS product, and it's much harder to change later than to get right early. Here's how we approach tenant isolation in our schema design.

Shared Schema, Row-Level Isolation

Rather than provisioning a separate database per customer (expensive to operate at scale) or a separate schema per customer (painful migrations), we use a shared schema with a tenant_id column on every table that needs isolation. Every query is required to filter by tenant_id, enforced at the ORM layer rather than left to developer discipline alone.

Enforcing Isolation at the Query Layer

Relying on every engineer to remember a WHERE tenant_id = ? clause is a recipe for an eventual data leak. We wrap all tenant-scoped queries in a repository layer that automatically injects the tenant filter, making it structurally difficult to accidentally query across tenants.

Indexing Strategy

Every tenant-scoped table has a composite index leading with tenant_id, which keeps queries fast even as total row counts grow into the hundreds of millions, since Postgres can narrow to a single tenant's rows before applying any other filter.

Handling Noisy Neighbors

A small number of high-volume tenants can degrade performance for everyone if they share the same database instance without safeguards. We monitor per-tenant query load and can migrate outlier tenants to dedicated read replicas or, in extreme cases, a dedicated database instance without any schema changes required.

Testing Isolation Rigorously

Given the severity of a tenant isolation failure, we maintain an automated test suite specifically designed to attempt cross-tenant data access through every API endpoint, running on every deployment. These tests create two tenants, populate them with data, and verify that every possible query path for tenant A returns zero rows belonging to tenant B — a deliberately adversarial testing approach rather than just testing the happy path.

When We'd Choose Differently

If we were building a platform with strict regulatory requirements demanding physical data separation (certain healthcare or government contracts), a database-per-tenant model would be worth the operational overhead. For a general-purpose SaaS survey platform, shared-schema multi-tenancy gives us the best balance of cost, simplicity, and scalability.

AR
Alex Rivera
AItocha Surveys