Designing Multi-Tenant SaaS Architecture: 100 to 10,000 Tenants Without Data Leaks
Comparing Database-per-Tenant, Schema-per-Tenant, and Shared Database with Tenant Discriminator: connection pooling, tenant isolation, and migrations.
By Amr Samir• August 22, 2026• 1 min
Designing Multi-Tenant SaaS Architecture: 100 to 10,000 Tenants Without Data Leaks
1. What Is Multi-Tenancy?
A Software-as-a-Service (SaaS) application serves multiple business customers (tenants) from a shared infrastructure. The primary engineering challenge is guaranteeing strict data isolation and cost efficiency across scaling boundaries.
code
+----------------------------------------------------------------------+
| 3 Multi-Tenancy Database Models |
+----------------------------------------------------------------------+
| 1. Database-per-Tenant | Separate DB for every customer (High cost) |
| 2. Schema-per-Tenant | Shared DB, isolated Postgres schemas |
| 3. Shared Database | Shared tables with TenantId column (Low cost|
+----------------------------------------------------------------------+
2. Comparing Multi-Tenancy Strategies
code
+--------------------+----------------+------------------+-------------------+
| Feature | DB-per-Tenant | Schema-per-Tenant| Shared Table |
+--------------------+----------------+------------------+-------------------+
| Data Isolation | Maximum (Hard) | High (Logical) | Logical (Software)|
| Cost Efficiency | Low | Medium | Maximum |
| Scale Boundary | ~ 100 tenants | ~ 1,000 tenants | 10,000+ tenants |
| DB Migrations | Very Slow | Slow | Fast (Single Run) |
+--------------------+----------------+------------------+-------------------+
3. Implementing Safe Shared-Table Isolation with Prisma / NestJS Middleware
typescript
import { PrismaClient } from '@prisma/client';
export function createTenantPrismaClient(tenantId: string) {
const prisma = new PrismaClient();
return prisma.$extends({
query: {
$allModels: {
async $allOperations({ model, operation, args, query }) {
// Automatically inject tenantId into all queries
if (['findMany', 'findFirst', 'count'].includes(operation)) {
args.where = { ...args.where, tenantId };
}
if (['create'].includes(operation)) {
args.data = { ...args.data, tenantId };
}
return query(args);
},
},
},
});
}
4. Critical Security Failures in SaaS Systems
- Cache Pollution: Always namespace cache keys with tenant ID (
tenant:${tenantId}:user:${userId}). - Background Jobs: Always preserve and pass the
tenantIdin message queue payloads. - Client-Side Tenant Tampering: Never trust
tenantIdsubmitted from URL query params or request bodies without verifying against the authenticated JWT session.
5. Summary
- Default to Shared Database with Tenant Discriminator for cost efficiency and scalability.
- Enforce tenant boundaries at the ORM / query layer automatically.
- Provide dedicated databases only for Enterprise-tier tenants with strict regulatory compliance requirements.
Recommended Posts
Related Projects
Skillora - Full-Stack LMS & Video Course Platform
A comprehensive learning management portal built with Express, React, and MongoDB, featuring signed Cloudinary video streaming URLs, nested curriculum models, student progress tracking, and PayPal course enrollments.