You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cloud/bestpractices/multitenancy.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,9 +5,9 @@ title: 'Multi tenancy'
5
5
description: 'Best practices to implement multi tenancy'
6
6
---
7
7
8
-
On a SaaS data analytics platform, it is common for multiple tenants, such as organizations, customers, or business units, to share the same database infrastructure while maintaining logical separation of their data. This allows different users to securely access their own data within the same platform
8
+
On a SaaS data analytics platform, it is common for multiple tenants, such as organizations, customers, or business units, to share the same database infrastructure while maintaining logical separation of their data. This allows different users to securely access their own data within the same platform.
9
9
10
-
Depending on the requirements, they are different ways to implement multi-tenancy. Below is a guide on how to implement them with ClickHouse Cloud.
10
+
Depending on the requirements, there are different ways to implement multi-tenancy. Below is a guide on how to implement them with ClickHouse Cloud.
11
11
12
12
## Shared table {#shared-table}
13
13
@@ -21,13 +21,13 @@ This method is particularly effective for handling a large number of tenants.
21
21
22
22
However, alternative approaches may be more suitable if tenants have different data schemas or are expected to diverge over time.
23
23
24
-
In extreme cases where there is a significant gap in dataset sizes, imagine one tenant with petabytes of data alongside many smaller tenants with only a few gigabytes. Query performance for smaller tenants may be impacted. Note, this issue is largely mitigated by including the tenant field in the primary key.
24
+
In extreme cases where there is a significant gap in dataset sizes query performance for smaller tenants may be impacted - imagine one tenant with petabytes of data alongside many smaller tenants with only a few gigabytes. Note, this issue is largely mitigated by including the tenant field in the primary key.
25
25
26
26
### Example {#shared-table-example}
27
27
28
28
This is an example of a shared table multi-tenancy model implementation.
29
29
30
-
First, let's create a shared table with a field `tenant_id`that included in the primary key.
30
+
First, let's create a shared table with a field `tenant_id` included in the primary key.
31
31
32
32
```sql
33
33
--- Create table events. Using tenant_id as part of the primary key
@@ -89,7 +89,7 @@ GRANT user_role TO user_1
89
89
GRANT user_role TO user_2
90
90
```
91
91
92
-
Now you can connect as `user_1` and run a simple select. Only rows from the tenant 1 are returned.
92
+
Now you can connect as `user_1` and run a simple select. Only rows from the first tenant are returned.
93
93
94
94
```sql
95
95
-- Logged as user_1
@@ -109,7 +109,7 @@ FROM events
109
109
110
110
In this approach, each tenant’s data is stored in a separate table within the same database, eliminating the need for a specific field to identify tenants. User access is enforced using a [GRANT statement](/sql-reference/statements/grant), ensuring that each user can access only tables containing their tenants' data.
111
111
112
-
Using separate tables is good choice when tenants have different data schemas.
112
+
> **Using separate tables is a good choice when tenants have different data schemas.**
113
113
114
114
For scenarios involving a few tenants with very large datasets where query performance is critical, this approach may outperform a shared table model. Since there is no need to filter out other tenants’ data, queries can be more efficient. Additionally, primary keys can be further optimized, as there is no need to include an extra field (such as a tenant ID) in the primary key.
115
115
@@ -171,15 +171,15 @@ CREATE USER user_1 IDENTIFIED BY '<password>'
171
171
CREATEUSERuser_2 IDENTIFIED BY '<password>'
172
172
```
173
173
174
-
Then `GRANT SELECT` privileges on the correponding table.
174
+
Then `GRANT SELECT` privileges on the corresponding table.
175
175
176
176
```sql
177
177
-- Grant read only to events table.
178
178
GRANTSELECTONdefault.events_tenant_1 TO user_1
179
179
GRANTSELECTONdefault.events_tenant_2 TO user_2
180
180
```
181
181
182
-
Now you can connect as `user_1` and run a simple select on the correct. Only rows from the tenant 1 are returned.
182
+
Now you can connect as `user_1` and run a simple select from the table corresponding to this user. Only rows from the first tenant are returned.
183
183
184
184
```sql
185
185
-- Logged as user_1
@@ -199,7 +199,7 @@ FROM default.events_tenant_1
199
199
200
200
Each tenant’s data is stored in a separate database within the same ClickHouse service.
201
201
202
-
This approach is useful if each tenant requires a large number of tables and possibly materialized views, and has different data schema. However, it may become challenging to manage if the number of tenants is large.
202
+
> **This approach is useful if each tenant requires a large number of tables and possibly materialized views, and has different data schema. However, it may become challenging to manage if the number of tenants is large.**
203
203
204
204
The implementation is similar to the separate tables approach, but instead of granting privileges at the table level, privileges are granted at the database level.
205
205
@@ -269,15 +269,15 @@ CREATE USER user_1 IDENTIFIED BY '<password>'
269
269
CREATEUSERuser_2 IDENTIFIED BY '<password>'
270
270
```
271
271
272
-
Then `GRANT SELECT` privileges on the correponding table.
272
+
Then `GRANT SELECT` privileges on the corresponding table.
273
273
274
274
```sql
275
275
-- Grant read only to events table.
276
276
GRANTSELECTONtenant_1.events TO user_1
277
277
GRANTSELECTONtenant_2.events TO user_2
278
278
```
279
279
280
-
Now you can connect as `user_1` and run a simple select on the correct. Only rows from the tenant 1 are returned.
280
+
Now you can connect as `user_1` and run a simple select on the events table of the appropriate database. Only rows from the first tenant are returned.
281
281
282
282
```sql
283
283
-- Logged as user_1
@@ -297,7 +297,7 @@ FROM tenant_1.events
297
297
298
298
The most radical approach is to use a different ClickHouse service per tenant.
299
299
300
-
This less common method would be a solution if tenants data are required to be stored in different regions - for legal, security or proximity reasons.
300
+
> **This less common method would be a solution if tenants data are required to be stored in different regions - for legal, security or proximity reasons.**
301
301
302
302
A user account must be created on each service where the user can access their respective tenant’s data.
303
303
@@ -348,7 +348,7 @@ Then `GRANT SELECT` privileges on the correponding table.
348
348
GRANTSELECTON events TO user_1
349
349
```
350
350
351
-
Now you can connect as `user_1` on the service for tenant 1 and run a simple select on the correct. Only rows from the tenant 1 are returned.
351
+
Now you can connect as `user_1` on the service for tenant 1 and run a simple select. Only rows from the first tenant are returned.
0 commit comments