Batch limits and best practices in Firebase - Time & Space Complexity
When working with Firebase batch operations, it is important to understand how the number of operations affects execution time.
We want to know how the time grows as we add more writes to a batch.
Analyze the time complexity of this batch write operation.
const batch = firestore.batch();
const docs = await firestore.collection('users').listDocuments();
for (const doc of docs) {
batch.update(doc, { active: true });
}
await batch.commit();
This code updates many user documents in a single batch write.
Look at what repeats as the batch size grows.
- Primary operation: Adding update commands to the batch for each document.
- How many times: Once per document in the collection.
- Dominant operation: The batch.commit() sends all updates at once, but the preparation grows with the number of documents.
As you add more documents, the number of update commands grows directly with the number of documents.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 update commands + 1 commit |
| 100 | 100 update commands + 1 commit |
| 1000 | 1000 update commands + 1 commit |
Pattern observation: The number of update commands grows linearly with the number of documents.
Time Complexity: O(n)
This means the time to prepare and send the batch grows directly with the number of documents you update.
[X] Wrong: "Batch writes always take the same time no matter how many updates are inside."
[OK] Correct: Each update adds work to the batch, so more updates mean more time to prepare and send.
Understanding how batch operations scale helps you design efficient data updates and avoid hitting limits in real projects.
"What if we split a large batch into multiple smaller batches? How would that affect the time complexity?"
Practice
Solution
Step 1: Recall Firestore batch write limits
Firestore limits batch writes to a maximum of 500 operations to ensure performance and consistency.Step 2: Confirm the exact number
The official Firestore documentation states the limit is 500 operations per batch.Final Answer:
500 operations -> Option DQuick Check:
Batch limit = 500 operations [OK]
- Thinking batch limit is 1000 or unlimited
- Confusing batch limit with document size limit
- Assuming batch size depends on network speed
Solution
Step 1: Identify Firestore batch syntax
The Firestore JavaScript SDK uses firestore.batch() to create a new batch instance.Step 2: Verify method names
Methods like startBatch(), BatchWrite(), or createBatchWrite() do not exist in Firestore SDK.Final Answer:
const batch = firestore.batch(); -> Option BQuick Check:
Correct batch start method = batch() [OK]
- Using non-existent methods like startBatch()
- Trying to instantiate batch with new keyword
- Confusing batch with transaction methods
const batch = firestore.batch();
const docRef1 = firestore.collection('users').doc('user1');
const docRef2 = firestore.collection('users').doc('user2');
batch.set(docRef1, {name: 'Alice'});
batch.update(docRef2, {age: 30});
await batch.commit();
console.log('Batch committed');What will be printed after successful execution?
Solution
Step 1: Understand batch commit behavior
Calling await batch.commit() waits for all operations to complete successfully.Step 2: Confirm console output after commit
After commit resolves, console.log prints 'Batch committed'. No errors occur if data is valid.Final Answer:
Batch committed -> Option AQuick Check:
Successful commit logs 'Batch committed' [OK]
- Expecting output before commit finishes
- Assuming update fails without checking document existence
- Ignoring async/await behavior
Solution
Step 1: Recognize Firestore batch limit
Firestore limits batch writes to 500 operations max, so 600 exceeds this limit causing errors.Step 2: Apply best practice to split batches
Splitting 600 operations into smaller batches (e.g., two batches of 300) avoids limit errors and keeps writes manageable.Final Answer:
Split the 600 operations into two batches of 300 each -> Option AQuick Check:
Split large batches to ≤ 500 ops [OK]
- Trying to increase batch limit (not possible)
- Ignoring batch size causing runtime errors
- Using transactions which have different limits
Solution
Step 1: Understand Firestore batch and transaction limits
Batch writes are limited to 500 operations; transactions have similar or stricter limits.Step 2: Apply best practice for large updates
Dividing 1200 updates into smaller batches (e.g., 3 batches of 400) avoids limit errors and ensures reliable commits.Step 3: Avoid single large batch or single transaction
Sending all 1200 in one batch or transaction exceeds limits and risks failure; updating one by one is inefficient.Final Answer:
Divide updates into 3 batches of 400 operations each and commit sequentially -> Option CQuick Check:
Split large updates into batches ≤ 500 ops [OK]
- Trying to batch all 1200 updates at once
- Using transactions for very large writes
- Updating records one by one causing slow performance
Ready to prove it?
Pass the test and this pattern moves unknown
