Read and write permissions in Firebase - Time & Space Complexity
When we check read and write permissions in Firebase, we want to know how the time it takes changes as more data or users are involved.
We ask: How does permission checking grow when we read or write more data?
Analyze the time complexity of the following operation sequence.
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /items/{itemId} {
allow read: if request.auth != null;
allow write: if request.auth.uid == resource.data.ownerId;
}
}
}
This rule checks if a user is signed in to read data, and if the user owns the item to write data.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Permission check for each read or write request.
- How many times: Once per document accessed or modified.
Each document access triggers one permission check. More documents mean more checks.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 permission checks |
| 100 | 100 permission checks |
| 1000 | 1000 permission checks |
Pattern observation: The number of permission checks grows directly with the number of documents accessed.
Time Complexity: O(n)
This means the time to check permissions grows in a straight line as you access more documents.
[X] Wrong: "Permission checks happen once for all documents at once."
[OK] Correct: Each document access triggers its own check, so more documents mean more checks.
Understanding how permission checks scale helps you design secure and efficient apps, a skill valued in real projects.
"What if we changed the rules to check permissions only once per batch of documents? How would the time complexity change?"
Practice
Solution
Step 1: Understand permission roles
Read and write permissions define who can access or modify data.Step 2: Identify the purpose
They protect data by controlling access, not by speeding up or backing up data.Final Answer:
To control who can view or change data in the database -> Option AQuick Check:
Permissions control access = A [OK]
- Confusing permissions with performance features
- Thinking permissions create database structure
- Assuming permissions handle backups
Solution
Step 1: Understand auth variable
In Firebase rules, auth != null means user is logged in.Step 2: Match condition to requirement
Allowing read only if auth != null means only authenticated users can read.Final Answer:
"read": "auth != null" -> Option BQuick Check:
auth != null means logged in = C [OK]
- Using auth == null which allows unauthenticated users
- Setting read to true allows everyone
- Setting read to false blocks all reads
{
"rules": {
"messages": {
".read": "auth != null",
".write": "auth.uid == data.child('owner').val()"
}
}
}What happens if a logged-in user tries to write a message where they are not the owner?
Solution
Step 1: Analyze write condition
Write is allowed only if auth.uid equals the owner field in data.Step 2: Check user ownership
If user is not owner, condition fails, so write is denied.Final Answer:
Write is denied because user is not the owner -> Option AQuick Check:
Write allowed only if user owns data = D [OK]
- Assuming logged-in users can write anywhere
- Confusing read and write rules
- Ignoring the owner check in write condition
{
"rules": {
"posts": {
".read": "auth != null",
".write": "auth.uid = data.child('author').val()"
}
}
}Solution
Step 1: Check write condition syntax
Single equals (=) is assignment, not comparison in Firebase rules.Step 2: Correct comparison operator
Use double equals (==) to compare auth.uid and data.child('author').val().Final Answer:
Using single equals (=) instead of double equals (==) in write condition -> Option DQuick Check:
Use == for comparison, not = [OK]
- Confusing assignment (=) with comparison (==)
- Thinking auth.uid needs quotes
- Believing data.child() is invalid in rules
{
"rules": {
"profiles": {
"$userId": {
".read": "?",
".write": "auth.uid == $userId"
}
}
}
}Solution
Step 1: Understand read condition need
Users should read only their own profile, so read must check auth.uid equals $userId.Step 2: Match read condition to requirement
Setting ".read": "auth.uid == $userId" restricts read access to owner only.Final Answer:
"auth.uid == $userId" -> Option CQuick Check:
Read allowed only if user matches profile ID = B [OK]
- Using auth != null allows all logged-in users to read
- Setting read to true allows everyone
- Using auth.uid != $userId allows others to read
Ready to prove it?
Pass the test and this pattern moves unknown
