📖 Scenario: You are building a simple social media app where users can post messages. To make the app fast and scalable, you want to store each post in two places: under the user's posts and in a global posts list.This is called the fan-out writes pattern, where one write operation updates multiple places in the database at once.
🎯 Goal: Build a Firebase Realtime Database update that writes a new post to both the user's posts and the global posts list in a single operation.
📋 What You'll Learn
Create a JavaScript object called postData with the exact keys author and content and their values.
Create a JavaScript object called updates that will hold the paths to update in the database.
Use the updates object to set the new post under /posts/<postId> and under /user-posts/<userId>/<postId>.
Add the final call to firebase.database().ref().update(updates) to perform the fan-out write.
💡 Why This Matters
🌍 Real World
Fan-out writes are used in real apps to keep data duplicated in multiple places for fast reads and easy queries.
💼 Career
Understanding fan-out writes is important for Firebase developers to build scalable and efficient real-time applications.
Progress0 / 4 steps
1
Create the post data object
Create a JavaScript object called postData with the keys author set to 'user123' and content set to 'Hello Firebase!'.
A. Duplicate keys cause only the last one to be saved
B. Firebase rejects duplicate keys with an error
C. Data will be merged correctly without issues
D. The update will only save the first key
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 A
Quick Check:
Duplicate keys overwrite previous in objects [OK]
Hint: No duplicate keys in update object; last wins [OK]
Common Mistakes:
Expecting Firebase to merge duplicate keys
Thinking Firebase throws error on duplicates
Assuming first key is saved instead of last
5. You want to save a new comment both under /comments/comment1 and also under /post-comments/post1/comment1 using fan-out writes. Which update object correctly implements this pattern?
D. { '/comments/comment1': { text: 'Nice!' }, '/post-comments/post1/comment1': 'Nice!' }
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.