Core Positioning
SaaS Multi-Tenancy allows you to serve multiple enterprise clients with a single codebase, where each client (tenant) can only see their own data, completely isolated from each other. Whether building a vertical SaaS product or managing multiple subsidiaries within an enterprise, this is an indispensable capability.
Two approaches: Column-based isolation (lightweight, suitable for small-to-medium scale) and database isolation (physical isolation, suitable for large-scale SaaS).
Suitable Users
| Scenario | Recommended Approach | Reason |
|---|---|---|
| SaaS Startup Phase | Column-based isolation | Simple operations, one database handles everything |
| Large SaaS Platform | Database isolation | Physical isolation, high security, independent backups |
| Enterprise Multi-Company | Column-based isolation | Limited number of companies, logical isolation is sufficient |
| Finance/Healthcare Compliance | Database isolation | Data must be physically isolated |
Two Approaches Comparison
same database, same tables"] C2["Each table adds
tenant_id column"] C3["SQL Interceptor
auto-appends WHERE tenant_id = ?"] C4["Pros: Simple operations, high resource utilization
Cons: Logical isolation, moderate security"] C1 --> C2 --> C3 --> C4 end subgraph DATASOURCE["Approach 2: Database Isolation (DATASOURCE Mode)"] direction TB D1["Master DB: Shared tables
Users/Menus/Config/Tenant info"] D2["Tenant DB A: Business tables"] D3["Tenant DB B: Business tables"] D4["Tenant DB C: Business tables"] D5["Pros: Physical isolation, high security, independent backups
Cons: Complex operations, lower resource utilization"] D1 --> D2 D1 --> D3 D1 --> D4 D2 & D3 & D4 --> D5 end
Detailed Comparison
| Dimension | Column-Based (COLUMN) | Database Isolation (DATASOURCE) |
|---|---|---|
| Isolation Level | Logical isolation | Physical isolation |
| Security | Medium (relies on SQL interceptor) | High (database-level isolation) |
| Resource Utilization | High (shared connection pool) | Low (independent connections per tenant) |
| Operations Complexity | Low (single DB operations) | High (multi-DB operations) |
| Scalability | Limited by single DB performance | Horizontally scalable |
| Data Backup | Need to filter by tenant_id | Independent backup per tenant |
| Applicable Scale | Hundreds of tenants | Thousands/Tens of thousands of tenants |
| Technical Implementation | MyBatis Plus interceptor | dynamic-datasource |
Column-Based Isolation Implementation
WHERE tenant_id = 'A' DB-->>APP: Returns only Tenant A's data APP-->>U: Display customer list
Core Mechanism: Through MyBatis Plus’s multi-tenant plugin, automatically adds tenant_id condition before executing any SQL, completely transparent to business code.
Database Isolation Implementation
Based on dynamic-datasource dynamic data sources, when a request comes in, it switches to the corresponding data source based on the current tenant ID:
spring:
datasource:
dynamic:
primary: master # Master DB (shared tables)
datasource:
master: # Master DB config
url: jdbc:mysql://localhost:3306/ruoyi_master
tenant_1: # Tenant 1's independent database
url: jdbc:mysql://localhost:3306/ruoyi_tenant_1
tenant_2: # Tenant 2's independent database
url: jdbc:mysql://localhost:3306/ruoyi_tenant_2
Tenant Package Management
System Management + Basic Features"] PACKAGE2["Professional Package
Basic + CRM + ERP"] PACKAGE3["Enterprise Package
All Features"] TenantA["Tenant A"] --> PACKAGE1 TenantB["Tenant B"] --> PACKAGE2 TenantC["Tenant C"] --> PACKAGE3
Each tenant can be bound to a package, which defines the menus and features visible to that tenant. Supports automatic disabling upon package expiration.