Why write patterns affect consistency in Firebase - Performance Analysis
When using Firebase, how you write data affects how fast and reliably your app stays in sync.
We want to see how different write patterns change the work Firebase does behind the scenes.
Analyze the time complexity of writing multiple user profiles one by one.
const db = firebase.firestore();
const users = [{id: 'u1', name: 'Alice'}, {id: 'u2', name: 'Bob'}, /* more users */];
for (const user of users) {
db.collection('users').doc(user.id).set({ name: user.name });
}
This code writes each user profile separately to the database.
Look at what repeats as the input grows.
- Primary operation: One write call per user document.
- How many times: Once for each user in the list.
Each user causes a separate write, so more users mean more writes.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 writes |
| 100 | 100 writes |
| 1000 | 1000 writes |
Pattern observation: The number of writes grows directly with the number of users.
Time Complexity: O(n)
This means the work grows in a straight line as you add more users to write.
[X] Wrong: "Writing many documents one by one is just as fast as writing them all at once."
[OK] Correct: Each separate write sends a request and waits for confirmation, so doing many writes separately takes more time and can cause delays in consistency.
Understanding how write patterns affect consistency helps you design apps that stay reliable and fast as they grow.
What if we used a batch write to send all user updates at once? How would the time complexity change?