Why Realtime Database differs from Firestore in Firebase - Performance Analysis
We want to understand how the speed of data operations changes when using Realtime Database versus Firestore.
How does the number of operations grow as we work with more data?
Analyze the time complexity of reading multiple records from each database.
// Realtime Database
const ref = firebase.database().ref('items');
ref.once('value').then(snapshot => {
snapshot.forEach(child => {
console.log(child.key, child.val());
});
});
// Firestore
const collection = firebase.firestore().collection('items');
collection.get().then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(doc.id, doc.data());
});
});
This code reads all items from a list in each database and logs their data.
Look at what happens repeatedly when reading data.
- Primary operation: Processing each item's data.
- How many times: Once per item in the list.
As the number of items grows, the number of operations grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations |
| 100 | About 100 operations |
| 1000 | About 1000 operations |
Each item requires a separate operation, so the work grows directly with the number of items.
Time Complexity: O(n)
This means the time to read all items grows in a straight line as the number of items increases.
[X] Wrong: "Fetching data from Firestore or Realtime Database always takes the same time no matter how many items there are."
[OK] Correct: Each item requires a separate operation, so more items mean more work and longer time.
Understanding how data operations scale helps you design apps that stay fast as they grow. This skill shows you can think about real-world app performance.
What if we used queries to limit the number of items fetched? How would that change the time complexity?
Practice
Solution
Step 1: Understand Realtime Database data structure
Realtime Database organizes data as a single large JSON tree, making it simple but less structured.Step 2: Understand Firestore data structure
Firestore organizes data into collections and documents, allowing more structure and flexibility.Final Answer:
Realtime Database stores data as one large JSON tree, while Firestore uses collections and documents. -> Option AQuick Check:
Data structure difference = Realtime Database stores data as one large JSON tree, while Firestore uses collections and documents. [OK]
- Confusing Firestore's collections with Realtime Database's JSON tree
- Thinking both use the same data structure
- Mixing up file formats like XML or CSV
Solution
Step 1: Identify Firestore syntax
Firestore uses collections and documents accessed by db.collection('name').doc('id').Step 2: Compare options
db.collection('users').doc('user123') matches Firestore syntax; db.ref('users/user123') is Realtime Database style; Options C and D are invalid.Final Answer:
db.collection('users').doc('user123') -> Option BQuick Check:
Firestore document reference = db.collection('users').doc('user123') [OK]
- Using Realtime Database syntax for Firestore
- Confusing doc() with ref()
- Trying to use SQL-like table and row methods
const users = await db.collection('users').where('age', '>', 18).get();
console.log(users.docs.length);What will this code output compared to a similar Realtime Database query?
Solution
Step 1: Understand Firestore query capabilities
Firestore supports advanced queries like filtering with where clauses.Step 2: Compare with Realtime Database
Realtime Database has limited querying and cannot filter by fields easily.Final Answer:
Number of users older than 18, Firestore supports this query directly. -> Option CQuick Check:
Firestore advanced queries = Number of users older than 18, Firestore supports this query directly. [OK]
- Thinking Firestore cannot filter data
- Assuming Realtime Database supports complex queries
- Confusing query syntax between databases
const ref = db.ref('users').orderByChild('age').equalTo(25);
ref.on('value', snapshot => { console.log(snapshot.val()); });But it returns null even though users with age 25 exist. What is the likely issue?
Solution
Step 1: Check Realtime Database query requirements
Realtime Database needs proper indexing for orderByChild queries to work.Step 2: Identify cause of null result
Without indexes, the query returns null even if data exists.Final Answer:
The query is invalid; Realtime Database requires indexes for orderByChild queries. -> Option DQuick Check:
Missing index causes null result = The query is invalid; Realtime Database requires indexes for orderByChild queries. [OK]
- Confusing Firestore and Realtime Database syntax
- Assuming data path is missing without checking
- Believing Realtime Database cannot query child properties
Solution
Step 1: Analyze app requirements
A chat app with many users and complex queries needs good scaling and query support.Step 2: Compare database features
Firestore supports complex queries and scales better than Realtime Database for large apps.Final Answer:
Use Firestore because it supports complex queries and scales well. -> Option AQuick Check:
Complex queries + scaling = Firestore [OK]
- Choosing Realtime Database for complex queries
- Confusing data structure descriptions
- Ignoring scaling needs for large apps
Ready to prove it?
Pass the test and this pattern moves unknown
