Fan-out writes pattern in Firebase - Time & Space Complexity
When using the fan-out writes pattern in Firebase, we write the same data to many places at once.
We want to know how the time it takes grows as we write to more locations.
Analyze the time complexity of the following operation sequence.
const updates = {};
const data = { name: "Alice", age: 30 };
const userId = "user123";
const postId = "post456";
updates[`/users/${userId}`] = data;
updates[`/posts/${postId}/author`] = data;
updates[`/feeds/${userId}/${postId}`] = data;
firebase.database().ref().update(updates);
This code writes the same data to multiple paths in the database at once using a fan-out update.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Writing data to multiple database paths in one update call.
- How many times: The number of paths we write to (3 in this example, but can be many more).
Each additional path we write to adds one more piece of data to send in the update.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 1 update call with 10 paths |
| 100 | 1 update call with 100 paths |
| 1000 | 1 update call with 1000 paths |
Pattern observation: The number of paths grows, but all are sent in a single update call, so the operation count stays one, but the data size grows linearly.
Time Complexity: O(n)
This means the time to complete the write grows in direct proportion to the number of paths we update.
[X] Wrong: "Because we use one update call, the time stays the same no matter how many paths we write to."
[OK] Correct: Even though it's one call, the data size grows with each path, so the time to send and process the data grows too.
Understanding how fan-out writes scale helps you design efficient database updates and shows you can think about how cloud operations grow with data size.
"What if we split the fan-out writes into multiple smaller update calls instead of one big update? How would the time complexity change?"
Practice
fan-out writes pattern in Firebase?Solution
Step 1: Understand fan-out writes concept
Fan-out writes save identical data in many database locations at once.Step 2: Identify the benefit
This helps apps read data faster and simpler by having data ready where needed.Final Answer:
To save the same data in multiple places at once for faster reads -> Option AQuick Check:
Fan-out writes = save data in many places [OK]
- Thinking fan-out writes delete data
- Confusing fan-out with backup
- Assuming fan-out encrypts data
Solution
Step 1: Recall Firebase write methods
set()writes to one location,push()adds new child,remove()deletes data.Step 2: Identify atomic multi-path update
update()can write to many paths at once atomically, perfect for fan-out writes.Final Answer:
firebase.database().update() -> Option DQuick Check:
Multi-path atomic write = update() [OK]
- Using set() which overwrites one path only
- Using push() which adds new child, not multi-path
- Using remove() which deletes data
{
'/posts/post1': { title: 'Hello' },
'/user-posts/user1/post1': { title: 'Hello' }
}What happens when you call
firebase.database().ref().update(updateObject)?Solution
Step 1: Understand update() with multiple paths
Update accepts an object with multiple paths as keys and writes data atomically to all.Step 2: Analyze given updateObject
It has two paths: '/posts/post1' and '/user-posts/user1/post1', both with same data.Step 3: Predict behavior
Calling update() writes data to both paths at once, ensuring consistency.Final Answer:
Data is saved at both '/posts/post1' and '/user-posts/user1/post1' atomically -> Option BQuick Check:
update() writes all paths atomically [OK]
- Thinking update() writes only one path
- Confusing update() with set()
- Assuming syntax error in object
{
'/posts/post1': { title: 'Hello' },
'/posts/post1': { author: 'Alice' }
}What is the problem with this update object?
Solution
Step 1: Identify duplicate keys in object
The object has two entries with the same key '/posts/post1'.Step 2: Understand JavaScript object behavior
Duplicate keys in an object overwrite previous ones; only the last remains.Step 3: Predict Firebase update behavior
Firebase receives only the last key-value pair, so only { author: 'Alice' } is saved.Final Answer:
Duplicate keys cause only the last one to be saved -> Option AQuick Check:
Duplicate keys overwrite previous in objects [OK]
- Expecting Firebase to merge duplicate keys
- Thinking Firebase throws error on duplicates
- Assuming first key is saved instead of last
/comments/comment1 and also under /post-comments/post1/comment1 using fan-out writes. Which update object correctly implements this pattern?Solution
Step 1: Understand fan-out write structure
Each path key must be a full path string to the exact location, and values must be objects with data.Step 2: Analyze each option
A: duplicate keys for same path; only one location updated.
B: distinct full paths to both locations with data objects; correct.
C: second path too shallow ('/post-comments/post1'), places text directly under post1, missing /comment1.
D: string instead of object at second path.Step 3: Choose correct update object
{ '/comments/comment1': { text: 'Nice!' }, '/post-comments/post1/comment1': { text: 'Nice!' } } correctly writes the comment data to both paths atomically.Final Answer:
{ '/comments/comment1': { text: 'Nice!' }, '/post-comments/post1/comment1': { text: 'Nice!' } } -> Option CQuick Check:
Full paths as keys with data objects = correct fan-out [OK]
- Using duplicate keys in update object
- Nesting data instead of using full path keys
- Assigning string instead of object as value
Ready to prove it?
Pass the test and this pattern moves unknown
