Multi-tenancy patterns in Firebase - Time & Space Complexity
When using Firebase for multi-tenancy, we want to know how the work grows as more tenants use the system.
We ask: How does the number of operations change when we add more tenants?
Analyze the time complexity of this Firebase data fetch for multiple tenants.
const tenants = ['tenantA', 'tenantB', 'tenantC'];
tenants.forEach(async (tenant) => {
const snapshot = await firebase.firestore()
.collection('tenants')
.doc(tenant)
.collection('users')
.get();
console.log(`${tenant} users count:`, snapshot.size);
});
This code fetches user data for each tenant separately from Firestore.
Look at what repeats as tenants increase.
- Primary operation: Firestore read for each tenant's users collection.
- How many times: Once per tenant in the list.
Each new tenant adds one more Firestore read operation.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 Firestore reads |
| 100 | 100 Firestore reads |
| 1000 | 1000 Firestore reads |
Pattern observation: The number of reads grows directly with the number of tenants.
Time Complexity: O(n)
This means the work grows in a straight line as you add more tenants.
[X] Wrong: "Fetching all tenants' data is just one operation regardless of tenant count."
[OK] Correct: Each tenant's data is stored separately, so Firebase must do a separate read for each tenant.
Understanding how operations grow with tenants helps you design scalable apps and explain your choices clearly.
What if we stored all tenants' users in a single collection with tenant IDs as fields? How would the time complexity change?
Practice
Solution
Step 1: Understand multi-tenancy concept
Multi-tenancy means many users use one app but their data stays separate.Step 2: Identify the purpose in Firebase context
Firebase apps use multi-tenancy to keep user data isolated while sharing the app.Final Answer:
To allow multiple users to share the same app while keeping their data separate -> Option AQuick Check:
Multi-tenancy = separate data for shared app [OK]
- Confusing multi-tenancy with performance optimization
- Thinking multi-tenancy is about offline access
- Assuming multi-tenancy reduces hosting costs
Solution
Step 1: Review Firestore data organization
Firestore uses collections and documents to organize data.Step 2: Choose best pattern for tenant separation
Grouping data by tenant ID in separate collections keeps tenant data isolated and easier to secure.Final Answer:
Separate collections for each tenant grouped by tenant ID -> Option BQuick Check:
Tenant data separated by collections = Separate collections for each tenant grouped by tenant ID [OK]
- Putting all tenant data in one document
- Not using tenant ID to separate data
- Using a single collection without grouping
match /tenants/{tenantId}/data/{docId} {
allow read, write: if request.auth.token.tenant == tenantId;
}What happens if a user with tenant ID 'tenantA' tries to read data from 'tenantB'?
Solution
Step 1: Understand the security rule condition
The rule allows access only if the user's tenant ID matches the tenantId in the path.Step 2: Apply the condition to the user and requested tenant
User's tenant ID is 'tenantA' but tries to access 'tenantB' data, so condition fails.Final Answer:
Access is denied because tenant IDs do not match -> Option DQuick Check:
Tenant ID mismatch blocks access [OK]
- Assuming access is granted without tenant ID check
- Thinking read access is allowed but write is denied
- Ignoring tenant ID in security rules
match /tenants/{tenantId}/data/{docId} {
allow read, write: if request.auth.token.tenantId == tenantId;
}But users report access errors. What is the likely problem?
Solution
Step 1: Check token field name in the rule
The rule uses 'request.auth.token.tenantId' but the token field is usually 'tenant'.Step 2: Understand impact of wrong token field
If the token field name is wrong, the condition always fails, causing access errors.Final Answer:
The token field name is incorrect; it should be 'tenant' not 'tenantId' -> Option AQuick Check:
Correct token field name fixes access [OK]
- Using wrong token field name
- Assuming Firestore ignores token field names
- Not defining path variables correctly
Solution
Step 1: Evaluate data separation methods
Separate collections per tenant keep data isolated and easier to manage.Step 2: Apply security best practices
Security rules must check tenant ID to prevent unauthorized access.Step 3: Compare options
Create separate Firestore collections per tenant and enforce tenant ID checks in security rules combines data separation and security rules, ensuring isolation and protection.Final Answer:
Create separate Firestore collections per tenant and enforce tenant ID checks in security rules -> Option CQuick Check:
Separate collections + security rules = best multi-tenancy [OK]
- Relying on client-side filtering only
- Using single collection without security rules
- Choosing Realtime Database without need
Ready to prove it?
Pass the test and this pattern moves unknown
