Migrating from Realtime Database to Firestore in Firebase - Time & Space Complexity
When moving data from Realtime Database to Firestore, it's important to understand how the time to migrate grows as the data size grows.
We want to know how the number of operations changes as we move more data.
Analyze the time complexity of migrating data by reading from Realtime Database and writing to Firestore.
const rtdbRef = ref(database, 'users');
get(rtdbRef).then(snapshot => {
const users = snapshot.val();
for (const userId in users) {
const userData = users[userId];
set(doc(firestore, 'users', userId), userData);
}
});
This code reads all users from Realtime Database and writes each user as a document in Firestore.
Look at what happens repeatedly during migration.
- Primary operation: Reading all user data once, then writing each user to Firestore individually.
- How many times: One read operation for all users, then one write operation per user.
As the number of users grows, the number of writes grows too, but the read stays one big operation.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 1 read + 10 writes = 11 operations |
| 100 | 1 read + 100 writes = 101 operations |
| 1000 | 1 read + 1000 writes = 1001 operations |
Pattern observation: The total operations grow roughly in a straight line with the number of users.
Time Complexity: O(n)
This means the time to migrate grows directly in proportion to the number of users being moved.
[X] Wrong: "Reading all data once means the migration time stays the same no matter how many users there are."
[OK] Correct: While the read is one operation, each user requires a separate write to Firestore, so more users mean more writes and more time.
Understanding how data migration scales helps you plan and explain real-world cloud tasks clearly and confidently.
"What if we batch multiple user writes into a single Firestore batch write? How would the time complexity change?"
Practice
Solution
Step 1: Understand data structure differences
Realtime Database organizes data as a single JSON tree, making it less flexible for complex queries.Step 2: Recognize Firestore's document model
Firestore stores data in documents grouped into collections, allowing more structured and scalable data management.Final Answer:
Realtime Database stores data as one large JSON tree, Firestore stores data in documents and collections. -> Option CQuick Check:
Data structure difference = Realtime Database stores data as one large JSON tree, Firestore stores data in documents and collections. [OK]
- Confusing query languages between databases
- Assuming Firestore lacks offline support
- Thinking backups are automatic in Realtime Database
Solution
Step 1: Identify the correct CLI command for Realtime Database export
The Firebase CLI usesfirebase database:get / > ./backup.jsonto export Realtime Database data.Step 2: Confirm the syntax
The command outputs JSON to stdout, which is redirected to./backup.json.Final Answer:
firebase database:get / > ./backup.json -> Option AQuick Check:
Export command syntax = firebase database:get / > ./backup.json [OK]
- Using firestore export command for Realtime Database
- Incorrect command syntax
- Confusing backup and export commands
const userData = {
"user123": {"name": "Alice", "age": 30},
"user456": {"name": "Bob", "age": 25}
};
for (const userId in userData) {
firestore.collection('users').doc(userId).set(userData[userId]);
}Solution
Step 1: Analyze the loop and document ID assignment
The loop usesdoc(userId), so the document ID is set explicitly to the key, e.g., "user123".Step 2: Confirm document ID is not auto-generated
Sincedoc(userId)is used, Firestore will not auto-generate an ID.Final Answer:
"user123" -> Option DQuick Check:
Document ID = userId key = "user123" [OK]
- Assuming Firestore auto-generates IDs always
- Confusing collection name with document ID
- Using user name as ID mistakenly
Solution
Step 1: Understand asynchronous Firestore writes
Firestore'sset()method returns a promise that must be awaited to complete the write.Step 2: Identify missing await causes empty documents
Ifset()is not awaited, the script may finish before writes complete, resulting in empty documents.Final Answer:
You forgot to await the asynchronous set() calls in the migration script. -> Option BQuick Check:
Await async writes = You forgot to await the asynchronous set() calls in the migration script. [OK]
- Ignoring async nature of Firestore writes
- Assuming collection name mismatch causes empty docs
- Confusing export source databases
Solution
Step 1: Understand Firestore data modeling best practices
Firestore supports subcollections to model one-to-many relationships efficiently.Step 2: Choose structure preserving hierarchy and query efficiency
Storing posts as subcollections under user documents keeps data organized and allows targeted queries.Final Answer:
Store users as documents in 'users' collection and posts as subcollections under each user document. -> Option AQuick Check:
Use subcollections for nested data = Store users as documents in 'users' collection and posts as subcollections under each user document. [OK]
- Putting all data in one large document causing size limits
- Flattening data losing natural hierarchy
- Separating posts without user linkage
Ready to prove it?
Pass the test and this pattern moves unknown
