Batch writes in Firebase - Time & Space Complexity
When using batch writes in Firebase, it's important to know how the time to complete changes as you add more writes.
We want to understand how the number of write operations affects the total time taken.
Analyze the time complexity of the following batch write operation.
const batch = firestore.batch();
const docs = [doc1, doc2, doc3, /* ... */ docN];
for (const doc of docs) {
batch.set(doc.ref, doc.data());
}
await batch.commit();
This code groups multiple document writes into one batch and sends them together to Firebase.
Look at what repeats as the input grows.
- Primary operation: Adding each write to the batch with
batch.set(). - How many times: Once for each document to write (n times).
- Dominant operation: The
batch.commit()sends all writes at once, but the batch size affects the total work.
As you add more documents, the number of write operations grows directly with the number of documents.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 writes added, 1 batch commit |
| 100 | 100 writes added, 1 batch commit |
| 1000 | 1000 writes added, 1 batch commit |
Pattern observation: The number of write additions grows linearly with input size, but the commit is a single call.
Time Complexity: O(n)
This means the total time grows roughly in direct proportion to the number of writes you add to the batch.
[X] Wrong: "Batch writes always take the same time no matter how many writes are included."
[OK] Correct: While the commit is one call, adding more writes means more work inside the batch, so time grows with the number of writes.
Understanding how batch writes scale helps you design efficient data updates and shows you can reason about cloud operation costs clearly.
What if we split a large batch into multiple smaller batches? How would that affect the time complexity?
Practice
Solution
Step 1: Understand batch writes concept
Batch writes allow grouping many data changes into one request to keep data consistent.Step 2: Identify the main purpose
The main purpose is to combine multiple changes to save time and ensure consistency.Final Answer:
To group multiple data changes into a single request -> Option AQuick Check:
Batch writes = group changes [OK]
- Thinking batch writes speed up data reading
- Confusing batch writes with database creation
- Assuming batch writes automatically backup data
Solution
Step 1: Recall Firebase batch write syntax
The correct method to start a batch is firestore.batch().Step 2: Compare options
Only const batch = firestore.batch(); uses the exact Firebase API method firestore.batch().Final Answer:
const batch = firestore.batch(); -> Option AQuick Check:
Start batch with firestore.batch() [OK]
- Using non-existent methods like startBatch or newBatch
- Confusing batch creation with other Firestore methods
- Misspelling the batch method name
const batch = firestore.batch();
const docRef1 = firestore.collection('users').doc('user1');
batch.set(docRef1, {name: 'Alice'});
const docRef2 = firestore.collection('users').doc('user2');
batch.update(docRef2, {age: 30});
await batch.commit();Solution
Step 1: Understand batch set and update behavior
batch.set creates or overwrites a document; batch.update requires the document to exist.Step 2: Analyze the code effect
Since 'user2' might not exist, batch.update will cause the entire batch commit to fail.Final Answer:
The batch commit will fail if 'user2' document does not exist -> Option CQuick Check:
batch.update needs existing doc = fail if missing [OK]
- Assuming batch.update creates documents
- Thinking batch.commit partially applies changes
- Ignoring that batch fails on any single error
const batch = firestore.batch();
const docRef = firestore.collection('items').doc('item1');
batch.update(docRef, {price: 100});
batch.set(docRef, {name: 'Book'});
await batch.commit();Solution
Step 1: Review batch operation order rules
Firebase batch allows multiple operations on the same doc; last write wins.Step 2: Analyze the code
batch.update then batch.set on same doc means set overwrites update fields.Final Answer:
No error; batch.commit will succeed and apply both changes -> Option DQuick Check:
Multiple ops allowed; last write wins [OK]
- Thinking batch.update must come after batch.set
- Assuming batch.set overwrites cause errors
- Believing batch.update needs a callback
Solution
Step 1: Understand atomic batch writes
Batch writes commit all changes together; if one fails, none apply.Step 2: Choose correct method for updating existing profiles
batch.update modifies existing docs without overwriting all fields, suitable for updates.Step 3: Analyze options
Use batch.update for each user document and commit once uses batch.update for all users and commits once, ensuring atomicity.Final Answer:
Use batch.update for each user document and commit once -> Option BQuick Check:
batch.update + single commit = atomic update [OK]
- Using multiple commits losing atomicity
- Using batch.set which overwrites entire documents
- Deleting docs before updating unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
