How would you design a database schema for a multi-tenant application to ensure data isolation?

Instruction: Outline a database schema design strategy for a multi-tenant application, emphasizing data isolation between tenants.

Context: This question tests the candidate's understanding of multi-tenancy concepts and their ability to design secure, scalable, and isolated data structures.

Updated

Official answer

Choose shared tables, separate schemas or separate databases from the isolation, recovery, scale and operational requirements. Separate databases can reduce some failure scope but do not automatically prevent a routing or privileged-access mistake. Schemas alone are namespaces, not a complete security boundary.

In shared tables, include tenant identity in keys and relationships. For example, use a parent key (tenant_id, project_id) and a child foreign key containing both columns; this prevents a task from referencing another tenant's project by project ID alone. Enforce required tenant values and business uniqueness within the intended scope.

Derive the tenant context from authenticated authorization, not an untrusted request field. Restrict database roles and use an appropriate row-security policy as another layer. PostgreSQL table owners and privileged roles can bypass ordinary row security, so test as the actual application role and understand FORCE ROW LEVEL SECURITY where applicable.

Test cross-tenant reads, writes, joins, background jobs, exports and pooled-connection reuse. Include cache/object-storage keys and administrative access. Define provisioning, migrations, tenant-specific restore and deletion procedures; database design is only part of end-to-end isolation.

Related Questions