Multi-Tenancy
- In Turkish
- çok kiracılı mimari
- Pronunciation
- MUL-tee TEN-un-see
In short
Multi-tenancy is an architecture in which one running system serves many customers, called tenants, while keeping each tenant's data and settings separate.
What is multi-tenancy?
In a multi-tenant system, a single deployment of an application serves many customers at once. Each customer, called a tenant, is usually an organization such as a company or a team, with its own users, data and settings, and each sees the system as if it were theirs alone. Most SaaS products work this way, much like an apartment building where the tenants share the structure, plumbing and power but each has a locked door.
The key design decision is how tenants share data. With a database per tenant, sometimes called the silo model, each tenant gets its own database: strong isolation, but more to run. With a shared schema, the pool model, every table has a tenant_id column and every query must filter by it, which is the cheapest option and scales to thousands of small tenants. A middle path gives each tenant its own schema in a shared database, and many products mix models, moving their largest customers into dedicated silos.
The biggest risk is a leak between tenants: one query that forgets the tenant filter can show a company's data to another. Teams guard against it by working out the tenant once per request, from the signed-in account or the subdomain, and enforcing the filter in one central place, for example with PostgreSQL's row-level security. The second classic problem is the noisy neighbor, a tenant whose heavy usage slows everyone else down, which is handled with rate limits, quotas and by moving heavy tenants to their own resources.
Multi-tenancy is often equated with SaaS, but they answer different questions. SaaS is how software is sold and delivered, as an online subscription; multi-tenancy is one way to build it, and a SaaS product can also be single-tenant, running a separate instance for each customer, usually at a higher price. Multi-tenancy also exists outside SaaS: public clouds run many customers' virtual machines on shared hardware, and companies share one Kubernetes cluster between teams.
Key takeaways
- One deployment serves many tenants, each with isolated data and settings.
- Data can be split per database, per schema, or shared with a
tenant_idcolumn. - A missing tenant filter can leak data, so isolation is enforced centrally.
- Noisy neighbors are contained with rate limits, quotas and dedicated resources.
- SaaS is a delivery model; multi-tenancy is an architecture often used to build it.
Example
-- One shared table: every row records which tenant owns it
CREATE TABLE invoices (
id bigserial PRIMARY KEY,
tenant_id uuid NOT NULL,
amount numeric NOT NULL
);
-- The database adds the tenant filter to every query on this table
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
USING (tenant_id = current_setting('app.tenant_id')::uuid);
-- Per request, the app (connected as a role that doesn't own the table) sets the tenant
SET app.tenant_id = '3f2b8c1e-5d4a-4c7e-9b1a-2e6f0d8c7a41';
SELECT * FROM invoices; -- only this tenant's invoices, with no WHERE clauseReaders ask
What is the difference between single-tenant and multi-tenant?
In a single-tenant setup each customer gets its own instance of the application and database, which gives the strongest isolation and room for customization but costs more to run and update. In a multi-tenant setup customers share one instance, which is cheaper and updates everyone at once, but isolation has to be built into the software.
Is a tenant the same as a user?
No. A tenant is usually a customer organization, and it can have many users. In a project tracker sold to companies, each company is a tenant and its employees are its users.
Which multi-tenant database model should I choose?
A shared schema with a tenant_id column suits many small tenants and is the cheapest; a database per tenant suits fewer, larger tenants or strict compliance needs, such as keeping a customer's data in a particular country. Many products start with the shared model and move big customers to their own database later.
See also
- SaaSDevOps & Cloud, p. 53SaaS (software as a service) is software delivered over the internet as a subscription; users sign in while the provider runs, updates and secures it.
- Cloud ComputingDevOps & Cloud, p. 10Cloud computing is the on-demand delivery of computing resources, such as servers, storage, and databases, over the internet with pay-as-you-go pricing.
- AuthorizationSecurity, p. 4Authorization is the process of deciding what an authenticated user or service is allowed to do, such as which data it can read, change, or delete.
- Rate LimitingBackend & APIs, p. 41Rate limiting is a technique that caps how many requests a client can make to a server or API within a time window, protecting it from abuse and overload.
- ShardingDatabases, p. 42Sharding is a way of scaling a database by splitting its data across several servers, called shards, so each one stores and handles only part of the total.
- ScalabilitySoftware Architecture, p. 38Scalability is a system's ability to handle growing amounts of work, such as more users or data, by adding resources without a drop in performance.
Sources
Spotted a mistake or something missing on this page?Suggest an edit