The Need for Physical Isolation
In high-concurrency enterprise SaaS applications, separating customer data is not just a feature—it is a core compliance and security mandate. While logical isolation (filtering queries by a tenantId) is common, physical isolation (assigning each tenant their own database instance) provides superior safety.
Here is a comparison of isolation strategies:
| Isolation Type | Security Level | Performance Overhead | Cost Efficiency |
|---|---|---|---|
| Logical (Shared DB) | Moderate | Medium | High |
| Physical (Database-per-tenant) | Maximum | Low (Dedicated Pools) | Medium |
Core Benefits of Physical Databases
- Compliance Gaps Closed: Strict regulatory standards (like HIPAA or GDPR) often require physical data residency boundaries.
- No Noisy Neighbors: Resource contention is eliminated since query pipelines do not share compute threads.
- Custom Backups: Restore or migration policies can be operated per tenant without affecting the main cluster.
[!WARNING] While database-per-tenant architectures maximize safety, they increase devops orchestrations. Always use a centralized database manager to automate provisioning.
Dynamic Tenant DB Routing
To resolve the correct database node dynamically, we leverage a dynamic Prisma client manager wrapper:
import { TenantClient } from '@prisma/client/tenant';
import { Pool } from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
const tenantClients = new Map<string, TenantClient>();
export function getTenantDb(dbUrl: string): TenantClient {
if (tenantClients.has(dbUrl)) {
return tenantClients.get(dbUrl)!;
}
const pool = new Pool({ connectionString: dbUrl });
const adapter = new PrismaPg(pool);
const client = new TenantClient({ adapter });
tenantClients.set(dbUrl, client);
return client;
}
By loading the tenant credentials from central routing scopes, the platform switches connection streams seamlessly.