Bird
Raised Fist0
Firebasecloud~10 mins

Multi-tenancy patterns in Firebase - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Multi-tenancy patterns
Start: User Request
↓
Identify Tenant
↓
Shared DB
↓
Serve Data
↓
Response to User
↓
End
The flow starts with a user request, identifies the tenant, selects a multi-tenancy pattern (shared, isolated, or hybrid), serves tenant-specific data, and responds.
Execution Sample
Firebase
// Pseudocode for tenant data fetch
function fetchTenantData(userId) {
  let tenantId = identifyTenant(userId);
  if (pattern == 'shared') {
    return querySharedDB(tenantId);
  } else if (pattern == 'isolated') {
    return queryIsolatedDB(tenantId);
  } else {
    return queryHybridDB(tenantId);
  }
}
This code fetches data for a user based on the tenant and multi-tenancy pattern.
Process Table
StepActionTenant IdentifiedPattern SelectedData Source QueriedResult
1User sends requestNoneNoneNoneWaiting
2Identify tenant from userIdtenantANoneNoneTenant identified
3Select multi-tenancy patterntenantAsharedNonePattern chosen
4Query shared database with tenantA filtertenantAsharedShared DBTenantA data returned
5Send response to usertenantAsharedShared DBResponse sent
6End of requesttenantAsharedShared DBComplete
💡 Request completes after sending tenant-specific data based on chosen pattern.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
tenantIdNonetenantAtenantAtenantAtenantA
patternNoneNonesharedsharedshared
dataSourceNoneNoneNoneShared DBShared DB
responseNoneNoneNoneTenantA dataResponse sent
Key Moments - 3 Insights
Why do we need to identify the tenant before querying data?
Because the system must know which tenant's data to fetch. Without tenant identification (see Step 2 in execution_table), the system cannot apply the correct filters or select the right database.
What is the difference between shared and isolated patterns in data querying?
In the shared pattern (Step 4), data for all tenants is in one database with tenant filters. In isolated, each tenant has a separate database. This affects how data is queried and secured.
Can the multi-tenancy pattern change during a request?
No, the pattern is selected once per request (Step 3) and remains fixed for that request to ensure consistent data access.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the tenantId after Step 2?
AtenantA
BNone
Cshared
DTenantA data
💡 Hint
Check the 'Tenant Identified' column at Step 2 in execution_table.
At which step is the multi-tenancy pattern selected?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Pattern Selected' column in execution_table.
If the pattern was 'isolated', which data source would be queried?
AShared DB
BHybrid DB
CIsolated DB
DNo DB
💡 Hint
Refer to the code in execution_sample and the concept_flow diagram.
Concept Snapshot
Multi-tenancy means serving multiple customers (tenants) from one system.
Patterns:
- Shared DB: One database, tenant data separated by filters.
- Isolated DB: Separate database per tenant.
- Hybrid: Mix of both.
Identify tenant first, then select pattern, then query data accordingly.
Full Transcript
This visual execution shows how a system handles multi-tenancy in Firebase. When a user sends a request, the system first identifies which tenant the user belongs to. Then it chooses a multi-tenancy pattern: shared, isolated, or hybrid. Based on this pattern, it queries the correct data source to get tenant-specific data. Finally, it sends the data back to the user. Variables like tenantId, pattern, and dataSource change step-by-step to reflect this process. Key points include the importance of tenant identification before data access and how different patterns affect data querying.

Practice

(1/5)
1. What is the main purpose of multi-tenancy in Firebase applications?
easy
A. To allow multiple users to share the same app while keeping their data separate
B. To increase the speed of database queries
C. To reduce the cost of Firebase hosting
D. To enable offline data access for users

Solution

  1. Step 1: Understand multi-tenancy concept

    Multi-tenancy means many users use one app but their data stays separate.
  2. Step 2: Identify the purpose in Firebase context

    Firebase apps use multi-tenancy to keep user data isolated while sharing the app.
  3. Final Answer:

    To allow multiple users to share the same app while keeping their data separate -> Option A
  4. Quick Check:

    Multi-tenancy = separate data for shared app [OK]
Hint: Multi-tenancy means shared app, separate data [OK]
Common Mistakes:
  • Confusing multi-tenancy with performance optimization
  • Thinking multi-tenancy is about offline access
  • Assuming multi-tenancy reduces hosting costs
2. Which Firestore data structure is best for separating tenant data in a multi-tenant Firebase app?
easy
A. A single collection with tenant ID as a field in each document
B. Separate collections for each tenant grouped by tenant ID
C. One document per tenant with all data nested inside
D. Storing all tenant data in one document without tenant ID

Solution

  1. Step 1: Review Firestore data organization

    Firestore uses collections and documents to organize data.
  2. Step 2: Choose best pattern for tenant separation

    Grouping data by tenant ID in separate collections keeps tenant data isolated and easier to secure.
  3. Final Answer:

    Separate collections for each tenant grouped by tenant ID -> Option B
  4. Quick Check:

    Tenant data separated by collections = Separate collections for each tenant grouped by tenant ID [OK]
Hint: Use collections grouped by tenant ID for clear separation [OK]
Common Mistakes:
  • Putting all tenant data in one document
  • Not using tenant ID to separate data
  • Using a single collection without grouping
3. Given this Firestore security rule snippet for multi-tenancy:
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'?
medium
A. Access is granted because tenant IDs are ignored
B. Access is granted only for read but not write
C. Access is denied only if the document does not exist
D. Access is denied because tenant IDs do not match

Solution

  1. Step 1: Understand the security rule condition

    The rule allows access only if the user's tenant ID matches the tenantId in the path.
  2. 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.
  3. Final Answer:

    Access is denied because tenant IDs do not match -> Option D
  4. Quick Check:

    Tenant ID mismatch blocks access [OK]
Hint: Tenant ID in token must match path tenant ID [OK]
Common Mistakes:
  • Assuming access is granted without tenant ID check
  • Thinking read access is allowed but write is denied
  • Ignoring tenant ID in security rules
4. You wrote this Firestore rule for multi-tenancy:
match /tenants/{tenantId}/data/{docId} {
  allow read, write: if request.auth.token.tenantId == tenantId;
}

But users report access errors. What is the likely problem?
medium
A. The token field name is incorrect; it should be 'tenant' not 'tenantId'
B. The path variable 'tenantId' is not defined in the rule
C. Firestore does not support tenant-based security rules
D. The rule allows access to all tenants without restriction

Solution

  1. Step 1: Check token field name in the rule

    The rule uses 'request.auth.token.tenantId' but the token field is usually 'tenant'.
  2. Step 2: Understand impact of wrong token field

    If the token field name is wrong, the condition always fails, causing access errors.
  3. Final Answer:

    The token field name is incorrect; it should be 'tenant' not 'tenantId' -> Option A
  4. Quick Check:

    Correct token field name fixes access [OK]
Hint: Match token field name exactly in rules [OK]
Common Mistakes:
  • Using wrong token field name
  • Assuming Firestore ignores token field names
  • Not defining path variables correctly
5. You want to design a Firebase app supporting multiple tenants with isolated data and secure access. Which combination of patterns is best?
hard
A. Use a single Firestore collection for all tenants and filter data in the app code
B. Use Firebase Realtime Database instead of Firestore for multi-tenancy
C. Create separate Firestore collections per tenant and enforce tenant ID checks in security rules
D. Store all tenant data in one document and use client-side filtering for access control

Solution

  1. Step 1: Evaluate data separation methods

    Separate collections per tenant keep data isolated and easier to manage.
  2. Step 2: Apply security best practices

    Security rules must check tenant ID to prevent unauthorized access.
  3. 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.
  4. Final Answer:

    Create separate Firestore collections per tenant and enforce tenant ID checks in security rules -> Option C
  5. Quick Check:

    Separate collections + security rules = best multi-tenancy [OK]
Hint: Combine data separation with security rules for best isolation [OK]
Common Mistakes:
  • Relying on client-side filtering only
  • Using single collection without security rules
  • Choosing Realtime Database without need
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard