What are Firestore transactions?
Answer
Firestore transactions are atomic operations that read one or more documents, then update them based on the read data — ensuring consistency even with concurrent access. All reads happen before any writes, and the transaction retries automatically if a read document changes before the transaction completes. Example: await runTransaction(db, async (transaction) => { const docRef = doc(db, "accounts", "alice"); const snap = await transaction.get(docRef); if (!snap.exists()) throw new Error("Account not found"); const currentBalance = snap.data().balance; if (currentBalance < 100) throw new Error("Insufficient funds"); transaction.update(docRef, { balance: currentBalance - 100 }); transaction.update(doc(db, "accounts", "bob"), { balance: increment(100) }); }). Transactions: (1) Can read and write across multiple documents; (2) Are limited to 500 document reads/writes; (3) Timeout after 60 seconds; (4) Are retried up to 5 times on contention. Use transactions when reads determine writes. For pure writes without conditional logic, use batch writes instead.